版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
.......C/C++1Designpattern把單標(biāo)準(zhǔn)各部進(jìn)象歸耦合不神秘東初者也能要有信心法初者初歸納不合合對合要GangofFourDesignPatterns:ElementsResualbelSoftware歸23對象有對象5)合對象(7)對對象進(jìn)(11)對要5..............FactoryMethodPatternAbstractPatternPatternPrototypePatternSingletonPattern76ProxyPattern7DecoratorPattern功能說此比生成更靈活適配器AdapterPattern轉(zhuǎn)望另本由于兼容能起那起橋Bridge部都Pattern成部--戶Facade此更容Pattern配責(zé)111314
TemplateMethodPatternCommand客責(zé)ChainofResponsibilityPattern由起成此客責(zé)Pattern起換MediatorPatternObserverPattern生于都更MementoPattern部VisitorPattern于..............StatePatternInterpreterPatternPattern1.21)(OCP,OpenForExtension,ClosedForModificationPrinciple)2)(SRP,SingleResponsibilityPrinciple)3)(DIP,DependenceInversion)具體實(shí)()也針口編程4)口隔(ISP,InterfacePrinciple)強(qiáng)迫戶他口口把裝到去5)里氏替(LSP,SubstitutionPrinciple)任現(xiàn)他實(shí)現(xiàn)替實(shí)虛擬機(jī)現(xiàn)6)(CARP,Composite/AggregateReuse任換到7)(LOD,LawofDemeter)他口現(xiàn)他實(shí)現(xiàn)實(shí)現(xiàn)他
實(shí)實(shí)..............121)..............2)3)
2GoF..............---
socket
Singleton
#include<iostream>usingnamespacestd;//classSingelton{private:{m_singer=NULL;m_count=0;cout<<"Singelton...do"<<endl;}public:..............staticSingelton*getInstance(){if==NULL)//12{m_singer=newSingelton;}return}staticvoidprintT(){cout<<"m_count:"<<m_count<<endl;}private:staticSingeltonstaticintm_count;};Singelton*Singelton::m_singer=NULL;//int=0;voidmain01_1(){
cout<<""endl;*p1=//*p2=if(p1p2){cout<<""endl;}else{cout<<""<<endl;}p1->printT();p2->printT();system("pause");return;}//////////////////////////////////////////////////////////////////////////..............//classSingelton2{private:{m_singer=NULL;m_count=0;cout<<"Singelton...do"<<endl;}public:staticSingelton2*getInstance(){//if==NULL)//{//m_singer=new//}return}staticvoidSingelton2::FreeInstance(){if!=NULL){deletem_singer=NULL;m_count=0;}}staticvoidprintT(){cout<<"m_count:"<<m_count<<endl;}private:staticSingelton2*m_singer;staticintm_count;};Singelton2=new//newintSingelton2::m_count0;..............void{cout<<"
"<<endl;Singelton2*p1//Singelton2*p2=Singelton2::getInstance();if(p1p2){cout<<""endl;}else{cout<<""<<endl;}p1->printT();p2->printT();system("pause");}//1"GetInstance()//NULL==m_instance//2//3:C++
C++就進(jìn)切另所進(jìn)打印值發(fā)
權(quán)經(jīng)現(xiàn)問題拋..............#include"stdafx.h"#include"windows.h"#include"winbase.h"#include<process.h>#includeusingnamespacestd;class{private:{countcout<<"Singeltonbegin\n"<<endl;Sleep(1000);cout<<"Singeltonend\n"<<endl;}private://Singelton&obj){;}Singelton&operator=(constSingelton{;}public:staticSingelton*getSingelton(){//1"GetInstance()//NULL==m_instance..............//2//3single;}staticSingelton*releaseSingelton(){if(single!=NULL)//{cout<<"single;singleNULL;}single;}pirntS()//{printf("SingeltonprintStestcount:%dcount);}private:staticSingelton*single;staticcount;};//noteSingelton=newintSingelton::count=0;int_tmainTTT(intargc,_TCHAR*{*s1=*s2=if(s1==s2){cout<<"ok....equal"<<endl;}else{cout<<"not.equal"<<endl;}s1->pirntS();cout<<"hello...."<<endl;..............system("pause");0;}unsignedintthreadfunc2(void*myIpAdd){intidprintf("\nthreadfunc%d\n",id);return1;}voidthreadfunc(void*myIpAdd){intidprintf("\nthreadfunc%d\n",id);return;}int_tmain(intargc,_TCHAR*argv[]){inti0;DWORDdwThreadId[201],dwThrdParam=1;HANDLEhThread[201];intthreadnum=3;for(i=0;i<threadnum;i++){//hThread[i]=(HANDLE)_beginthreadex(NULL,0,&threadfunc,NULL,0,&dwThreadId[i]);hThread[i]=(HANDLE)_beginthread(&threadfunc,0,0);if(hThread[i]==NULL){thread%derror!!!\n",i);break;}}//,for(i=0;i<threadnum;i++){WaitForSingleObject(hThread[i],INFINITE);}printf("\n");for(i=0;i<threadnum;i++)..............{//CloseHandle(hThread[i]);}cout<<"hello...."<<endl;system("pause");0;}Double-CheckedLockingMFC//MFCDiagram#include"stdafx.h"#include"01.h"#include"01Dlg.h"#include"afxdialogex.h"#includeusingnamespacestd;//staticCCriticalSectioncs;//manpthread_create()class{private:{TRACE("Singletonbegin\n");Sleep(1000);TRACE("Singletonend\n");}Singleton(constSingleton&);Singleton&=Singleton&);public:staticprintV(){TRACE("printV..\n");..............}//Double-Check2if(pInstanceNULL)123//1123//223//31cs.unlock()23//422if(pInstance==NULL)2//52cs.unlock()33if(pInstance==NULL)newstaticSingleton*Instantialize(){if(pInstance==NULL)check{cs.Lock();//nullif(pInstanceNULL){pInstance=newSingleton();}cs.Unlock();}pInstance;}staticSingleton*pInstance;};Singleton*0;void{CCriticalSectioncs;cs.Lock();cs.Unlock();//TODO:
}voidthreadfunc(void*myIpAdd){..............intidTRACE("\nthreadfunc%d\n",id);Singleton::Instantialize()->printV();//Singelton::getSingelton()->pirntS();}voidDlg::OnBnClickedButton2(){inti0;DWORDdwThreadId[201],dwThrdParam=1;HANDLEhThread[201];intthreadnum=3;for(i=0;i<threadnum;i++){//hThread[i]=(HANDLE)_beginthreadex(0,&threadfunc,0,&dwThreadId[i]);hThread[i]=(HANDLE)_beginthread(&threadfunc,0,0);if(hThread[i]==NULL){TRACE("begin%derror!!!\n",i);break;}}for(i=0;i<threadnum;i++){WaitForSingleObject(hThread[i],INFINITE);}TRACE("\n");for(i=0;i<threadnum;i++){//CloseHandle(hThread[i]);}TRACE("ddddd\n");}..............
Synchronization
Windows—signalstatenonsignalstate可行反必須這:1Event標(biāo)志傳遞可可開始執(zhí)行它類:自重手重置manual-reset2界區(qū)Section臨界必須只擁那可護(hù)可該前前必釋放臨界3MutexSemaphore類臨界區(qū)只護(hù)可護(hù)4Semaphore它始可1釋它10須Windows98Windows95:可WaitableTimer它行這linux可23那可它C++標(biāo):1Primer(StanleyB.Lippman),始2interlockedEffectiveC++04(ScottMeyers)始MeyersDouble-CheckedLocking,Threads,CompilerOptimizations,andMoreScott..............Meyersauto_ptr.resetresetC++(CSDN)windows(JeffreyRichter)2.2,1.Creator2.Product口3.ConcreteProduct體//賴另參或返回值3缺點(diǎn)在整關(guān)鍵在必要判斷能夠據(jù)給信決竟在根據(jù)這整體點(diǎn)體在
當(dāng)體能要.............."iostream"usingnamespacestd;virtual//////////classFruit{public:virtualvoidgetFruit()=0;private:};classBananapublicFruit{public:virtualvoidgetFruit(){cout<<"}private:};class:publicFruit{public:virtualvoidgetFruit(){cout<<"..............}private:};class{public:staticFruit*Create(char*name){Fruit=NULL;if(strcmp(name,"pear")==0){tmpnew}elseif(strcmp(name,"banana")==0){tmp=newBanana();}else{NULL;}tmp;}private:};voidmain41(){Fruit*pear=Factory::Create("pear");if(pear==NULL){cout<<"pear}pear->getFruit();Fruit*banana=banana->getFruit();system("pause");}..............virtualBUG#includeusingnamespacestd;//////,////
COperation(2)//
CCalculatorFactory//
virtualclassCOperation{public:intintsecond;public:virtualdoubleGetResult()=0;private:};..............classAddOperation:publicCOperation{public:doubleGetResult(){first+second;}private:};classSubOperation:publicCOperation{public:doubleGetResult(){first}private:};classCCalculatorFactory{public:staticCOperation*CreateOperation(charcOperator){COperationtmp=NULL;switch(cOperator){'+':tmpnewAddOperation();break;'-':tmp=newbreak;default:tmp=NULL;}tmp;}};void{..............COperation=CCalculatorFactory::CreateOperation('+');=10;op1->second20;cout<<op1->GetResult()<<endl;COperation*op2=CCalculatorFactory::CreateOperation('-');op2->first10;op2->second=20;cout<<"hello...\n";system("pause");}2.3
CreatorConcreteCreator
ConcreteProduct
..............#includenamespacestd;..............classFruit{public:virtualvoidsayname(){cout<<"fruit\n";}};classFruitFactory{public:virtualFruit*{returnnewFruit();}};//classBananapublicFruit{public:virtualvoidsayname(){cout<<"Banana\n"<<endl;}};//classBananaFactorypublicFruitFactory{public:virtualFruit*{returnnewBanana;}};//class:publicFruit{..............public:virtualvoidsayname(){cout<<"Apple\n"<<endl;}};//classAppleFactory:publicFruitFactory{public:virtualFruit*{returnnewApple;}};void{ffNULL;Fruit*fruit//1=newfruitff->getFruit();fruit->sayname();fruit;deleteff;//2=newAppleFactory();fruitff->getFruit();fruit->sayname();fruit;deleteff;cout<<"hello....\n";system("pause");}..............2.41:,:,
//;()//()//1.
Creator2.ConcreteCreator..............3.Product4.Product..............classFruit{public:virtualvoidsayname(){cout<<"fruit\n";}};classFruitFactory{public:virtualgetApple(){returnnewFruit();}virtualgetBanana(){returnnewFruit();}};//classSouthBanana:publicFruit{public:virtualvoidsayname(){cout<<"SouthBanana}};//classSouthApple:publicFruit{public:virtualvoidsayname(){cout<<"SouthApple\n"<<endl;..............}};//classNorthBanana:publicFruit{public:virtualvoidsayname(){cout<<"NorthBanana\n"<<endl;}};//classNorthApple:publicFruit{public:virtualvoidsayname(){cout<<"NorthApple\n"<<endl;}};classSourthFruitFactory:publicFruitFactory{public:virtualgetApple(){returnnewSouthApple();}virtualgetBanana(){returnnewSouthBanana();}};classNorthFruitFactory:publicFruitFactory{public:virtualgetApple(){returnnewNorthApple();..............}virtualgetBanana(){returnnewNorthBanana();}};void{ffNULL;Fruit*fruit=newSourthFruitFactory();fruitfruit->sayname();fruitff->getBanana();fruit->sayname();fruit;deleteff;=newNorthFruitFactory();fruitfruit->sayname();fruitff->getBanana();fruit->sayname();fruit;deleteff;cout<<"hello....\n";system("pause");}2.5BuilderGoF23Builder-..............-builderBuilderConcreteBuilderABCDirectorProduct(?)(?)---AA..............4BB:AC精會作逐出來案例關(guān)字公隊(duì)#include<iostream>namespacestd;#include"string"classHouse{public:voidsetFloor(stringfloor){this->m_floor=floor;}voidwall){this->m_wall=}voidsetDoor(string{this->m_door}
墅..............//stringgetFloor(){}stringsetWall(){returnm_wall;}string{m_door;}private:stringstringm_wall;stringm_door;};classBuilder{public:virtualvoidmakeFloor()=voidmakeWall()=0;virtualvoidmakeDoor()=0;House*GetHouse()=0;};//classFlatBuild:publicBuilder{public:{pHousenewHouse;}virtualvoid{pHouse->setFloor("flatDoor");}virtualvoidmakeWall()..............{pHouse->setWall("flatWall");}virtualvoidmakeDoor(){pHouse->setDoor("flat}House*GetHouse(){pHouse;}private:House*pHouse;};//classVillaBuild:public{public:VillaBuild(){pHousenewHouse;}virtualvoid{pHouse->setFloor("villa}virtualvoidmakeWall(){pHouse->setWall("villaWall");}virtualvoidmakeDoor(){pHouse->setDoor("villaDoor");}House*GetHouse(){pHouse;}private:House*pHouse;};..............classDirector{public:void*builder){builder->makeFloor();builder->makeWall();builder->makeDoor();}private:};void{//House*pHose=newHouse;pHose->setDoor("wbm");pHose->setFloor("wbmFloor");pHose->setWall("wbmWall");deletepHose;/*//*builder=newFlatBuild;builder->makeFloor();builder->makeWall();builder->makeDoor();*///*directornew//*builder=newFlatBuild;
director->Construct(builder);//House*house=builder->GetHouse();cout<<house->getFloor()<<endl;deletehouse;builder;//buildernewVillaBuild;
..............director->Construct(builder);//house=builder->GetHouse();cout<<house->getFloor()<<endl;deletehouse;builder;director;system("pause");return;
}2.6PrototypePrototypePrototype..............classPerson{public:Person*Clone()=0;virtualvoidprintT()0;};classJavaProgrammer:publicPerson{public:JavaProgrammer(){this->m_name="";this->m_age=0;m_resume=NULL;}JavaProgrammer(stringintage){this->m_name=name;this->m_age=age;m_resume=NULL;}~JavaProgrammer(){if(m_resume!=NULL){free(m_resume);m_resume=NULL;}}virtualPerson{JavaProgrammer*p=JavaProgrammer;*p=*this;..............returnp;}voidsetResume(char*resume){m_resume=newchar[strlen(resume)+1];strcpy(m_resume,resume);}virtualvoidprintT(){cout<<"m_name:"<<<<"\t""m_age:"<<<<endl;if(m_resume!=NULL){cout<<<<}}private:stringm_name;intm_age;char*m_resume;};void{JavaProgrammerjavaperson1("java");Person=javaperson1.Clone();//p2->printT();deletep2;cout<<"hello..."<<endl;system("pause");return;}3
..............ProxyProxy..............
RealSubject
Proxy
abaprotocolabab#include<string>#include<iostream>namespacestd;//class{public:virtualvoidRequest()=0;};//classRealClass:public{public:..............virtualvoidRequest(){cout<<""<<endl;}};//class:public{private:RealClass*m_realClass;public:virtualvoidRequest(){m_realClass=newRealClass();m_realClass->Request();deletem_realClass;}};int{ProxyClass*test=newProxyClass();0;}2#include<iostream>namespacestd;/*
RealSubject
..............Proxyababprotocol*/classSubject{public:voidSaleBook()=0;private:};class:Subject{public:virtualvoidSaleBook(){cout<<"....\n";}private:};classProxyTaoBao:public{public:virtualvoidSaleBook(){//RealSubjectrs;rs.SaleBook();}void{<<"Double11"<<endl;}..............private:};classProxyTaoBao2:publicSubject{public:voidSetRealSubject(RealSubject*rs){m_s=rs;}virtualvoidSaleBook(){m_s->SaleBook();}void{<<"Double11"<<endl;}private:RealSubject};voidmain61(){ProxyTaoBao*ptb=ProxyTaoBao;ptb->SaleBook();deleteptb;return;}#includenamespacestd;//abbclassAppProtocol{public:virtualintApplicationDidFinsh()=0;private:};..............//classAppDelegate:publicAppProtocol{public:AppDelegate(){;}virtualintApplicationDidFinsh()//cocos2dx{cout<<"ApplicationDidFinshdo...\n";0;}};//ApplicationclassApplication{public:Application(){ap=NULL;}public:voidrun(){ap=newAppDelegate();ap->ApplicationDidFinsh();ap;}private:AppDelegate*ap;};//mainvoidmain31(){Application*appnewApplication();app->run();if==NULL){..............free(app);}cout<<"hello..."<<endl;system("pause");}3.2Decorator
..............DecoratorPattern#include<iostream>namespacestd;classCar{public:virtualshow()=0;private:};classRunCar:publicCar{public:voidrun(){cout<<""<<endl;}..............virtualvoidshow(){run();}private:};classSwimCarDirector:publicCar{public:SwimCarDirector(Car*p){m_p=p;}voidswim(){cout<<""<<endl;}virtualvoidshow(){m_p->show();}private:Car*m_p;};classFlyCarDirector:publicCar{public:FlyCarDirector(Car*p){m_p=p;}voidfly(){cout<<""<<endl;}virtualvoidshow(){..............m_p->show();fly();}private:Car*m_p;};void{Car*runcar=NULL;runcar=newRunCar;runcar->show();<<"SwimCarDirector*swimCar=newSwimCarDirector(runcar);swimCar->show();<<"fly"<<endl;FlyCarDirector=newFlyCarDirector(swimCar);flyCar->show();deleteflyCar;swimCar;deleteruncar;return;}3.3AdapterAdapter..............#include<iostream>namespacestd;classCurrent18v{public:voiduse18vCurrent(){cout<<"18v"<<endl;}private:};classCurrent220v{public:voiduse220vCurrent(){cout<<"220v"<<endl;..............}private:};classAdapter:publicCurrent18v{public:Adapter(Current220v*p220v){m_p220v=}voiduse18vCurrent(){cout<<"adapter"endl;m_p220v->use220vCurrent();}private:Current220v*m_p220v;};void{Current220v*p220vnewCurrent220v;Adapter*padapternewAdapter(p220v);padapter->use18vCurrent();deletepadapter;system("pause");return;}Player
AttackDefenseclasspublicPlayer..............classCenter:publicPlayer3.4
CompositeComponent---[]LeafComponentCompositeComponent“--..............#include<iostream>namespacestd;#include"string"http://classIFile{public:virtualvoidvirtualint*ifile)=0;virtual*ifile)=0;virtuallist<IFile*>*getChild()0;private:};classFilepublicIFile{public:File(string{NULL;="";name;}~File(){if(m_list!=NULL){deletem_list;}}virtualvoiddisplay(){cout<<<<endl;}virtualadd(IFile*ifile){-1;..............}virtual*ifile){-1;}virtuallist<IFile*>*getChild(){NULL;}private:list<IFile*>*m_list;stringm_name;};class:publicIFile{public:Folder(stringname){name;m_list=newlist<IFile*>;}Folder(){if(m_list==NULL){deletem_list;}}virtualvoiddisplay(){cout<<<<endl;}virtualadd(IFile*ifile){m_list->push_back(ifile);0;}virtual*ifile){0;..............}virtuallist<IFile*>*getChild(){m_list;}private:list<IFile*>*m_list;string};void*ifile,int{list<IFile*>*lint=0;fori<level;{printf("\t");}ifile->display();l=ifile->getChild();if!=NULL){for(list<IFile*>::iteratorit=l->begin();it++){if((*it)->getChild()NULL){for(i=0;i<=level;i++)//<={printf("\t");}}else{showTree((*it),level+1);}}}}..............void{Folder*root=newFolder=newFolder("111dir");File*txt1=newFile("aaa.txt");Folder*dir12newFolder("222dir");//dir12->display();File=newFile("222.txt");//txt12->display();root->display();root->add(dir1);root->add(txt1);dir1->add(dir12);dir1->add(txt12);/*list<IFile*>*ldir1->getChild();for(list<IFile*>::iteratorit=l->begin();it++){}*///cout<<""<<endl;showTree(root,deletetxt12;deletedir12;deletedir1;deletetxt1;deleteroot;cout<<"hello..."<<endl;system("pause");return;}..............3.5bridgeBridgeabstractionimplementationClientBridgeAbstractionRefinedAbstractionAbstractionImplementor(AbstractionImplementor)ConcreteImplementorImplementorPattern
..............
#include<iostream>namespacestd;classMyCar1{public:virtualvoidinstallEngine()=0;};class:publicMyCar1{public:virtualvoidinstallEngine(){cout<<3500CC"<<}};class:publicMyCar1{public:virtualvoidinstallEngine(){cout"BMW64000CC"<<endl;}};classJeep11:publicMyCar1{public:virtualvoidinstallEngine(){cout<<"Jeep111100CC"<<endl;}};..............classJeep12publicMyCar1{public:virtualvoidinstallEngine(){cout<<"Jeep121200CC"<<}};////voidmain1601(){Jeep12*j12=newj12->installEngin
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣西玉林市2022-2023學(xué)年五年級上學(xué)期英語期末試卷
- 物業(yè)管理常識與法規(guī)培訓(xùn)講義
- 三年戰(zhàn)略規(guī)劃報(bào)告
- 二零二五年度住宅小區(qū)監(jiān)控設(shè)備采購與安裝合同3篇
- 基于U-Net變體的醫(yī)學(xué)圖像分割算法綜述
- 陜西省渭南市尚德中學(xué)2024-2025學(xué)年高二上學(xué)期第二次質(zhì)量檢測歷史試卷(含答案)
- 城市社區(qū)居家養(yǎng)老服務(wù)體系的政策網(wǎng)絡(luò)治理-以政府購買公共服務(wù)模式為例
- 大功率電力半導(dǎo)體器件及新型功率器件產(chǎn)業(yè)化項(xiàng)目可行性研究報(bào)告寫作模板-申批立項(xiàng)
- 第18課 美國的獨(dú)立 課件(19張)
- 湖南省益陽市2024-2025學(xué)年高一(上)期末考試物理試卷(含答案)
- 2023中專《解剖學(xué)基礎(chǔ)》題庫202311593753185
- 化妝品生產(chǎn)許可申請表樣板
- 電工工具報(bào)價(jià)單
- 教科版三年級上冊科學(xué)教案(全冊)
- 勞動(dòng)力安排計(jì)劃及勞動(dòng)力計(jì)劃表(樣板)
- 利潤表4(通用模板)
- 教育評價(jià)學(xué)全套ppt課件完整版教學(xué)教程
- 注塑領(lǐng)班作業(yè)指導(dǎo)書
- ASTM B330-20 Standard Test Methods for Estimating Average Particle Size of Metal Powders and Related Compounds Using%2
- 顧客忠誠度論文
- 血?dú)夥治黾芭R床應(yīng)用
評論
0/150
提交評論