首页 > 代码库 > 设计模式(二)

设计模式(二)

模板方法模式

   GOOD:把不变的代码部分都转移到父类中,将可变的代码用virtual留到子类重写

#include<iostream>

#include<vector>

#include<string>

using namespacestd;

 

classAbstractClass

{

public:

       void Show()

       {

              cout<<"我是"<<GetName()<<endl;

       }

protected:

       virtual string GetName()=0;

};

 

class Naruto :public AbstractClass

{

protected:

       virtual string GetName()

       {

              return "火影史上最帅的六代目---一鸣惊人naruto";

       }

};

 

class OnePice :public AbstractClass

{

protected:

       virtual string GetName()

       {

              return "我是无恶不做的大海贼---路飞";

       }

};

 

//客户端

int main()

{

       Naruto* man = new Naruto();

       man->Show();

 

       OnePice* man2 = new OnePice();

       man2->Show();

 

       return 0;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

外观模式

   GOOD:为子系统的一组接口提供一个一致的界面。使用户使用起来更加方便。

#include<iostream>

#include<string>

using namespacestd;

 

class SubSysOne

{

public:

       void MethodOne()

       {

              cout<<"方法一"<<endl;

       }

};

 

class SubSysTwo

{

public:

       void MethodTwo()

       {

              cout<<"方法二"<<endl;

       }

};

 

classSubSysThree

{

public:

       void MethodThree()

       {

              cout<<"方法三"<<endl;

       }

};

 

//外观类

class Facade

{

private:

       SubSysOne* sub1;

       SubSysTwo* sub2;

       SubSysThree* sub3;

public:

       Facade()

       {

              sub1 = new SubSysOne();

              sub2 = new SubSysTwo();

              sub3 = new SubSysThree();

       }

       ~Facade()

       {

              delete sub1;

              delete sub2;

              delete sub3;

       }

 

       voidFacadeMethod()

       {

              sub1->MethodOne();

              sub2->MethodTwo();

              sub3->MethodThree();

       }

};

 

//客户端

int main()

{

       Facade* test = new Facade();

       test->FacadeMethod();

       return 0;

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

建造者模式(生成器模式)

   GOOD:在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用。

#include<string>

#include<iostream>

#include<vector>

using namespacestd;

 

//最终的产品类

class Product

{

private:

       vector<string> m_product;

public:

       void Add(string strtemp)

       {

              m_product.push_back(strtemp);

       }

       void Show()

       {

              vector<string>::iteratorp=m_product.begin();

              while (p!=m_product.end())

              {

                     cout<<*p<<endl;

                     p++;

              }

       }

};

 

//建造者基类

class Builder

{

public:

       virtual void BuilderA()=0;

       virtual void BuilderB()=0;

       virtual Product* GetResult()=0;

};

//第一种建造方式

classConcreteBuilder1 : public Builder

{

private:

       Product* m_product;

public:

       ConcreteBuilder1()

       {

              m_product=new Product();

       }

       virtual void BuilderA()

       {

              m_product->Add("one");

       }

       virtual void BuilderB()

       {

              m_product->Add("two");

       }

       virtual Product* GetResult()

       {

              return m_product;

       }

};

//第二种建造方式

classConcreteBuilder2 : public Builder

{

private:

       Product * m_product;

public:

       ConcreteBuilder2()

       {

              m_product=new Product();

       }

       virtual void BuilderA()

       {

              m_product->Add("A");

       }

       virtual void BuilderB()

       {

              m_product->Add("B");

       }

       virtual Product* GetResult()

       {

              return m_product;

       }

};

 

//指挥者类

class Direct

{

public:

       voidConstruct(Builder* temp)

       {

              temp->BuilderA();

              temp->BuilderB();

       }

};

 

//客户端

int main()

{

       Direct *p=new Direct();

       Builder* b1=new ConcreteBuilder1();

       Builder* b2=new ConcreteBuilder2();

 

       p->Construct(b1);     //调用第一种方式

       Product* pb1=b1->GetResult();

       pb1->Show();

 

       p->Construct(b2);          //调用第二种方式

       Product * pb2=b2->GetResult();

       pb2->Show();

       return 0;

}

 

例二(其实这个例子应该放在前面讲的):

#include<string>

#include<iostream>

#include<vector>

using namespacestd;

 

class Person

{

public:

       virtual void CreateHead()=0;

       virtual void CreateHand()=0;

       virtual void CreateBody()=0;

       virtual void CreateFoot()=0;

};

class ThinPerson: public Person

{

public:

       virtual void CreateHead()

       {

              cout<<"thinhead"<<endl;

       }

       virtual void CreateHand()

       {

              cout<<"thinhand"<<endl;

       }

       virtual void CreateBody()

       {

              cout<<"thinbody"<<endl;

       }

       virtual void CreateFoot()

       {

              cout<<"thinfoot"<<endl;

       }

};

 

classThickPerson : public Person

{

public:

       virtual void CreateHead()

       {

              cout<<"ThickPersonhead"<<endl;

       }

       virtual void CreateHand()

       {

              cout<<"ThickPersonhand"<<endl;

       }

       virtual void CreateBody()

       {

              cout<<"ThickPersonbody"<<endl;

       }

       virtual void CreateFoot()

       {

              cout<<"ThickPersonfoot"<<endl;

       }

};

//指挥者类

class Direct

{

private:

       Person* p;

public:

       Direct(Person* temp) { p = temp;}

       void Create()

       {

              p->CreateHead();

              p->CreateBody();

              p->CreateHand();

              p->CreateFoot();

       }

};

 

//客户端代码:

int main()

{

    Person *p=new ThickPerson();

       Direct *d= new Direct(p);

       d->Create();

       delete d;

       delete p;

       return 0;

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

观察者模式

   GOOD:定义了一种一对多的关系,让多个观察对象(公司员工)同时监听一个主题对象(秘书),主题对象状态发生变化时,会通知所有的观察者,使它们能够更新自己。

#include <string>

#include <iostream>

#include <vector>

using namespace std;

 

class Secretary;

//看股票的同事类(观察对象,观察者)

class StockObserver

{

private:

       stringname;

       Secretary*sub;

public:

       StockObserver(stringstrname,Secretary* strsub)

       {

              name=strname;

              sub=strsub;

       }

       voidUpdate();

};

//秘书类(主题对象,通知者)

class Secretary

{

private:

       vector<StockObserver>observers;

public:

       stringaction;

       voidAdd(StockObserver ob)

       {

              observers.push_back(ob);

       }

       voidNotify()

       {

       vector<StockObserver>::iterator p = observers.begin();

              while(p!=observers.end())

              {

                     (*p).Update();

                     p++;

              }

       }

};

 

void StockObserver::Update()

{

       cout<<name<<":"<<sub->action<<",不要玩股票了,要开始工作了"<<endl;

}

 

//客户端

int main()

{

   Secretary *p=new Secretary();  //创建通知者

 

       //观察者

       StockObserver*s1= new StockObserver("小李",p);

       StockObserver*s2 = new StockObserver("小赵",p);

   //加入通知队列

       p->Add(*s1);

       p->Add(*s2);

   //事件

       p->action="老板来了";

       //通知

       p->Notify();

       return0;

}

 

#include <string>

#include<iostream>

#include<vector>

using namespacestd;

 

classSecretaryBase;

//抽象观察者

classCObserverBase

{

protected:

       string name;

       SecretaryBase* sub;

public:

       CObserverBase(stringstrname,SecretaryBase* strsub)

       {

              name=strname;

              sub=strsub;

       }

       virtual void Update()=0;

};

//具体的观察者,看股票的

classStockObserver : public CObserverBase

{

public:

       StockObserver(stringstrname,SecretaryBase* strsub) : CObserverBase(strname,strsub)

       {

       }

       virtual void Update();

};

 

//具体观察者,看NBA的

classNBAObserver : public CObserverBase

{

public:

       NBAObserver(string strname,SecretaryBase*strsub) : CObserverBase(strname,strsub){}

       virtual void Update();

};

 

 

//抽象通知者

classSecretaryBase

{

public:

       string action;

       vector<CObserverBase*> observers;

public:

       virtual void Attach(CObserverBase*observer)=0;

       virtual void Notify()=0;

};

 

//具体通知者

class Secretary:public SecretaryBase

{

public:

       void Attach(CObserverBase* ob)

       {

              observers.push_back(ob);

       }

       void Notify()

       {

        vector<CObserverBase*>::iteratorp = observers.begin();

              while (p!=observers.end())

              {

                     (*p)->Update();

                     p++;

              }

       }

};

 

voidStockObserver::Update()

{

       cout<<name<<":"<<sub->action<<",不要玩股票了,要开始工作了"<<endl;

}

voidNBAObserver::Update()

{

       cout<<name<<":"<<sub->action<<",不要看NBA了,老板来了"<<endl;

}

 

 

客户端:

int main()

{

    SecretaryBase *p=new Secretary();  //创建观察者

      

       //被观察的对象

       CObserverBase *s1= new NBAObserver("小李",p);

       CObserverBase *s2 = newStockObserver("小赵",p);

    //加入观察队列

       p->Attach(s1);

       p->Attach(s2);

    //事件

       p->action="老板来了";

       //通知

       p->Notify();

 

       return 0;

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

设计模式(二)