码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-11 22:05:19      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

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

图片摘自大话设计模式:

bubuko.com,布布扣

运算类:

bubuko.com,布布扣
 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

加法类:

bubuko.com,布布扣
 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

减法类:

bubuko.com,布布扣
 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

乘法类:

bubuko.com,布布扣
 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

除法类:

bubuko.com,布布扣
 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

简单工厂类:

bubuko.com,布布扣
 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

客户端:

bubuko.com,布布扣
 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

 

大话设计模式_简单工厂模式(Java代码),布布扣,bubuko.com

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

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/creasylai19/p/3772231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!