《大话设计模式》c++实现 外观模式

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

 

外观模式在什么时候使用呢?

分为三个阶段:

(1)首先,在设计初期阶段,应该要有意识的将不同的两个层分离。

(2)第二,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也会产生很多很小的类,这本是好事儿,但是也给外部调用他们的用户程序带来了使用上的困难,增加外观Facade可以提供一个简单的接口,减少他们之间的依赖。

(3)第三,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但因为它包含非常重要的功能,新的需求开发必须要依赖于它。此时用外观模式Facade也是非常合适的。

 1 #include<iostream>
 2 #include<string>
 3 
 4 class SubSystemOne{
 5 public:
 6     void MethodOne(){
 7         std::cout << "SubSystemOne MethodOne  " << std::endl;
 8     }
 9 };
10 
11 class SubSystemTwo{
12 public:
13     void MethodTwo(){
14         std::cout << "SubSystemTwo MethodTwo  " << std::endl;
15     }
16 };
17 
18 class SubSystemThree{
19 public:
20     void MethodThree(){
21         std::cout << "SubSystemThree MethodThree  " << std::endl;
22     }
23 };
24 
25 class SubSystemFour{
26 public:
27     void MethodFour(){
28         std::cout << "SubSystemFour MethodFour  " << std::endl;
29     }
30 };
31 
32 
33 class Facade{
34 private:
35     SubSystemOne* one;
36     SubSystemTwo* two;
37     SubSystemThree* three;
38     SubSystemFour* four;
39 public:
40     Facade(){
41         one = new SubSystemOne();
42         two = new SubSystemTwo();
43         three = new SubSystemThree();
44         four = new SubSystemFour();
45 
46     }
47     ~Facade(){
48         delete one;
49         delete two;
50         delete three;
51         delete four;
52     }
53     void MethodA(){
54         std::cout << "MethodA-------" << std::endl;
55         one->MethodOne();
56         two->MethodTwo();
57         four->MethodFour();
58         std::cout << std::endl;
59     }
60     void MethodB(){
61         std::cout << "MethodB-------" << std::endl;
62         two->MethodTwo();
63         three->MethodThree();
64         std::cout << std::endl;
65     }
66 
67 };
68 
69 
70 
71 
72 
73 //Client
74 void main()
75 {
76     Facade* facade = new Facade();
77 
78     facade->MethodA();
79     facade->MethodB();
80 
81     delete facade;
82 
83     system("pause");
84 
85 }

猜你喜欢

转载自www.cnblogs.com/zle1992/p/10126983.html