【B2B研发商城】 【加入收藏】 【设为首页】 【进入论坛】 【站点地图】

你的位置:中国研发网 >> 技术文章 >> 软件设计 >> 软件工程 >> 详细内容 在线投稿

.Net设计模式之-外观设计模式实例分析

热度154票  浏览23次 【共0条评论】【我要评论 时间:2010年1月20日 15:42

一、外观模式简介(Brief Introduction

外观模式,为子系统的一组接口提供一个统一的界面,此模式定义了一个高层接口,这一个高层接口使的子系统更加容易使用。

 

二、解决的问题(What To Solve

1、分离不同的两个层

      典型的分层例子是Net三层架构,界面层与业务逻辑层分离,业务逻辑层与数据访问层分类。这样可以为子系统提供统一的界面和接口,降低了系统的耦合性。

2、减少依赖

      随着功能增加及程序的重构,系统会变得越来越复杂,这时增加一个外观可以提供一个简单的接口,减少他们之间的依赖。

3、为新旧系统交互提供接口

      有的时候,新系统需要旧系统的核心功能,而这个旧的系统已经很难维护和扩展,可以给新系统增加一个Façade类,是的新系统与Façade类交互,Façade类与旧系统交互素有复杂的工作。

三、外观模式分析(Analysis

1、外观模式结构

 

2、源代码

1、子系统类SubSystemOne

publicclassSubSystemOne

{

   publicvoidMethodOne()

   {

       Console.WriteLine("执行子系统One中的方法One");

   }

}

 

2、子系统类SubSystemTwo

publicclassSubSystemTwo

{

   publicvoidMethodTwo()

   {

       Console.WriteLine("执行子系统Two中的方法Two");

   }

}

 

3、子系统类SubSystemThree

publicclassSubSystemThree

{

   publicvoidMethodThree()

   {

       Console.WriteLine("执行子系统Three中的方法Three");

   }

}

 

4Facade外观类,为子系统类集合提供更高层次的接口和一致的界面

publicclassFacade

{

   SubSystemOneone;

   SubSystemTwotwo;

   SubSystemThreethree;

 

   publicFacade()

   {

       one =newSubSystemOne();

       two =newSubSystemTwo();

       three =newSubSystemThree();

   }

 

   publicvoidMethodA()

   {

       Console.WriteLine("开始执行外观模式中的方法A");

       one.MethodOne();

       two.MethodTwo();

       Console.WriteLine("外观模式中的方法A执行结束");

       Console.WriteLine("---------------------------");

   }

 

   publicvoidMethodB()

   {

       Console.WriteLine("开始执行外观模式中的方法B");

       two.MethodTwo();

       three.MethodThree();

       Console.WriteLine("外观模式中的方法B执行结束");

   }

}

 

5、客户端代码

staticvoidMain(string[] args)

{

   Facadefacade =newFacade();

   facade.MethodA();

   facade.MethodB();

   Console.Read();

}

3、程序运行结果

四.案例分析(Example

1、场景

假设远程网络教育系统-用户注册模块包括功能有

1、验证课程是否已经满人

2、收取客户费用

3、通知用户课程选择成功

如下图所示

子系统类集合包括:PaymentGateway类、RegisterCourse类、NotifyUser

PaymentGateway类:用户支付课程费用

RegisterCourse类:验证所选课程是否已经满人以及计算课程的费用

NotifyUser类:"用户选择课程成功与否"通知用户

RegistrationFacade类:外观类,提供一个统一的界面和接口,完成课程校验、网上支付、通知用户功能

2、代码

1、子系统类集合

1.  namespace FacadePattern   

2.  {   

3.      /// <summary>  

4.      /// Subsystem for making financial transactions  

5.      /// </summary>  

6.      public class PaymentGateway   

7.      {   

8.          public bool ChargeStudent(string studentName, int costTuition)   

9.          {   

10.              //Charge the student  

11.              Console.WriteLine(String.Format("Charging student {0} for ${1}", studentName, costTuition.ToString()));   

12.              return true;   

13.          }   

14.      }   

15.    

16.      /// <summary>  

17.      /// Subsystem for registration of courses  

18.      /// </summary>  

19.      public class RegisterCourse   

20.      {   

21.          public 本站所有文章欢迎任何形式的转载,但请注明作者及出处,尊重他人劳动成果!
文章转载自:中国研发网 [http://www.yanfaw.com]
本文标题:.Net设计模式之-外观设计模式实例分析

TAG: Net NET 实例 模式 设计 外观
顶:6 踩:9
对本文中的事件或人物打分:
当前平均分:-0.33 (49次打分)
对本篇资讯内容的质量打分:
当前平均分:0.84 (49次打分)
【已经有41人表态】
7票
感动
6票
路过
5票
高兴
4票
难过
3票
搞笑
2票
愤怒
6票
无聊
8票
同情
上一篇 下一篇
发表评论

网友评论仅供网友表达个人看法,并不表明本网同意其观点或证实其描述。

查看全部回复【已有0位网友发表了看法】