设计模式之简单工厂模式

Posted smilingfish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之简单工厂模式相关的知识,希望对你有一定的参考价值。

简单工厂模式,也叫静态工厂模式,即由一个静态工厂类来统一创建所需要的实例对象。

本例中,将主要实现加减乘除计算。

第一步,定义抽象基类Operation

 1 /**
 2  * @author abo
 3  * @title Operation
 4  * @ProjectName Test
 5  * @Description: 运算类的基类,定义了两个运算数
 6  * @CreateDate 2018-10-13 14:15
 7  */
 8 public abstract  class Operation {
 9     private double numberA=0;
10     private double numberB=0;
11 
12     public double getNumberA() {
13         return numberA;
14     }
15 
16     public void setNumberA(double numberA) {
17         this.numberA = numberA;
18     }
19 
20     public double getNumberB() {
21         return numberB;
22     }
23 
24     public void setNumberB(double numberB) {
25         this.numberB = numberB;
26     }
27 
28     public abstract double getResult();
29 }

第二步:定义加减乘除四个子类继承于Operation类

OperationAdd(加法运算子类)

 1 /**
 2  * @author abo
 3  * @title OperationAdd
 4  * @ProjectName Test
 5  * @Description: 继承操作类,实现加法运算
 6  * @CreateDate 2018-10-13 14:17
 7  */
 8 public class OperationAdd extends Operation {
 9 
10     @Override
11     public double getResult() {
12         double result = 0;
13         result = getNumberA()+getNumberB();
14         return result;
15     }
16 }

OperationSub(减法运算子类)

 1 /**
 2  * @author abo
 3  * @title OperationSub
 4  * @ProjectName Test
 5  * @Description: 继承操作类,实现减法运算
 6  * @CreateDate 2018-10-13 14:22
 7  */
 8 public class OperationSub extends Operation {
 9     @Override
10     public double getResult() {
11         double result = 0;
12         result = getNumberA()-getNumberB();
13         return result;
14     }
15 }

OperationMul(乘法运算子类)

 1 /**
 2  * @author abo
 3  * @title OperationMul
 4  * @ProjectName Test
 5  * @Description: 继承操作类,实现乘法运算
 6  * @CreateDate 2018-10-13 14:23
 7  */
 8 public class OperationMul extends Operation {
 9     @Override
10     public double getResult() {
11         double result= 0;
12         result = getNumberA()*getNumberB();
13         return result;
14     }
15 }

OperationDiv(除法运算子类)

 1 /**
 2  * @author abo
 3  * @title OperationDiv
 4  * @ProjectName Test
 5  * @Description: 继承操作类,实现除法运算
 6  * @CreateDate 2018-10-13 14:24
 7  */
 8 public class OperationDiv extends Operation {
 9     @Override
10     public double getResult() {
11         double result=0;
12         result = getNumberA()/getNumberB();
13         return result;
14     }
15 }

OperationFactory(运算工厂类)

 1 /**
 2  * @author abo
 3  * @title OperationFactory
 4  * @ProjectName Test
 5  * @Description: 简单运算工厂类,用于创建操作类对象
 6  * @CreateDate 2018-10-13 14:26
 7  */
 8 public class OperationFactory {
 9     public static Operation createOperate(String operate){
10         Operation operation = null;
11         switch(operate){
12             case "+":
13                 operation=new OperationAdd();
14                 break;
15             case "-":
16                 operation=new OperationSub();
17                 break;
18             case "*":
19                 operation=new OperationMul();
20                 break;
21             case "/":
22                 operation=new OperationDiv();
23                 break;
24         }
25         return operation;
26     }
27 }

客户端程序Test类

 1         Scanner scanner = new Scanner(System.in);
 2         System.out.println("请输入第一个数字:");
 3         double numberA = scanner.nextDouble();
 4         System.out.println("请输入第二个数字:");
 5         double numberB = scanner.nextDouble();
 6         System.out.println("请输入操作符:如+、-、*、/");
 7         String operator = scanner.next();
 8         try {
 9             Operation oper;
10             oper = OperationFactory.createOperate(operator);
11             oper.setNumberA(200);
12             oper.setNumberB(4);
13             System.out.println("结果为:"+oper.getResult());
14         } catch (Exception e) {
15             e.printStackTrace();
16             System.out.println("运算结果出错!");
17         }

 

以上是关于设计模式之简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式之简单工厂模式

设计模式之简单工厂模式

创建型模式之简单工厂模式实例及代码操作

java设计模式之简单工厂模式

设计模式之工厂模式

设计模式之工厂模式-简单工厂(02)