您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 2、CC++与设计模式基础课程-讲义-v1.0.4
C++C/C++11.1--DesignpatternGangofFourDesignPatterns:ElementsofResualbelSoftware23:5)(7)(11)C++51FactoryMethodPattern2AbstractFactoryPattern3BuilderPattern4PrototypePattern5SingletonPattern76ProxyPattern7DecoratorPattern8AdapterPattern9BridgePattern10CompositePattern--11FacadePattern12FlyweightPattern1113TemplateMethodPattern14CommandPattern15ChainofResponsibilityPattern16StrategyPattern17MediatorPattern18ObserverPattern19MementoPatternC++20VisitorPattern21StatePattern22InterpreterPattern23IteratorPattern1.21)(OCP,OpenForExtension,ClosedForModificationPrinciple)2)(SRP,SingleResponsibilityPrinciple)3)(DIP,DependenceInversionPrinciple)(),()4)(ISP,InterfaceSegegationPrinciple)5)(LSP,LiskovSubstitutionPrinciple)6)(CARP,Composite/AggregateReusePrinciple)7)(LOD,LawofDemeter)()C++121)C++2)3)22.12.2.1C++GoF2.2.2-socketservlet--SingletonSingleton2.2.3a)b)c)2.2.4#includeiostreamusingnamespacestd;//classSingelton{private:Singelton(){m_singer=NULL;m_count=0;coutSingelton...doendl;}C++public:staticSingelton*getInstance(){if(m_singer==NULL)//12{m_singer=newSingelton;}returnm_singer;}staticvoidprintT(){coutm_count:m_countendl;}private:staticSingelton*m_singer;staticintm_count;};Singelton*Singelton::m_singer=NULL;//intSingelton::m_count=0;voidmain01_1(){coutendl;Singelton*p1=Singelton::getInstance();//Singelton*p2=Singelton::getInstance();if(p1!=p2){coutendl;}else{coutendl;}p1-printT();p2-printT();system(pause);return;}C++////////////////////////////////////////////////////////////////////////////classSingelton2{private:Singelton2(){m_singer=NULL;m_count=0;coutSingelton...doendl;}public:staticSingelton2*getInstance(){//if(m_singer==NULL)//{//m_singer=newSingelton2;//}returnm_singer;}staticvoidSingelton2::FreeInstance(){if(m_singer!=NULL){deletem_singer;m_singer=NULL;m_count=0;}}staticvoidprintT(){coutm_count:m_countendl;}private:staticSingelton2*m_singer;staticintm_count;};Singelton2*Singelton2::m_singer=newSingelton2;//newC++intSingelton2::m_count=0;voidmain(){coutendl;Singelton2*p1=Singelton2::getInstance();//Singelton2*p2=Singelton2::getInstance();if(p1!=p2){coutendl;}else{coutendl;}p1-printT();p2-printT();Singelton2::FreeInstance();Singelton2::FreeInstance();system(pause);}2.2.5//1GetInstance()//NULL==m_instance//2//3:C++C++C++#includestdafx.h#includewindows.h#includewinbase.h#includeprocess.h#includeiostreamusingnamespacestd;classSingelton{private:Singelton(){count++;coutSingeltonbegin\nendl;Sleep(1000);coutSingeltonend\nendl;}private://Singelton(constSingelton&obj){;}Singelton&operator=(constSingelton&obj){;}public:staticSingelton*getSingelton(){C++//1GetInstance()//NULL==m_instance//2//3returnsingle;}staticSingelton*releaseSingelton(){if(single!=NULL)//{cout\nendl;deletesingle;single=NULL;}returnsingle;}voidpirntS()//{printf(SingeltonprintStestcount:%d\n,count);}private:staticSingelton*single;staticintcount;};//noteSingelton*Singelton::single=newSingelton();intSingelton::count=0;int_tmainTTT(intargc,_TCHAR*argv[]){Singelton*s1=Singelton::getSingelton();Singelton*s2=Singelton::getSingelton();if(s1==s2){coutok....equalendl;}else{coutnot.equalendl;}s1-pirntS();C++Singelton::releaseSingelton();couthello....endl;system(pause);return0;}unsignedintthreadfunc2(void*myIpAdd){intid=GetCurrentThreadId();printf(\nthreadfunc%d\n,id);return1;}voidthreadfunc(void*myIpAdd){intid=GetCurrentThreadId();printf(\nthreadfunc%d\n,id);Singelton::getSingelton()-pirntS();return;}int_tmain(intargc,_TCHAR*argv[]){inti=0;DWORDdwThreadId[201],dwThrdParam=1;HANDLEhThread[201];intthreadnum=3;for(i=0;ithreadnum;i++){//hThread[i]=(HANDLE)_beginthreadex(NULL,0,&threadfunc,NULL,0,&dwThreadId[i]);hThread[i]=(HANDLE)_beginthread(&threadfunc,0,0);if(hThread[i]==NULL){printf(beginthread%derror!!!\n,i);break;}}for(i=0;ithreadnum;i++){WaitForSingleObject(hThread[i],INFINITE);}C++printf(\n);for(i=0;ithreadnum;i++){//CloseHandle(hThread[i]);}Singelton::releaseSingelton();couthello....endl;system(pause);return0;}2.2.6Double-CheckedLockingMFC//MFCDiagram#includestdafx.h#include01.h#include01Dlg.h#includeafxdialogex.h#includeiostreamusingnamespacestd;//staticCCriticalSectioncs;classSingleton{private:Singleton(){TRACE(Singletonbegin\n);Sleep(1000);TRACE(Singletonend\n);}Singleton(constSingleton&);Singleton&operator=(constSingleton&);public:C++staticvoidprintV(){TRACE(printV..\n);}//Double-Check2if(pInstance==NULL)123//1123//2123//31cs.unlock()23//422if(pInstance==NULL)2//52cs.unlock()33if(pInstance==NULL)newstaticSingleton*Instantialize(){if(pInstance==NULL)//doublecheck{cs.Lock();//pInstancenullif(pInstance==NULL){pInstance=newSingleton();}cs.Unlock();}returnpInstance;}staticSingleton*pInstance;};Singleton*Singleton::pInstance=0;voidCMy01Dlg::OnBnClickedButton1(){CCriticalSectioncs;cs.Lock();cs.Unlock();//TODO:}C++voidthreadfunc(void*myIpAdd){intid=GetCurrentThreadId();TRACE(\nthreadfunc%d\n,id);Singleton::Instantialize()-printV();//Singelton::getSingelton()-pirntS();}voidCMy01
本文标题:2、CC++与设计模式基础课程-讲义-v1.0.4
链接地址:https://www.777doc.com/doc-6391596 .html