名称 | Facade |
结构 | |
意图 | 为子系统中的一组接口提供一个一致的界面,F a c a d e 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。 |
适用性 |
|
Code Example |
1
//
Facade
2 3 // Intent: "Provide a unified interface to a set of interfaces in a subsystem. 4 // Facade defines a higher-level interface that makes the subsystem easier 5 // to use". 6 7 // For further information, read "Design Patterns", p185, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9 10 /**/ /* Notes: 11 * Many subsystems are complex to manage - and this is a dis-incentive for 12 * client code to use them. A Facasde design pattern provides a simlified 13 * high-level API to the client, thus shielding it from direct contact 14 * with the (more complex) subsystem classes. 15 * 16 * Often the code that is inside a facade would have to be inside client 17 * code without the facade. The subsystem code beneath the facade can 18 * change, without affecting the client code. 19 */ 20 21 namespace Facade_DesignPattern 22 { 23 using System; 24 25 class SubSystem_class1 26 { 27 public void OperationX() 28 { 29 Console.WriteLine("SubSystem_class1.OperationX called"); 30 } 31 } 32 33 class SubSystem_class2 34 { 35 public void OperationY() 36 { 37 Console.WriteLine("SubSystem_class2.OperationY called"); 38 } 39 } 40 41 class SubSystem_class3 42 { 43 public void OperationZ() 44 { 45 Console.WriteLine("SubSystem_class3.OperationZ called"); 46 } 47 } 48 49 class Facade 50 { 51 private SubSystem_class1 c1 = new SubSystem_class1(); 52 private SubSystem_class2 c2 = new SubSystem_class2(); 53 private SubSystem_class3 c3 = new SubSystem_class3(); 54 55 public void OperationWrapper() 56 { 57 Console.WriteLine("The Facade OperationWrapper carries out complex decision-making"); 58 Console.WriteLine("which in turn results in calls to the subsystem classes"); 59 c1.OperationX(); 60 if (1==1 /**//*some really complex decision*/) 61 { 62 c2.OperationY(); 63 } 64 // lots of complex code here . . . 65 c3.OperationZ(); 66 } 67 68 } 69 70 /**//// <summary> 71 /// Summary description for Client. 72 /// </summary> 73 public class Client 74 { 75 public static int Main(string[] args) 76 { 77 Facade facade = new Facade(); 78 Console.WriteLine("Client calls the Facade OperationWrapper"); 79 facade.OperationWrapper(); 80 return 0; 81 } 82 } 83} 84 85 |
转载于:https://www.cnblogs.com/DarkAngel/archive/2005/06/30/183725.html