首页 > 代码库 > 大话设计模式_简单工厂模式(Java代码)

大话设计模式_简单工厂模式(Java代码)

简单的描述:一个父类、多个子类,实例化那个子类由一个单独的工厂类来进行

图片摘自大话设计模式:

运算类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class Calculate {
 4 
 5     private double firstNum;
 6     private double secondNum;
 7 
 8     public double getFirstNum() {
 9         return firstNum;
10     }
11 
12     public void setFirstNum(double firstNum) {
13         this.firstNum = firstNum;
14     }
15 
16     public double getSecondNum() {
17         return secondNum;
18     }
19 
20     public void setSecondNum(double secondNum) {
21         this.secondNum = secondNum;
22     }
23 
24     public double getResult() {
25         return 0d;
26     }
27 }
Calculate

加法类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class OperationAdd extends Calculate {
 4 
 5     @Override
 6     public double getResult() {
 7         return getFirstNum() + getSecondNum();
 8     }
 9 
10 }
OperationAdd

减法类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class OperationMinus extends Calculate {
 4 
 5     @Override
 6     public double getResult() {
 7         return getFirstNum() - getSecondNum();
 8     }
 9     
10 }
OperationMinus

乘法类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class OperationMutil extends Calculate {
 4 
 5     @Override
 6     public double getResult() {
 7         return getFirstNum() * getSecondNum();
 8     }
 9     
10 }
OperationMutil

除法类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class OperationDivide extends Calculate {
 4 
 5     @Override
 6     public double getResult() {
 7         if( Math.abs(getSecondNum()) < 0.000001 ) {
 8             throw new RuntimeException("除数不能为0");
 9         }
10         return getFirstNum() / getSecondNum();
11     }
12 
13 }
OperationDivide

简单工厂类:

 1 package com.longsheng.simpleFactory;
 2 
 3 public class OperationFactory {
 4 
 5     public static Calculate getCalculate( String operation ) {
 6         Calculate cal = null;
 7         switch( operation ) {
 8             case "+":
 9                 cal = new OperationAdd();
10                 break;
11             case "-":
12                 cal = new OperationMinus();
13                 break;
14             case "*":
15                 cal = new OperationMutil();
16                 break;
17             case "/":
18                 cal = new OperationDivide();
19                 break;
20         }
21         return cal;
22     }
23     
24 }
OperationFactory

客户端:

 1 //两个数运算,输出结果
 2 package com.longsheng.simpleFactory;
 3 
 4 import java.util.Scanner;
 5 
 6 public class Client {
 7 
 8     public static void main(String[] args) {
 9         double firstNum;
10         String oper;
11         double secondNum;
12 
13         Scanner sc = new Scanner(System.in);
14 
15         System.out.println("请输入第一个数字");
16         firstNum = sc.nextDouble();
17         System.out.println("请输入运算符(+、-、*、/)");
18         oper = sc.next();
19         System.out.println("请输入第二个运算符");
20         secondNum = sc.nextDouble();
21 
22         Calculate cal = OperationFactory.getCalculate(oper);
23         cal.setFirstNum(firstNum);
24         cal.setSecondNum(secondNum);
25         System.out.print("运算的结果是");
26         System.out.println(cal.getResult());
27         sc.close();
28     }
29 
30 }
Client