Gof 设计模式 结构型

来源:互联网 发布:php 字符串true false 编辑:程序博客网 时间:2024/06/06 09:01
#include<iostream>using namespace std;#if 0//                桥接模式//桥接模式是软件设计模式中最复杂的模式之一,//它把事物对象和其具体行为、具体特征分离开来,使它们可以各自独立的变化。//事物对象仅是一个抽象的概念。如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、//“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。class AImpshape{public:AImpshape(){}virtual ~AImpshape(){}virtual void draw() = 0;};class Paint     {public:Paint(){}virtual ~Paint(){}virtual void painting() = 0;public:AImpshape *impshape; //关联一种颜色};class paintcircle :public Paint{public:void painting(){cout << "painting circle" << endl;this->impshape->draw();}};class paintrectangle :public Paint{public:void painting(){this->impshape->draw();}};class AImpshapeRed :public AImpshape{void draw(){cout << "Red painting..." << endl;}};class AImpshapeYellow :public AImpshape{void draw(){ cout << "Yellow painting..." << endl; }};int main(){Paint *pat = new paintcircle();pat->impshape = new AImpshapeRed();pat->painting();pat->impshape = new AImpshapeYellow();pat->painting();return 0;}#endif#if 0//              适配器模式//在设计模式中,适配器模式(英语:adapter pattern)有时候也称包装样式或者包装(wrapper)。//将一个类的接口转接成用户所期待的。一个适配使得因接口不兼容而不能在一起工作的类工作在一起,//做法是将类自己的接口包裹在一个已存在的类中。//Adapter模式主要应用于“希望复用一些现存的类,但是接口又与复用环境要求不一致的情况”,在遗留代码复用、类库迁移等方面非常有用。//Adapter模式有对象适配器和类适配器两种形式的实现结构,但是类适配器采用“多继承”的实现方式,带来了不良的高耦合,所以一般不推荐使用。对象适配器采用“对象组合”的方式,更符合松耦合精神。//Adapter模式的实现可以非常的灵活,不必拘泥于GOF23中定义的两种结构。例如,完全可以将Adapter模式中的“现存对象”作为新的接口方法参数,来达到适配的目的。//Adapter模式本身要求我们尽可能地使用“面向接口的编程”风格,这样才能在后期很方便的适配。     /*类模型*/class Target{public:Target(){}virtual ~Target(){}virtual void Request(){}};class Adaptee{public:Adaptee(){}~Adaptee(){}void SpecificRequest(){}};class Adapter :public Target, public Adaptee{public:Adapter(){}~Adapter(){}void Request(){this->SpecificRequest();}};int main(){//Adapter *adp = new Adapter();Target* adp = new Adapter();adp->Request();return 0;}     /*对象模型*/class Target{public:Target(){}virtual ~Target(){}virtual void Request(){}};class Adaptee{public:Adaptee(){}~Adaptee(){}void SpecificRequest(){}};class Adapter :public Target{public:Adapter(){ adaptor = new Adaptee(); }~Adapter(){}void Request(){adaptor->SpecificRequest();}private:Adaptee *adaptor;};int main(){Target *adp = new Adapter();adp->Request();return 0;}#endif//             外观模式//外观模式(Facade pattern),是软件工程中常用的一种软件设计模式,//它为子系统中的一组接口提供一个统一的高层接口,使得子系统更容易使用。#if 0class A{public:void testA(){}};class B{public:void testB(){}};class C{public:void testC(){}};class Facade{public:Facade(){a = new A();b = new B();c = new C();}void run(){a->testA();b->testB();c->testC();}private:A *a;B *b;C *c;};int main(){Facade *fac = new Facade();fac->run();return 0;}#endif //           组合模式//组合模式(composite pattern)将对象组合成树形结构以表示“部分-整体”的层次结构。//它使得客户对单个对象和复合对象的使用具有一致性。//这种形式涉及到三个角色://抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象。在安全式的合成模式里,构件角色并不是定义出管理子对象的方法,这一定义由树枝构件对象给出。//树叶构件(Leaf)角色:树叶对象是没有下级子对象的对象,定义出参加组合的原始对象的行为。//树枝构件(Composite)角色:代表参加组合的有下级子对象的对象。树枝对象给出所有的管理子对象的方法,如add()、remove()、getChild()等。#if 0class Component{public:Component(){}virtual ~Component(){}public:virtual void Operation() = 0;virtual void Add(const Component&){}virtual void Remove(const Component&){}virtual Component* GetChild(int){ return NULL; }};class Composite :public Component{public:Composite(){}~Composite(){}public:void Operation(){vector<Component*>::iterator comIter = comVec.begin();for (; comIter != comVec.end(); comIter++){(*comIter)->Operation();}}void Add(Component* com){comVec.push_back(com);}void Remove(Component* com){vector<Component*>::const_iterator it = comVec.begin();for (; it != comVec.end();){if (*it == com){ break; }}comVec.erase(it);}Component* GetChild(int index){return comVec[index];}private:vector<Component*> comVec;};class Leaf :public Component{public:Leaf(){}~Leaf(){}void Operation(){cout << "Leaf operation....." << endl;}};int main(int argc, char* argv[]){Leaf* l = new Leaf();l->Operation();Composite* com = new Composite();com->Add(l);com->Operation();Component* ll = com->GetChild(0);ll->Operation();return 0;}#endif//                   修饰模式//修饰模式(Decorato parttern),是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式。//就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能。#if 0class phone{public:virtual ~phone(){}virtual void Operation(){}protected:phone(){}};class Iphone :public phone{public:Iphone(){}~Iphone(){}void Operation(){}};class phoneshell :public phone{public:phoneshell(phone* com):_com(com){}virtual ~phoneshell(){}void Operation(){}protected:phone* _com;};class redphoneshell :public phoneshell{public:redphoneshell(phone* com):phoneshell(com){}~redphoneshell(){}void Operation(){AddedBehavior();}void AddedBehavior(){cout << "asdfghjklqwertyuiopzxcvbnm" << endl;}};int main(int argc, char* argv[]){phone *ph = new Iphone();phoneshell *ps = new redphoneshell(ph);ps->Operation();return 0;}#endif//          代理模式//代理模式(Proxy Pattern)是程序设计中的一种设计模式。所谓的代理者是指一个类可以作为其它东西的接口。//代理者可以作任何东西的接口:网络连接、内存中的大对象、文件或其它昂贵或无法复制的资源。//著名的代理模式例子为引用计数(英语:reference counting)指针对象。(智能指针)//当一个复杂对象的多份副本须存在时,代理模式可以结合享元模式以减少内存用量。//典型作法是创建一个复杂对象及多个代理者,每个代理者会引用到原本的复杂对象。//而作用在代理者的运算会转送到原本对象。一旦所有的代理者都不存在时,复杂对象会被移除。#if 0#include<list>#include<map>class refcount{public:refcount():count(0){}void addref(){ count++; }int subref(){ return --count; }void resetref(){ count = 0; }private:int count;};template<class Ty>class smartptr{public:smartptr():Data(NULL){}smartptr(Ty* mdata) :Data(mdata){Refmap.insert(pair<Ty *,refcount>(Data,refcount()));map<Ty *, refcount>::iterator it = Refmap.find(Data);(it->second).addref();}smartptr(const smartptr & mdata){Data = mdata.Data;map<Ty *, refcount>::iterator it = Refmap.find(Data);(it->second).addref();}smartptr &operator=(smartptr &mdata){if (this != &mdata){Data = mdata.Data;map<Ty *, refcount>::iterator it = Refmap.find(Data);(it->second).addref();}return *this;}~smartptr(){map<Ty *, refcount>::iterator it = Refmap.find(Data);if ((it->second).subref()==0){delete Data;Refmap.erase(it);}}private:Ty* Data;static map<Ty *,refcount> Refmap;};template<class Ty>map<Ty *, refcount> smartptr<Ty>::Refmap;int main(){smartptr<int> smt = new int[10];//smt 代理这块内存smartptr<int> smp = smt;return 0;}#endif//                享元模式//享元模式(英语:Flyweight Pattern)是一种软件设计模式。//它使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件;//它适合用于当大量物件只是重复因而导致无法令人接受的使用大量内存。通常物件中的部分状态是可以分享。//常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。#include<string>#include<vector>class Flyweight{public:virtual ~Flyweight();virtual void Operation(const string& extrinsicState);string GetIntrinsicState();protected:Flyweight(string intrinsicState);private:string _intrinsicState;};class ConcreteFlyweight :public Flyweight{public:ConcreteFlyweight(string intrinsicState);~ConcreteFlyweight();void Operation(const string& extrinsicState);};class FlyweightFactory{public:FlyweightFactory();~FlyweightFactory();Flyweight* GetFlyweight(const string& key);private:vector<Flyweight*> _fly;};int main(int argc, char* argv[]){FlyweightFactory* fc = new FlyweightFactory();Flyweight* fw1 = fc->GetFlyweight("hello");Flyweight* fw2 = fc->GetFlyweight("world!");Flyweight* fw3 = fc->GetFlyweight("hello");return 0;}//Flyweight 模式中有一个类似 Factory 模式的对象构造工厂//FlyweightFactory,当客户程序员( Client)需要一个对象时候就会向 FlyweightFactory 发出//请求对象的消息 GetFlyweight()消息, FlyweightFactory 拥有一个管理、存储对象的“仓//库”(或者叫对象池, vector 实现), GetFlyweight()消息会遍历对象池中的对象,如果已//经存在则直接返回给 Client,否则创建一个新的对象返回给 Client。

0 0
原创粉丝点击