首页 > 其他 > 详细

设计模式 (三)

时间:2014-11-06 22:06:05      阅读:347      评论:0      收藏:0      [点我收藏+]

抽象工厂模式

   GOOD:定义了一个创建一系列相关或相互依赖的接口,而无需指定它们的具体类。

             用于交换产品系列,如ACCESS->SQLSERVER;产品的具体类名被具体工厂的实现分离

例:

#include<string>

#include<iostream>

#include<vector>

using namespacestd;

 

//用户抽象接口

class IUser

{

public :

       virtual void GetUser()=0;

       virtual void InsertUser()=0;

};

 

//部门抽象接口

classIDepartment

{

public:

       virtual void GetDepartment()=0;

       virtual void InsertDepartment()=0;

};

 

//ACCESS用户

classCAccessUser : public IUser

{

public:

       virtual void GetUser()

       {

              cout<<"Access GetUser"<<endl;

       }

       virtual void InsertUser()

       {

              cout<<"AccessInsertUser"<<endl;

       }

};

 

//ACCESS部门

classCAccessDepartment : public IDepartment

{

public:

       virtual void GetDepartment()

       {

              cout<<"AccessGetDepartment"<<endl;

       }

       virtual void InsertDepartment()

       {

              cout<<"AccessInsertDepartment"<<endl;

       }

};

 

//SQL用户

class CSqlUser :public IUser

{

public:

       virtual void GetUser()

       {

              cout<<"SqlUser"<<endl;

       }

       virtual void InsertUser()

       {

              cout<<"SqlUser"<<endl;

       }

};

 

//SQL部门类

classCSqlDepartment: public IDepartment

{

public:

       virtual void GetDepartment()

       {

              cout<<"sqlgetDepartment"<<endl;

       }

       virtual void InsertDepartment()

       {

              cout<<"sqlinsertdepartment"<<endl;

       }

};

 

//抽象工厂

class IFactory

{

public:

       virtual IUser* CreateUser()=0;

       virtual IDepartment*CreateDepartment()=0;

};

 

//ACCESS工厂

classAccessFactory : public IFactory

{

public:

       virtual IUser* CreateUser()

       {

              return new  CAccessUser();

       }

       virtual IDepartment* CreateDepartment()

       {

              return new CAccessDepartment();

       }

};

 

//SQL工厂

class SqlFactory: public IFactory

{

public:

       virtual IUser* CreateUser()

       {

              return new  CSqlUser();

       }

 

       virtual IDepartment* CreateDepartment()

       {

              return new CSqlDepartment();

       }

};

 

客户端:

int main()

{

       IFactory* factory= new SqlFactory();

       IUser* user=factory->CreateUser();

       IDepartment* depart =factory->CreateDepartment();

       user->GetUser();

       depart->GetDepartment();

       return 0;

}

状态模式

   GOOD:当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,可考虑用到状态模式

#include<iostream>

using namespacestd;

 

class Work;

class ForenoonState;

class NoonState;

 

class State

{

public:

       virtual void WriteProgram(Work* w)=0;

};

 

class Work

{

private:

       State* current;

public:

       double hour;

public:

       Work();

       void SetState(State* temp)

       {

       current =temp;

       }

       void Writeprogram()

       {

              current->WriteProgram(this);

       }

 

};

 

class NoonState:public State

{

public:

       virtual void WriteProgram(Work* w)

       {

              cout<<"execute"<<endl;

              if((w->hour)<13)

                     cout<<"还不错啦"<<endl;

              else

                     cout<<"不行了,还是睡觉吧"<<endl;

       }

};

 

classForenoonState : public State

{

public:

       virtual void WriteProgram(Work* w)

       {

              if((w->hour)<12)

                     cout<<"现在的精神无敌好"<<endl;

              else

              {

                     w->SetState(newNoonState());

                     w->Writeprogram(); //注意加上这句

              }

       }

};

 

 

Work::Work()

