设计模式之策略模式、代理模式、装饰模式、工厂模式
一、策略模式
定义:定义一组算法,将每个算法都封装起来,并且使它们之间可以互换
类图关系:
代码:在C/S的射击类游戏中,同一个角色,拿起不同的武器,会有不同的行为。武器可以随意的切换。
#include <iostream>
using namespace std;
class Weapon
{
public:
virtual void use() = 0;
};
class Knife :public Weapon
{
public:
void use()
{
cout<<" 大刀砍你"<<endl;
}
};
class Gun :public Weapon
{
public:
void use()
{
cout<<" 机枪突突你"<<endl;
}
};
class CCsprite
{
public:
CCsprite(Weapon *k)
{
_w = k;
}
void changWeapon(Weapon *w)
{
_w = w;
}
void faighting()
{
_w->use();
}
protected:
Weapon *_w;
};
int main()
{
Knife k;
CCsprite character(&k);
character.faighting();
Gun g;
character.changWeapon(&g);
character.faighting();
character.changWeapon(&k);
character.faighting();
return 0;
}
二、代理模式
定义:为其他对象提供一种代理以控制对这个对象的访问
类图关系:
代码:当一个对象加载过程,很慢,但又对当前的展示造成影响,此时就需要用代理的方式来
解决。
#include <iostream>
#include <unistd.h>
using namespace std;
class Text
{
public:
void showText()
{
cout<<"好诗好诗好诗好 诗好诗 好诗好诗 好诗 "
"好诗好诗 好诗好诗 好诗好诗 好诗好诗 "
"好诗好诗 好诗好诗 好诗好诗 好诗好诗 "
"好诗好诗 好诗好诗 好诗好诗 好诗好诗 "
"好诗好诗 好诗好诗 好诗好诗 好诗好诗 "
"好诗好诗 好诗好诗 好诗好诗 好诗好诗 "<<endl;
}
};
class ImageSubject
{
public:
virtual void showPicture() = 0;
};
//己经写就的类
class LargeImage:public ImageSubject
{
public:
LargeImage()
{
sleep(6);
cout<<"我终于加载完了图片,你可以显示了"<<endl;
}
virtual void showPicture()
{
cout<<"清明上河图长卷"<<endl;
}
};
class ProxyLargeImage:public ImageSubject
{
public:
ProxyLargeImage():li(NULL){}
void showPicture()
{
if(li == NULL)
li = new LargeImage;
li->showPicture();
}
protected:
LargeImage *li;
};
class Document
{
public:
Document()
{
_t = new Text; //1
_i = new ProxyLargeImage; //1000
}
void print()
{
_t->showText();
_i->showPicture();
_t->showText();
}
Text *_t;
ProxyLargeImage *_i;
};
int main(int argc, char *argv[])
{
Document doc;
doc.print(); // 一分部加载文件字,一部分加载图片
return 0;
}
三、装饰模式
定义:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。
优先使用组合,而不是继承
类图关系:
代码:在我们生活中去买手机,经常会有套餐的例子,比如,我们买一部手机祼机多少钱,外加一个耳机,构成的套餐价,多少钱。外加一个保护膜,构成的另外套餐价多少钱,组合套餐呢?
#include <iostream>
using namespace std;
class Phone
{
public:
virtual float cost() = 0;
};
class IPhone :public Phone
{
public:
IPhone():_iprice(5000){}
float cost()
{
return _iprice;
}
private:
float _iprice;
};
class NoKiaPhone:public Phone
{
};
class Decorator:public IPhone
{
public:
Decorator(Phone *phone):_phone(phone){}
Phone * _phone;
};
class ScreenProtected: public Decorator
{
public:
ScreenProtected(Phone *phone):Decorator(phone),_spprice(300){}
float cost()
{
return _phone->cost() + _spprice;
}
private:
float _spprice;
};
class HeadSet:public Decorator
{
public:
HeadSet(Phone *phone):Decorator(phone),_hsprice(500){}
float cost()
{
return _phone->cost() + _hsprice;
}
private:
float _hsprice;
};
int main()
{
IPhone iphone;
cout<<iphone.cost()<<endl;
ScreenProtected sp(&iphone);
cout<<sp.cost()<<endl;
HeadSet hs(&sp);
cout<<hs.cost()<<endl;
Phone *p = new ScreenProtected(new HeadSet(new IPhone));
cout<<p->cost()<<endl;
return 0;
}
四、简单工厂
定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类。
类图定义:
#include <iostream>
using namespace std;
enum CoreType
{
COREA,COREB,COREC
};
class SingleCore
{
public:
virtual void work() = 0;
};
class SingleCoreA:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreA"<<endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreB"<<endl;
}
};
class SingleCoreC:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreC"<<endl;
}
};
class Factory
{
public:
SingleCore * createSingleCore(enum CoreType type)
{
if(type == COREA)
return new SingleCoreA;
if(type == COREB)
return new SingleCoreB;
if(type == COREC)
return new SingleCoreC;
}
};
int main()
{
Factory fac;
SingleCore* pa = fac.createSingleCore(COREA);
pa->work();
SingleCore* pb = fac.createSingleCore(COREB);
pb->work();
SingleCore* pc = fac.createSingleCore(COREC);
pc->work();
delete pa; delete pb; delete pc;
return 0;
}
五、工厂模式
#include <iostream>
using namespace std;
enum CoreType
{
COREA,COREB,COREC
};
class SingleCore
{
public:
virtual void work() = 0;
};
class SingleCoreA:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreA"<<endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreB"<<endl;
}
};
class SingleCoreC:public SingleCore
{
public:
void work()
{
cout<<"SingleCoreC"<<endl;
}
};
class Factory
{
public:
virtual SingleCore * createSingleCore() = 0;
};
class FactoryCA:public Factory
{
public:
SingleCore * createSingleCore()
{
return new SingleCoreA;
}
};
class FactoryCB:public Factory
{
public:
SingleCore * createSingleCore()
{
return new SingleCoreB;
}
};
class FactoryCC:public Factory
{
public:
SingleCore * createSingleCore()
{
return new SingleCoreC;
}
};
//加入内存管理
int main(void)
{
Factory* fca = new FactoryCA();
fca->createSingleCore()->work();
Factory* fcb = new FactoryCB();
fcb->createSingleCore()->work();
return 0;
}
迷失的人迷失了,相逢的人会再相逢