码迷,mamicode.com
首页 > 其他好文 > 详细

学习大话设计模式01_简单工厂模式(简易计算器)

时间:2014-08-19 12:43:14      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   div   log   new   

 1 /**
 2  * 面向对象(运算类)
 3  * 简易计算器
 4  * @author Monica
 5  *
 6  */
 7 public class Operation {
 8     private double numberA = 0;
 9     private double numberB = 0;
10     
11     //虚函数
12     public double GetResult() {
13         double result = 0;
14         return result;
15     }
16 
17     public double getNumberA() {
18         return numberA;
19     }
20 
21     public void setNumberA(double numberA) {
22         this.numberA = numberA;
23     }
24 
25     public double getNumberB() {
26         return numberB;
27     }
28 
29     public void setNumberB(double numberB) {
30         this.numberB = numberB;
31     }
32 }

 

 1 /**
 2  * 加法
 3  * @author Monica
 4  *
 5  */
 6 public class OperationAdd extends Operation {
 7     
 8     /**
 9      * 继承运算类
10      */
11     @Override
12     public double GetResult() {
13         double result = 0;
14         result = getNumberA() + getNumberB();
15         return super.GetResult();
16     }
17 }

 

 1 /**
 2  * 简单工厂模式
 3  * @author Administrator
 4  *
 5  */
 6 public class OperationFactory {
 7 
 8     public static Operation createOperate(char operate) {
 9         Operation oper = null;
10         switch (operate) {
11         case ‘+‘:
12             oper = new OperationAdd();
13             break;
14         }
15         return oper;
16     }
17 }

 

 1 public class Main {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         Operation oper; 
 8         oper = OperationFactory.createOperate(‘+‘);
 9         oper.setNumberA(1);
10         oper.setNumberB(2);
11         double result = oper.GetResult();
12     }
13 
14 }

 

学习大话设计模式01_简单工厂模式(简易计算器),布布扣,bubuko.com

学习大话设计模式01_简单工厂模式(简易计算器)

标签:style   blog   color   io   ar   div   log   new   

原文地址:http://www.cnblogs.com/qqiaoq/p/3921599.html

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