• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gooliugle
博客园    首页    新随笔    联系   管理    订阅  订阅
大话设计模式读书笔记1----简单工厂模式

简单工厂模式----计算器代码

代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 
  5 namespace SimpleFactory
  6 {
  7     class Program
  8     {
  9         static void Main(string[] args)
 10         {
 11             double sum = 0;
 12             Operation oper = null;
 13             oper = OperationFactory.createOperate("+");
 14             oper.NumberA = 1;
 15             oper.NumberB = 2;
 16             sum = oper.getResult();
 17             Console.WriteLine(sum);
 18             Console.ReadLine();
 19         }
 20     }
 21     public abstract class Operation
 22     {
 23         private double _NumberA;
 24 
 25         public double NumberA
 26         {
 27             get { return _NumberA; }
 28             set { _NumberA = value; }
 29         }
 30         private double _NumberB;
 31 
 32         public double NumberB
 33         {
 34             get { return _NumberB; }
 35             set { _NumberB = value; }
 36         }
 37         public virtual double getResult()
 38         {
 39             double result = 0.0;
 40             return result;
 41         }
 42     }
 43     class OprationAdd : Operation
 44     {
 45         public override double getResult()
 46         {
 47             double result = 0;
 48             result = NumberA + NumberB;
 49             return result;
 50         }
 51     }
 52     class OprationSub : Operation
 53     {
 54         public override double getResult()
 55         {
 56             double result = 0;
 57             result = NumberA - NumberB;
 58             return result;
 59         }
 60     }
 61     class OprationMul : Operation
 62     {
 63         public override double getResult()
 64         {
 65             double result = 0;
 66             result = NumberA * NumberB;
 67             return result;
 68         }
 69     }
 70     class OprationDiv : Operation
 71     {
 72         public override double getResult()
 73         {
 74             double result = 0;
 75             if (NumberB == 0)
 76             {
 77                 throw new Exception("除数不能为零。");
 78             }
 79             else
 80             {
 81                 result = NumberA / NumberB;
 82             }
 83             return result;
 84         }
 85     }
 86     public class OperationFactory
 87     {
 88         public static Operation createOperate(string operate)
 89         {
 90             Operation oper = null;
 91             switch (operate)
 92             {
 93                 case "+":
 94                     oper = new OprationAdd();
 95                     break;
 96                 case "-":
 97                     oper = new OprationSub();
 98                     break;
 99                 case "*":
100                     oper = new OprationMul();
101                     break;
102                 case "/":
103                     oper = new OprationDiv();
104                     break;
105             }
106             return oper;
107         }
108     }
109 
110 }


 

posted on 2010-04-23 21:56  gooliugle  阅读(345)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3