外观模式的C#实现
来源:互联网 发布:倩女幽魂mac版 编辑:程序博客网 时间:2024/05/21 06:19
借鉴于各路大神,也就不做链接了
class FacadePattern { /// <summary> /// 子系统1 /// </summary> public class System1 { public void Method() { Console.WriteLine("System1.Method"); } } /// <summary> /// 子系统2 /// </summary> public class System2 { public void Method() { Console.WriteLine("System2.Method"); } } /// <summary> /// 子系统3 /// </summary> public class System3 { public void Method() { Console.WriteLine("System3.Method"); } } /// <summary> /// 外观类 /// </summary> public class Facade { private System1 system1; private System2 system2; private System3 system3; public Facade() { system1 = new System1(); system2 = new System2(); system3 = new System3(); } public void Method()//外观类中存在多种通过子系统完成的操作,我们不需要知道它怎么完成的,只管调用就好 { system1.Method(); system2.Method(); system3.Method(); } public void Method1() { system1.Method(); system3.Method(); } } //调用 //FacadePattern.Facade facade = new FacadePattern.Facade(); //facade.Method(); //然而问题来了,如果我觉得 子系统2 在某种情况下不好用,我又不能更换 //那我就再写一个 子系统4 代替这种情况下的 子系统2 ,然后在 外观类 中增加一个方法 //好像又篡改了别人的代码 //可以通过抽象外观类解决这个问题 //当需要大量重复使用某种操作的时候新建一个外观类继承于抽象外观类,重新“组装”一个方法供使用 //新增子系统可以不更改原代码 public abstract class AbstractFacade { public abstract void Method(); } /// <summary> /// 新建外观类 继承于抽象外观类,用 子系统4 代替 子系统2 /// </summary> public class Facade1 : AbstractFacade { private System1 system1; private System4 system4; private System3 system3; public Facade1() { system1 = new System1(); system4 = new System4(); system3 = new System3(); } public override void Method() { system1.Method(); system4.Method(); system3.Method(); } } /// <summary> /// 子系统4 /// </summary> public class System4 { public void Method() { Console.WriteLine("System4.Method"); } } //调用 //FacadePattern.Facade1 facade = new FacadePattern.Facade1(); //facade.Method(); }
阅读全文
0 0
- 外观模式的C#实现
- 外观模式以及FishiGUI子系统外观模式的实现
- 外观模式(facade)C#
- 【C#设计模式-外观模式】
- C#设计模式--外观模式
- 外观模式c++实现
- 外观模式 JAVA实现
- 外观模式lua实现
- 外观模式----C++实现
- 外观模式 C++实现
- 外观模式C++实现
- java 外观模式实现
- 外观模式C++实现
- 外观模式(java实现)
- GOF23设计模式之外观模式不使用外观模式的实现
- C#设计模式之外观
- 数据库连接池之外观模式+单例模式的实现
- 最常用的设计模式---外观模式(C++实现)
- A Comprehensive Guide to Data Exploration(数据探索的综合指南)
- js变量赋值3e3
- 数据库基本概念&MySQL基本操作详解
- c++ 输出到文本格式 ofstream && FILE
- 按钮闪烁效果
- 外观模式的C#实现
- cannot resolve corresponding JNI function
- GIT的使用2(基本配置)
- eclipse快捷键
- 数据结构 红黑树
- String、StringBuffer与StringBuilder之间区别
- python读取二进制文件以及二进制、十进制、16进制间的转化
- c++/python协程服务器框架
- Python编程细节(三)