• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hello World
This is the first demo for each programming language
            管理     

看《你必须知道的.NET》有感--工厂模式的另类解读

最近在看博客园里的推荐的《你必须知道的.NET》,对里面有一段关于银行的客户和职员的安排的程序有点兴趣。这里我把他改了下,促进理解:

 

Code
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace ConsoleApplication1
  6{
  7    class Program
  8    {
  9        static void Main(string[] args)
 10        {
 11            bankManage bank = new bankManage();
 12            bank.operate(new savingRequestion());
 13            bank.operate(new outingRequest());
 14            bank.operate(new turningRequest());
 15            Console.ReadKey();
 16        }

 17    }

 18    /**//// <summary>
 19    /// 银行业务接口
 20    /// </summary>

 21    public interface operation
 22    {
 23        void operate();
 24    }

 25    /**//// <summary>
 26    /// 存钱业务
 27    /// </summary>

 28    public class savingBox : operation
 29    {
 30        operation Members#region operation Members
 31
 32        public void operate()
 33        {
 34            Console.Write("You are saving!\n");
 35        }

 36
 37        #endregion

 38    }

 39    /**//// <summary>
 40    /// 取钱业务
 41    /// </summary>

 42    public class outingBox : operation
 43    {
 44        operation Members#region operation Members
 45
 46        public void operate()
 47        {
 48            Console.Write("You are outing!\n");
 49        }

 50
 51        #endregion

 52    }

 53    /**//// <summary>
 54    /// 需求
 55    /// </summary>

 56    public interface requestion
 57    {
 58        operation request();
 59    }

 60    /**//// <summary>
 61    /// 存钱需求
 62    /// </summary>

 63    public class savingRequestion : requestion
 64    {
 65        requestion Members#region requestion Members
 66
 67        public operation request()
 68        {
 69            return new savingBox();
 70        }

 71
 72        #endregion

 73    }

 74    /**//// <summary>
 75    /// 取钱需求
 76    /// </summary>

 77    public class outingRequest : requestion
 78    {
 79        requestion Members#region requestion Members
 80
 81        public operation request()
 82        {
 83            return new outingBox();
 84        }

 85
 86        #endregion

 87    }

 88    /**//// <summary>
 89    /// 银行管理
 90    /// </summary>

 91    public class bankManage
 92    {
 93
 94        public void operate(requestion man)
 95        {
 96            operation oper = man.request();
 97            oper.operate();
 98        }

 99    }

100    /**//// <summary>
101    /// 转账业务
102    /// </summary>

103    public class turningBox : operation
104    {
105        operation Members#region operation Members
106
107        public void operate()
108        {
109            Console.Write("you are turning!\n");
110        }

111
112        #endregion

113    }

114    /**//// <summary>
115    /// 转账需求
116    /// </summary>

117    public class turningRequest : requestion
118    {
119        requestion Members#region requestion Members
120
121        public operation request()
122        {
123            return new turningBox();
124        }

125
126        #endregion

127    }

128}

类图:

如果我们屏蔽设计模式在本银行系统中的应用,单从供求关系来理解这段程序最好不过了,对于一个银行系统来说,主要包含职员和客户,职员按分工的不同分为诸如存款职员、转账职员、取款职员等,客户又分为存款客户、取款客户、转账客户等,从这方面来讲,似乎更难理解,换种方式,将银行系统分为需求和业务,客户对应需求,每个银行的客户都会对银行产生一种需求;银行提供的服务既是业务,职员对应业务;于是这样产生一个自然界的恒等式,即需求=业务,如果客户没有需求,那么银行就不会提供该种业务;所以在程序中不论用户的存款、取款、转账需求,最终都将return一个银行业务,单对于每一个业务的处理过程(operate),由于是银行内部的事情,所以应封装在业务类中,中间采用一个银行管理对象来(bankManage)连接二者之间的交互过程。

posted @ 2008-08-27 11:04  Justin X  阅读(657)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3