Facade

  • 分割したサブシステムへの入り口を1ヶ所にまとめる
    • サブシステム間の結合度が軽減される
  • クライアントは必ずFacadeを経由してサブシステムを利用するので、サブシステム内の細かい単位を直接利用できなくなる

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // The 'Subsystem ClassA' class
  5. class SubSystemOne {
  6. public:
  7. void MethodOne() {
  8. cout << " SubSystemOne Method" << endl;
  9. }
  10. };
  11.  
  12. // The 'Sybsystem ClassB' class
  13. class SubSystemTwo {
  14. public:
  15. void MethodTwo() {
  16. cout << " SubSystemTwo Method" << endl;
  17. }
  18. };
  19.  
  20. // The 'Sybsystem ClassC' class
  21. class SubSystemThree {
  22. public:
  23. void MethodThree() {
  24. cout << " SubSystemThree Method" << endl;
  25. }
  26. };
  27.  
  28. // The 'Sybsystem ClassD' class
  29. class SubSystemFour {
  30. public:
  31. void MethodFour() {
  32. cout << " SubSystemFour Method" << endl;
  33. }
  34. };
  35.  
  36. // The 'Facade' class
  37. class Facade {
  38. public:
  39. Facade() {
  40. _one = new SubSystemOne();
  41. _two = new SubSystemTwo();
  42. _three = new SubSystemThree();
  43. _four = new SubSystemFour();
  44. }
  45. virtual ~Facade() {
  46. delete _one;
  47. delete _two;
  48. delete _three;
  49. delete _four;
  50. }
  51. void MethodA() {
  52. cout << endl << "MethodA() ---- " << endl;
  53. _one->MethodOne();
  54. _two->MethodTwo();
  55. _four->MethodFour();
  56. }
  57. void MethodB() {
  58. cout << endl << "MethodB() ---- " << endl;
  59. _two->MethodTwo();
  60. _three->MethodThree();
  61. }
  62. private:
  63. SubSystemOne *_one;
  64. SubSystemTwo *_two;
  65. SubSystemThree *_three;
  66. SubSystemFour *_four;
  67. };
  68.  
  69. // client
  70. int main() {
  71. Facade *facade = new Facade();
  72.  
  73. facade->MethodA();
  74. facade->MethodB();
  75.  
  76. delete facade;
  77. return 0;
  78. }
  79.  

出力
MethodA() ----
 SubSystemOne Method
 SubSystemTwo Method
 SubSystemFour Method

MethodB() ----
 SubSystemTwo Method
 SubSystemThree Method



参考サイト

デザインパターンを“喩え話”で分かり易く理解する
http://www.netlaputa.ne.jp/~hijk/study/oo/designpattern.html



デザインパターンの骸骨たち
http://www002.upp.so-net.ne.jp/ys_oota/mdp/

最終更新:2012年03月16日 01:03