Headfirst设计模式的C++实现——外观模式(Facade)
light.h
1 #ifndef _LIGHT_H_ 2 #define _LIGHT_H_ 3 4 #include <iostream> 5 6 class LIGHT { 7 public: 8 void dim(int level) { std::cout << "LIGHT dim to " << level << std::endl; } 9 }; 10 11 #endif
popper.h
1 #ifndef _POPPER_H_ 2 #define _POPPER_H_ 3 4 #include <iostream> 5 6 class POPPER { 7 public: 8 void on() { std::cout << "POPPER on" << std::endl; } 9 void pop() { std::cout << "POPPER pop" << std::endl; } 10 }; 11 #endif
facade.h
1 #ifndef _FACADE_H_ 2 #define _FACADE_H_ 3 4 #include "light.h" 5 #include "popper.h" 6 7 class FACADE { 8 private: 9 POPPER & popper; 10 LIGHT & light; 11 public: 12 FACADE(POPPER &_popper, LIGHT &_light) : popper(_popper), light(_light) {} 13 void watch_movie() { 14 popper.on(); 15 popper.pop(); 16 light.dim(5); 17 } 18 }; 19 #endif
main.cpp
1 #include "facade.h" 2 3 int main() 4 { 5 LIGHT light; 6 POPPER popper; 7 FACADE facade(popper, light); 8 facade.watch_movie(); 9 }