{

              current = new ForenoonState();

}

 

客户端:

int main()

{

       Work* mywork=new Work();

       mywork->hour=9;

       mywork->Writeprogram();

       mywork->hour = 14;

       mywork->Writeprogram();

       return 0;

}

适配器模式

   GOOD:双方都不适合修改的时候,可以考虑使用适配器模式

例:

#include<iostream>

using namespacestd;

 

class Target

{

public:

       virtual void Request()

       {

              cout<<"普通的请求"<<endl;

       }

};

 

class Adaptee

{

public:

       void SpecificalRequest()

       {

              cout<<"特殊请求"<<endl;

       }

};

 

class Adapter :public Target

{

private:

       Adaptee* ada;

public:

       virtual voidRequest()

       {

              ada->SpecificalRequest();

              Target::Request();

       }

       Adapter()

       {

              ada=newAdaptee();

       }

       ~Adapter()

       {

              deleteada;

       }

};

 

客户端:

int main()

{

       Adapter * ada=new Adapter();

       ada->Request();

       delete ada;

       return 0;

}

 

例二

#include<iostream>

#include<string>

using namespacestd;

 

class Player

{

protected:

       string name;

public:

       Player(string strName) { name = strName;}

       virtual void Attack()=0;

       virtual void Defense()=0;

};

 

class Forwards :public Player

{

public:

       Forwards(stringstrName):Player(strName){}

public:

       virtual void Attack()

       {

              cout<<name<<"前锋进攻"<<endl;

       }

       virtual void Defense()

       {

              cout<<name<<"前锋防守"<<endl;

       }

};

 

class Center :public Player

{

public:

       Center(string strName):Player(strName){}

public:

       virtual void Attack()

       {

              cout<<name<<"中场进攻"<<endl;

       }

       virtual void Defense()

       {

          cout<<name<<"中场防守"<<endl;

       }

};

 

//为中场翻译

class TransLater: public Player

{

private:

       Center *player;

public:

       TransLater(stringstrName):Player(strName)

       {

              player =new Center(strName);

       }

       virtual voidAttack()

       {

              player->Attack();

       }

       virtual voidDefense()

       {

              player->Defense();

       }

};

 

 

客户端

int main()

{

       Player *p=new TransLater("小李");

       p->Attack();

       return 0;

}

备忘录模式

   GOOD:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以将以后的对象状态恢复到先前保存的状态。

适用于功能比较复杂的,但需要记录或维护属性历史的类或者需要保存的属性只是众多属性中的一小部分时Originator可以根据保存的Memo还原到前一状态。

例:

#include<iostream>

#include<string>

using namespacestd;

 

class Memo;

 

//发起人类

class Originator

{

public:

       string state;

    Memo* CreateMemo();

       void SetMemo(Memo* memo);

       void Show()

       {

              cout<<"状态:"<<state<<endl;

       }

};

//备忘录类

class Memo

{

public:

       string state;

       Memo(string strState)

       {

              state= strState;

       }

};

 

Memo*Originator::CreateMemo()

{

       return new Memo(state);

}

 

voidOriginator::SetMemo(Memo* memo)

{

       state = memo->state;

}

 

//管理者类

class Caretaker

{

public:

       Memo* memo;

};

 

客户端:

int main()

{

       Originator* on=new Originator();

       on->state = "on";

       on->Show();

 

       Caretaker* c= new Caretaker();

       c->memo = on->CreateMemo();

 

       on->state = "off";

       on->Show();

 

       on->SetMemo(c->memo);

       on->Show();

       return 0;

}

组合模式

   GOOD:整体和部分可以被一致对待(如WORD中复制一个文字、一段文字、一篇文章都是一样的操作)

例:

#include<iostream>

#include<string>

#include<vector>

using namespacestd;

 

class Component

{

public:

       string m_strName;

       Component(string strName)

       {

              m_strName = strName;

       }

       virtual void Add(Component* com)=0;

