1 2 3 4 5 6 …">

帐前卒专栏

Without software, we are nothing.

Design Pattern -- Facade Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Facade.cpp : Defines the entry point for the console application.
// author: chillyc
// email: chillycreator @t gmail.com

#include<iostream>

namespace pattern{
  /// this pattern is facade pattern
  /// facade pattern is for hiding relationship and behavior of inner classes .
  /// client or outer class should only connect with facade class.
  /// But it will not forbid accessing inner classes.
#ifndef INNERA_H
#define INNERA_H
  class Inner_A{
  public:
      void Do(){std::cout<<"A"<<std::endl;}
  };
#endif
#ifndef INNERB_H
#define INNERB_H
  class Inner_B{
  public:
      void Do(){std::cout<<"B"<<std::endl;}
  };
#endif

#ifndef FACADE_H
#define FACADE_H
  class Facade{
  private:
      Inner_A* a;
      Inner_B* b;
  public:
      Facade(){
          a = new Inner_A();
          b = new Inner_B();

      }
      void Do(){
          a->Do();
          b->Do();
      }
      ~Facade(){
          delete a;
          delete b;
      }
  };
#endif
}

int main()
{
  pattern::Facade f;
  f.Do();
  return 0;
}

Comments