7适配器模式和外观模式.docx
- 文档编号:7296901
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:16
- 大小:59.17KB
7适配器模式和外观模式.docx
《7适配器模式和外观模式.docx》由会员分享,可在线阅读,更多相关《7适配器模式和外观模式.docx(16页珍藏版)》请在冰豆网上搜索。
7适配器模式和外观模式
适配器模式
类图:
代码:
#include
usingnamespacestd;
classDuck
{
public:
virtualvoidquack()=0;
virtualvoidfly()=0;
};
classMallardDuck:
publicDuck
{
public:
MallardDuck()
{
}
~MallardDuck()
{
}
voidquack()
{
cout<<"Quack"< } voidfly() { cout<<"I'mflying"< } }; classTurkey { public: virtualvoidgobble()=0; virtualvoidfly()=0; }; classWildTurkey: publicTurkey { public: WildTurkey() { } ~WildTurkey() { } voidgobble() { cout<<"Gobblegobble"< } voidfly() { cout<<"I'mflyingashortdistance"< } }; classTurkeyAdapter: publicDuck { public: Turkey*turkey; TurkeyAdapter(Turkey*t) { turkey=t; } voidquack() { turkey->gobble(); } voidfly() { for(inti=0;i<5;i++) turkey->fly(); } ~TurkeyAdapter() { } }; voidtestDuck(Duck*d) { d->quack(); d->fly(); } intmain() { MallardDuck*duck=newMallardDuck(); WildTurkey*turkey=newWildTurkey(); Duck*turkeyAdapter=newTurkeyAdapter(turkey); cout<<"TheTuekeysays..."< turkey->gobble(); turkey->fly(); cout<<"TheDucksays..."< testDuck(duck); cout<<"TheTurkeyAdaptersays..."< testDuck(turkeyAdapter); } 外观模式 类图: 代码: #include usingnamespacestd; classPopcornPopper { public: PopcornPopper() { } ~PopcornPopper() { } voidon() { cout<<"PopcornPopperon"< } voidoff() { cout<<"PopcornPopperoff"< } voidpop() { cout<<"PopcornPopperpoppingpopcorn! "< } voidtoString() { cout<<"PopcornPopper"< } }; classTheaterLights { public: TheaterLights() { } ~TheaterLights() { } voidon() { cout<<"TheaterCeilingLightson"< } voidoff() { cout<<"TheaterCeilingLightsoff"< } voiddim() { cout<<"TheaterCeilingLightsdimmingto10%"< } voidtoString() { cout<<"TheaterCeilingLights"< } }; classScreen { public: Screen() { } ~Screen() { } voidup() { cout<<"TheaterScreengoingup"< } voiddown() { cout<<"TheaterScreengoingdown"< } voidtoString() { cout<<"TheaterScreen"< } }; classTurner { public: //Amplifer*amplifer; intam; intfm; intfrequency; Turner() { } ~Turner() { } voidon() { cout<<"Turneron"< } voidoff() { cout<<"Turneroff"< } voidsetAm(inta) { am=a; } voidsetFm(intf) { fm=f; } voidsetFrequency(intf) { frequency=f; } voidtoString() { cout<<"Turner"< } }; classCdPlayer { public: //Amplifer*amplifer; CdPlayer() { } ~CdPlayer() { } voidon() { cout<<"Top-O-LineCdPlayeron"< } voidoff() { cout<<"Top-O-LineCdPlayeroff"< } voideject() { cout<<"Top-O-LineCdPlayereject"< } voidplay(stringmovie) { cout<<"Top-O-LineCdPlayerplaying\""< } voidpause(stringmovie) { cout<<"Top-O-LineCdPlayerpaused\""< } voidstop(stringmovie) { cout<<"Top-O-LineCdPlayerstopped\""< } }; classDvdPlayer { public: //Amplifer*amplifer; DvdPlayer() { } ~DvdPlayer() { } voidon() { cout<<"Top-O-LineDvdPlayeron"< } voidoff() { cout<<"Top-O-LineDvdPlayeroff"< } voideject() { cout<<"Top-O-LineDvdPlayereject"< } voidplay(stringmovie) { cout<<"Top-O-LineDvdPlayerplaying\""< } voidpause(stringmovie) { cout<<"Top-O-LineDvdPlayerpaused\""< } voidsetSurroundAudio() { cout<<"Top-O-LineDvdPlayersettingsurroundAudio"; } voidsetTwoChannelAudio() { cout<<"Top-O-LineDvdPlayersettingtwoChannelAudio"; } voidstop(stringmovie) { cout<<"Top-O-LineDvdPlayerstopped\""< } }; classProjector { public: //Amplifer*amplifer; Projector() { } ~Projector() { } voidon() { cout<<"Top-O-LineProjectoron"< } voidoff() { cout<<"Top-O-LineProjectoroff"< } voidtvMode() { cout<<"Top-O-LineProjectorintvmode"< } voidwideScreenMode() { cout<<"Top-O-LineProjectorinwideScreenmode(16x9aspectratio)"< } voidtoString() { cout<<"Top-O-LineProjector"< } }; classAmplifer { public: Turner*turner; DvdPlayer*dvdPlayer; CdPlayer*cdPlayer; Amplifer() { } ~Amplifer() { } voidon() { cout<<"Top-O-LineAmpliferon"< } voidoff() { cout<<"Top-O-LineAmpliferoff"< } voidsetCd(CdPlayer*cd) { cdPlayer=cd; } voidsetDvd(DvdPlayer*dvd) { dvdPlayer=dvd; cout<<"Top-O-LineAmpliersettingDVDplayertoTop-O-LineDVDPlayer"< } voidsetStereoSound() { cout<<"Top-O-LineAmplierstersosoundon(1speakers,1subwoofer)"< } voidsetSurroundSound() { cout<<"Top-O-LineAmpliersurroundsoundon(5speakers,1subwoofer)"< } voidsetTurner(Turner*t) { turner=t; } voidsetVolume() { cout<<"Top-O-LineAmpliersettingvolumeto5"< } voidtoString() { cout<<"Amplifer"< } }; classHomeTheaterFacade { public: Amplifer*amp; Turner*turner; DvdPlayer*dvd; CdPlayer*cd; Projector*projector; TheaterLights*lights; Screen*screen; PopcornPopper*popper; HomeTheaterFacade(Amplifer*a,Turner*t,DvdPlayer*d,CdPlayer*c,Projector*p,TheaterLights*l,Screen*s,PopcornPopper*po) { amp=a; turner=t; dvd=d; cd=c; projector=p; lights=l; screen=s; popper=po; } voidwatchMovie(stringmovie) { cout<<"Getreadytowatchamovie..."< popper->on(); popper->pop(); lights->dim(); screen->down(); projector->on(); projector->wideScreenMode(); amp->on(); amp->setDvd(dvd); amp->setSurroundSound(); amp->setVolume(); dvd->on(); dvd->play(movie); } voidendMovie(stringmovie) { cout<<"Shuttingmovietheaterdown..."< popper->off(); lights->on(); screen->up(); projector->off(); amp->off(); dvd->stop(movie); dvd->eject(); dvd->off(); } }; intmain() { HomeTheaterFacade*homeTheater=newHomeTheaterFacade(newAmplifer(),newTurner(),newDvdPlayer(),newCdPlayer(),newProjector(),newTheaterLights(),newScreen(),newPopcornPopper()); homeTheater->watchMovie("RaidersoftheLostArk"); homeTheater->endMovie("RaidersoftheLostArk"); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 适配器 模式 外观