设计模式 (三)

抽象工厂模式

   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;

}

时间: 2024-08-08 09:35:33

设计模式 (三)的相关文章

Php设计模式(三):行为型模式part1

原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=5 可以在线运行查看效果哦! 在上一篇我们讲了结构型模式,结构型模式是讨论类和对象的结构的.总共有7种.而今天我们来介绍一下行为型模式. 一.什么是行为型模式? 行为型模式: 就是描述类和对象之间的通信和职责的.简而言之,就是类和对象扮演什么角色,还有怎么扮演这个角色的问题. 二.行为型模式的种类 大体上分为三个大类:常见模式.已知模式.深度模式 常见模式包括: 模版方法模式.命令模式.迭代器模式.观

Php设计模式(三):行为型模式part2

原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=5 可以在线运行查看效果哦! <接上文> 5.中介者模式(Mediator) : 用中介对象封装一系列的对象交互,中介使各对象不需要显式地相互引用.类似于邮局,邮寄者和收件者不用自己跑很远路,通过邮局就可以. 好处:简化了对象之间的关系,减少子类的生成. 弊端:中介对象可能变得非常复杂,系统难以维护. 应用场景:不需要显示地建立交互. 代码实现: ? 1 2 3 4 5 6 7 8 9 10

设计模式三—抽象工厂模式

设计模式三-抽象工厂模式 一.定义 抽象工厂模式是工厂方法模式的进一步抽象.如果产品簇中只有一种产品,则退化为工厂方法模式. 二.原理图 三.代码实例 * 苹果和土豆是园丁1的杰作 * 葡萄和西红柿是园丁2的杰作 1.Fruit.java public interface Fruit { /* * 生长 * 收获 * 栽种 */ public void grow(); public void harvest(); public void plant(); } 2.Apple.java publi

C#中的异步调用及异步设计模式(三)——基于事件的异步模式

四.基于事件的异步模式(设计层面) 基于事件的C#异步编程模式是比IAsyncResult模式更高级的一种异步编程模式,也被用在更多的场合.该异步模式具有以下优点: ·                  “在后台”执行耗时任务(例如下载和数据库操作),但不会中断您的应用程序. ·                  同时执行多个操作,每个操作完成时都会接到通知(在通知中可以区分是完成了哪个操作). ·                  等待资源变得可用,但不会停止(“挂起”)您的应用程序. ·  

设计模式三: 代理模式(Proxy) -- JDK的实现方式

设计模式三: 代理模式(Proxy) -- JDK的实现方式 简介 代理模式属于行为型模式的一种, 控制对其他对象的访问, 起到中介作用. 代理模式核心角色: 真实角色,代理角色; 按实现方式不同分为静态代理和动态代理两种; 意图 控制对其它对象的访问. 类图 实现 JDK自带了Proxy的实现, 下面我们先使用JDK的API来演示代理如何使用, 随后再探究Proxy的实现原理,并自己来实现Proxy. JDK代理类的使用: (InvocationHandler,Proxy) 使用JDK实现的代

设计模式之总结篇(设计模式六大原则以及设计模式三种类型)

经过这半年的学习,自己对设计模式这门课程也有了一定的认知.前面也发了关于23个设计模式的博客.现在对这些设计模式进行一个整体的总结. 首先,设计模式有六大原则: 原则一.单一职责原则 定义:There should never be more than one reason for a class to change(不要存在多于一个导致类变更的原因).通俗的说,即一个类只负责一项职责. 好处: 1.降低类的复杂度. 2.提高可读性. 3.提高可维护性. 4.变更引起的风险降低. 原则二.里氏替

设计模式三(转载)

本章是关于设计模式的最后一讲,会讲到第三种设计模式——行为型模式,共11种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式. 命令模式.备忘录模式.状态模式.访问者模式.中介者模式.解释器模式.这段时间一直在写关于设计模式的东西,终于写到一半了,写博文是个很费时间的东 西,因为我得为读者负责,不论是图还是代码还是表述,都希望能尽量写清楚,以便读者理解,我想不论是我还是读者,都希望看到高质量的博文出来,从我本人出 发,我会一直坚持下去,不断更新,源源动力来自于读者朋友们的不断支持,我会

【设计模式(三)】单例模式——单例有几种写法?

有一回对我说道,“你写过编程么?”我略略点一点头.他说,“写过,……我便考你一考.单例模式,是怎样写的?”我想,讨饭一样的人,也配考我么?便回过脸去,不再理会.孔乙己等了许久,很恳切的说道,“不能写罢?……我教给你,记着! 单例模式(Singleton Pattern) 单例模式是 Java 中最简单的设计模式之一.这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建.这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象. 单例模式三要素:1.单

设计模式三:工厂方法模式

疑惑解答: 1.interface接口不能够实例化(new),但是可以定义引用来指向继承该接口的实体类: 如: interface animal{ void eat(); } class Cat implements animal{ public void eat(){ System.out.println("The cat eat!"); } } animal an = new Cat(); 一.什么是工厂方法模式 1.是简单工厂模式的进一步抽象: 2.定义一个创建产品对象的工厂接口

和我一起学设计模式(三)

观察者模式,又称订阅者/发布者模式.这个模式对于减少模块之间的偶合还是非常有用的.为什么这么说,是因为被观察者不必事先知道有多少个模块对它有依赖,扩展和修改都非常自由.许多设计模式的书上对这个模式都写的过于神秘,以致于许多新手不愿意去学习它,应用它. 我从百度上搜索"观察者模式“: 观察者模式(有时又被称为发布-订阅Subscribe>模式.模型-视图View>模式.源-收听者Listener>模式或从属者模式)是软件设计模式的一种. 在此种模式中,一个目标物件管理所有相依于它