适配器模式
将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。
code implement:
<pre name="code" class="html">Duck refer to Target TurkeyAdapter refer to Adapter Adaptee
pulbic class TurkeyAdapter implements Duck { Turkey turkey; public TurKeyAdapter(Turkey turkey) { this.turkey = turkey; } public void quack()// 适配接口 { turkey.gobble(); } public void fly() { for(int i=0; i<5; i++) { turkey.fly(); } }
枚举迭代器设计
上述为对象适配器,对于另一种类适配器,暂不考虑。
提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
class HomeTheaterFacade { private: Amplifier* _amp; private: Tuner* _tuner; private: DvdPlayer* _dvd; private: CdPlayer* _cd; private: Projector* _projector; private: TheaterLights* _lights; private: Screen* _screen; private: PopcornPopper* _popper; private: HomeTheaterFacade( const HomeTheaterFacade& ); // Disable copy constructor private: void operator=( const HomeTheaterFacade& ); // Disable assignment operator public: HomeTheaterFacade( Amplifier* amp, Tuner* tuner, DvdPlayer* dvd, CdPlayer* cd, Projector* projector, Screen* screen, TheaterLights* lights, PopcornPopper* popper ) : _amp( amp ), _tuner( tuner ), _dvd( dvd ), _cd( cd ), _projector( projector ), _lights( lights ), _screen( screen ), _popper( popper ) { assert(amp); assert(tuner); assert(dvd); assert(cd); assert(projector); assert(screen); assert(lights); assert(popper); } public: void watchMovie( std::string movie ) { std::cout << "Get ready to watch a movie..." << std::endl; _popper->on(); _popper->pop(); _lights->dim( 10 ); _screen->down(); _projector->on(); _projector->wideScreenMode(); _amp->on(); _amp->setDvd( _dvd ); _amp->setSurroundSound(); _amp->setVolume( 5 ); _dvd->on(); _dvd->play( movie ); } public: //more interface }