前言:一点一点的积累吧。如果文章中,有不恰当的地方,还请指正。之前看过《大话设计模式》,今天通过手敲代码进一步加深理解简单工厂模式!
下面是我按照我的思路的实现,有的步骤,谁在前谁在后都无所谓。
1.抽象出相同的属性和方法。这里可以用接口,也可以用抽象类。在开发中接口用的比较多。
package ch06; /** * @ClassName Operation * @Description TODO * @author XingZhaohu [email protected] * @date 2016-4-9 下午8:52:43 */ public abstract class Operation { private double numberA; private double numberB; public Operation(){ } public Operation(double numberA,double numberB){ this.numberA = numberA; this.numberB = numberB; } /** * @param numberA the numberA to set */ public void setNumberA(double numberA) { this.numberA = numberA; } /** * @param numberB the numberB to set */ public void setNumberB(double numberB) { this.numberB = numberB; } public double getNubmerA(){ return this.numberA; } public double getNumberB(){ return this.numberB; } public abstract double getResult();//需要重写的方法。 }
2.实现各种具体操作,比如简单的加减乘除。
package ch06; /** * @ClassName Addtion * @Description 加法类 * @author XingZhaohu [email protected] * @date 2016-4-9 下午9:29:26 */ public class Addtion extends Operation{ public Addtion(){ } /** * @param numberA * @param numberB */ public Addtion(double numberA, double numberB) { super(numberA, numberB); // TODO Auto-generated constructor stub } /* (non-Javadoc) * @see ch06.Operation#getResult() */ @Override public double getResult() { // TODO Auto-generated method stub return super.getNubmerA()+super.getNumberB(); } }
package ch06; /** * @ClassName Subtraction * @Description 减法类 * @author XingZhaohu [email protected] * @date 2016-4-9 下午9:26:31 */ public class Subtraction extends Operation{ public Subtraction(){ } /** * @param numberA * @param numberB */ public Subtraction(double numberA, double numberB) { super(numberA, numberB); // TODO Auto-generated constructor stub } /* (non-Javadoc) * @see ch06.Operation#getResult() */ @Override public double getResult() { // TODO Auto-generated method stub return super.getNubmerA()-super.getNumberB(); } }
package ch06; /** * * @ClassName Multiply * @Description 乘法类 * @author XingZhaohu [email protected] * @date 2016-4-9 下午10:58:42 */ public class Multiply extends Operation{ public Multiply(){ } public Multiply(double numberA,double nubmerB){ super(numberA,nubmerB); } /* (non-Javadoc) * @see ch06.Operation#getResult() */ @Override public double getResult() { // TODO Auto-generated method stub return super.getNubmerA()*super.getNumberB(); } }
package ch06; /** * @ClassName Divsion * @Description 除法类 * @author XingZhaohu [email protected] * @date 2016-4-9 下午10:58:32 */ public class Divsion extends Operation{ /* (non-Javadoc) * @see ch06.Operation#getResult() */ @Override public double getResult() { // TODO Auto-generated method stub return super.getNubmerA()/super.getNumberB(); } }
3.利用静态工厂实例化具体的操作。
package ch06; /** * @ClassName OperatorFactory * @Description 利用静态工厂实例化各种操作 * @author XingZhaohu [email protected] * @date 2016-4-9 下午9:34:33 */ public class OperatorFactory { public static Operation createOperation(char operator){ Operation ope = null; switch(operator){ case '+': ope = new Addtion();break; case '-': ope = new Subtraction();break; case '*': ope = new Multiply();break; case '/': ope = new Divsion();break; } return ope; } }
4.测试类;
整个程序中自己定义了构造函数,但是没有用到,原本想用过构造函数来给数值的。不能用抽象类来new一个对象,可以用它的非抽象子类来new对象。这也就是向上转型!
package ch06; import java.util.Scanner; /** * @ClassName ComputerTest * @Description TODO * @author XingZhaohu [email protected] * @date 2016-4-9 下午9:44:18 */ public class ComputerTest { public static void main(String[] args){ /* * 声明一下:只是为了简单的测试,并没有处理异常。 * */ Operation ope = null; Scanner in = new Scanner(System.in); System.out.println("请输入数A:"); double numberA = in.nextDouble(); System.out.println("请输入运算符:"); char operator = in.next().charAt(0);//运算符 ope = OperatorFactory.createOperation(operator);//利用静态工厂实例化对应得对象 System.out.println("请输入数B:"); double numberB = in.nextDouble();//数B ope.setNumberA(numberA); ope.setNumberB(numberB); System.out.printf("%f%c%f=%f",numberA,operator,numberB,ope.getResult()); } }