标签:设计模式 工厂模式
工厂模式,是用工厂方法代替new来创建对象的一种模式。通过这种模式使代码更容易扩展,接下来我们以简单计算器为例为说明。
一、需求分析
1、输入操作A,操作数B及运算符“+,-,*,/”,然后根据运算符作相应的运算
2、返回运算结果
二、编码前思考
通过什么样的方法使得我们的代码易维护,易扩展,易复用?
这四种运算的共同点是:
a、都有2个操作数A,B
b、都返回计算结果
不同点是:计算方式不同
那么,我们就可以把相同点封装在一个父类Operator里面,代码如下:
public abstract class Operator { protected double operatorA; protected double operatorB; protected double result; private String[] opStr = new String[]{"+","-","*","/"}; public static class operator { public static final short ADD = 0; public static final short MINUS = 1; public static final short MUL = 2; public static final short DIV = 3; } public Operator(double a,double b) { this.operatorA = a; this.operatorB = b; } public abstract double getResult(); public void printf(short op) { System.out.println(this.operatorA + this.opStr[op]+this.operatorB+"="+this.result); } }
接下来就可以分别写加、减、乘、除四个类来继承Operator类。
加法类:
public class OperatorAdd extends Operator { public OperatorAdd(double a, double b) { super(a, b); // TODO Auto-generated constructor stub } @Override public double getResult() { this.result = this.operatorA + this.operatorB; this.printf(Operator.operator.ADD); return this.result; } }
减法类:
public class OperatorMinus extends Operator { public OperatorMinus(double a, double b) { super(a, b); // TODO Auto-generated constructor stub } @Override public double getResult() { this.result = this.operatorA - this.operatorB; this.printf(Operator.operator.MINUS); return this.result; } }
乘法类:
public class OperatorMul extends Operator { public OperatorMul(double a, double b) { super(a, b); // TODO Auto-generated constructor stub } @Override public double getResult() { this.result = this.operatorA * this.operatorB; this.printf(Operator.operator.MUL); return this.result; } }
除法类:
public class OperatorDiv extends Operator { public OperatorDiv(double a, double b) { super(a, b); // TODO Auto-generated constructor stub } @Override public double getResult() { if(this.operatorB == 0) { try { System.out.println("除数不能为0"); throw new Exception(); } catch (Exception e) { e.printStackTrace(); } }else{ this.result = this.operatorA / this.operatorB; this.printf(Operator.operator.DIV); } return this.result; } }
工厂类来负责对应对象的初始化:
public class OperatorFactory { public static Operator operatorMake(double a,double b,short op) { Operator operator = null; switch(op) { case Operator.operator.ADD: operator = new OperatorAdd(a,b); break; case Operator.operator.MINUS: operator = new OperatorMinus(a,b); break; case Operator.operator.MUL: operator = new OperatorMul(a,b); break; case Operator.operator.DIV: operator = new OperatorDiv(a,b); break; } return operator; } }
测试:
public class MyOperator { public static void main(String[] args) { Operator op = null; //加法 op = OperatorFactory.operatorMake(33, 6, Operator.operator.ADD); op.getResult(); //减法 op = OperatorFactory.operatorMake(99, 82, Operator.operator.MINUS); op.getResult(); //乘法 op = OperatorFactory.operatorMake(23, 8, Operator.operator.MUL); op.getResult(); //除法 op = OperatorFactory.operatorMake(150, 3, Operator.operator.DIV); op.getResult(); } }
输出效果:
33.0+6.0=39.0
99.0-82.0=17.0
23.0*8.0=184.0
150.0/3.0=50.0
如果还要其他运算,比如开根,平方等,则只需要继承Operator类,然后在工厂类里面加上相应的对象初始化即可。
本文出自 “严富坤-昆明移动互联网” 博客,请务必保留此出处http://kinbos.blog.51cto.com/2092114/1665125
标签:设计模式 工厂模式
原文地址:http://kinbos.blog.51cto.com/2092114/1665125