       virtual void Display(int nDepth)=0;

};

 

class Leaf :public Component

{

public:

       Leaf(string strName):Component(strName){}

 

       virtual void Add(Component* com)

       {

              cout<<"leaf can‘tadd"<<endl;

       }

 

       virtual void Display(int nDepth)

       {

              string strtemp;

              for(int i=0; i < nDepth; i++)

              {

                     strtemp+="-";

              }

              strtemp += m_strName;

              cout<<strtemp<<endl;

       }

};

 

class Composite: public Component

{

private:

       vector<Component*> m_component;

public:

       Composite(string strName) : Component(strName){}

 

       virtual void Add(Component* com)

       {

              m_component.push_back(com);

       }

 

       virtual void Display(int nDepth)

       {

              string strtemp;

              for(int i=0; i < nDepth; i++)

              {

                     strtemp+="-";

              }

              strtemp += m_strName;

              cout<<strtemp<<endl;

 

              vector<Component*>::iteratorp=m_component.begin();

              while (p!=m_component.end())

              {

                     (*p)->Display(nDepth+2);

                     p++;

              }

       }

 

};

 

//客户端

#include"Model.h"

 

int main()

{

       Composite* p=new Composite("小王");

       p->Add(new Leaf("小李"));

       p->Add(new Leaf("小赵"));

 

       Composite* p1 = new Composite("小小五");

       p1->Add(new Leaf("大三"));

 

       p->Add(p1);

       p->Display(1);

       return 0;

}

例二

#include<iostream>

#include<string>

#include<vector>

using namespacestd;

 

class Company

{

protected:

       string m_strName;

public:

       Company(string strName)

       {

              m_strName = strName;

       }

 

       virtual void Add(Company* c)=0;

       virtual void Display(int nDepth)=0;

       virtual void LineOfDuty()=0;

};

 

classConcreteCompany: public Company

{

private:

       vector<Company*> m_company;

public:

       ConcreteCompany(string strName):Company(strName){}

 

       virtual void Add(Company* c)

       {

              m_company.push_back(c);

       }

       virtual void Display(int nDepth)

       {

              string strtemp;

              for(int i=0; i < nDepth; i++)

              {

                     strtemp += "-";

              }

              strtemp +=m_strName;

              cout<<strtemp<<endl;

 

              vector<Company*>::iteratorp=m_company.begin();

              while (p!=m_company.end())

              {

                     (*p)->Display(nDepth+2);

                     p++;

              }

       }

       virtual void LineOfDuty()

       {

              vector<Company*>::iteratorp=m_company.begin();

              while (p!=m_company.end())

              {

                     (*p)->LineOfDuty();

                     p++;

              }

       }

};

 

classHrDepartment : public Company

{

public:

 

       HrDepartment(string strname) :Company(strname){}

 

       virtual void Display(int nDepth)

       {

              string strtemp;

              for(int i = 0; i < nDepth; i++)

              {

                     strtemp += "-";

              }

 

              strtemp += m_strName;

              cout<<strtemp<<endl;

       }

       virtual void Add(Company* c)

       {

              cout<<"error"<<endl;

       }

 

       virtual void LineOfDuty()

       {

              cout<<m_strName<<":招聘人才"<<endl;

       }

};

 

//客户端:

int main()

{

       ConcreteCompany *p = newConcreteCompany("清华大学");

       p->Add(new HrDepartment("清华大学人才部"));

 

       ConcreteCompany *p1 = newConcreteCompany("数学系");

       p1->Add(new HrDepartment("数学系人才部"));

 

       ConcreteCompany *p2 = newConcreteCompany("物理系");

       p2->Add(new HrDepartment("物理系人才部"));

 

       p->Add(p1);

       p->Add(p2);

 

       p->Display(1);

       p->LineOfDuty();

       return 0;

}



设计模式 (三)

原文:http://blog.csdn.net/fengyelengfeng/article/details/40864925

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!