版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.......C/C++1Designpattern把单标准各部进象归耦合不神秘东初者也能要有信心法初者初归纳不合合对合要GangofFourDesignPatterns:ElementsResualbelSoftware归23对象有对象5)合对象(7)对对象进(11)对要5..............FactoryMethodPatternAbstractPatternPatternPrototypePatternSingletonPattern76ProxyPattern7DecoratorPattern功能说此比生成更灵活适配器AdapterPattern转望另本由于兼容能起那起桥Bridge部都Pattern成部--户Facade此更容Pattern配责111314
TemplateMethodPatternCommand客责ChainofResponsibilityPattern由起成此客责Pattern起换MediatorPatternObserverPattern生于都更MementoPattern部VisitorPattern于..............StatePatternInterpreterPatternPattern1.21)(OCP,OpenForExtension,ClosedForModificationPrinciple)2)(SRP,SingleResponsibilityPrinciple)3)(DIP,DependenceInversion)具体实()也针口编程4)口隔(ISP,InterfacePrinciple)强迫户他口口把装到去5)里氏替(LSP,SubstitutionPrinciple)任现他实现替实虚拟机现6)(CARP,Composite/AggregateReuse任换到7)(LOD,LawofDemeter)他口现他实现实现他
实实..............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++就进切另所进打印值发
权经现问题抛..............#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标志传递可可开始执行它类:自重手重置manual-reset2界区Section临界必须只拥那可护可该前前必释放临界3MutexSemaphore类临界区只护可护4Semaphore它始可1释它10须Windows98Windows95:可WaitableTimer它行这linux可23那可它C++标: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缺点在整关键在必要判断能够据给信决竟在根据这整体点体在
当体能要.............."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精会作逐出来案例关字公队#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"//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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 施工劳务分包合同协议书2025年
- 生物公司收购居间合同2025年
- 个人给个人借款合同范本2025年
- 2025年学校食堂装修施工及食品安全培训服务合同3篇
- 2025年度战略合作合同标的及合作领域3篇
- 2025年学校食堂装修施工与餐饮服务标准制定合同3篇
- 2025年度家政服务退款协议合同(2025版)3篇
- 个人借款担保合同范本3
- GB/T 44944-2024鞋类和鞋类部件抗真菌性能定量评估试验方法
- 深圳市罗湖区小升初学位申请 租赁合同
- 伤口造口护理质量标准
- 热性惊厥诊断治疗与管理专家共识
- 《桥梁轻量化监测系统建设规范(征求意见稿)》
- 现代农业产业园建设规划方案(2篇)
- 物流配送中心租赁合同
- 幼儿园幼小衔接方案及反思
- 生命科学前沿技术智慧树知到期末考试答案章节答案2024年苏州大学
- 低空经济产业园项目可行性研究报告
- 中国神话故事绘本仓颉造字
- 消化道出血护理新进展
- MOOC 心理健康与创新能力-电子科技大学 中国大学慕课答案
评论
0/150
提交评论