Decorator模式:
在面向对象的设计和开发过程中,可能会经常遇到以下的情况;我们需要为一个已经定义好的类添加新的职责,通过情况我们会给定义一个新类继承自定义好的类,通过继承的方式解决这样的情况会带来系统的复杂性,因为继承的深度会变得很深。而Decorator提供了一种给类增加职责的方法,不是通过继承实现,而是通过组合。
Decorator装饰模式是一种结构型模式,它主要是解决:”过度地使用了继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀(多继承)。继承为类型引入的静态特质的意思是说以继承的方式使某一类型要获得功能是在编译时。所谓静态,是指在编译时;动态,是指在运行时。
举例:
#pragma once
#ifndef _DECORATOR_H
#define _DECORATOR_H
class Component
{
public:
virtual ~Component();
virtual void Operation();
protected:
Component();
private:
};
class ConcreteComponet:public Component
{
public:
ConcreteComponet();
~ConcreteComponet();
void Operation();
protected:
private:
};
class DecoratorClass:public Component
{
public:
DecoratorClass(Component *com);
virtual ~DecoratorClass(void);
void Operation();
protected:
Component *_com;
private:
};
class ConcreteDecorator:public DecoratorClass
{
public:
ConcreteDecorator(Component *com);
~ConcreteDecorator();
void Operation();
void AddedBehavior();
protected:
private:
};
#endif
//
#include "StdAfx.h"
#include "DecoratorClass.h"
#include <iostream>
using namespace std;
Component::Component()
{
cout<<"component construct..."<<endl;
}
Component::~Component()
{
cout<<"component exit..."<<endl;
}
void Component::Operation()
{
cout<<"component operation..."<<endl;
}
ConcreteComponet::ConcreteComponet()
{
cout<<"concrete component construct..."<<endl;
}
ConcreteComponet::~ConcreteComponet()
{
cout<<"concrete component exit..."<<endl;
}
void ConcreteComponet::Operation()
{
cout<<"concreteComponet operation.."<<endl;
}
DecoratorClass::DecoratorClass(Component *com)
{
this->_com=com;
}
DecoratorClass::~DecoratorClass(void)
{
delete _com;
}
void DecoratorClass::Operation()
{
}
ConcreteDecorator::ConcreteDecorator(Component *com):DecoratorClass(com)
{
}
ConcreteDecorator::~ConcreteDecorator()
{
}
void ConcreteDecorator::AddedBehavior()
{
cout<<"concreteDecorator::AddedBehacior..."<<endl;
}
void ConcreteDecorator::Operation()
{
_com->Operation();
this->AddedBehavior();
}
/
// Decorator.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "DecoratorClass.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Component *com=new ConcreteComponet();
DecoratorClass *dec=new ConcreteDecorator(com);
dec->Operation();
delete dec;
return 0;
}
composite模式:
Composite组合模式主要是应对这样的问题:一类具有“容器特征”的对象——即他们在充当对象的同时,又是其他对象的容器的情况。在编写时我们常常会造成:客户代码过多地依赖于对象容器复杂的内部实现,对象容器内部实现结构(而非抽象接口)的变化将引起客户代码的频繁变化,带来了代码的维护性、扩展性的弊端。
GoF《设计模式》中说到:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得客户对单个对象和组合对象的使用具有一致性。
Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上。比如说C和A不在一个服务器上,A要频繁的调用C,我们可以在A上做一个代理类Proxy,把访问C的工作交给Proxy,这样对于A来说,就好像在直接访问C的对象。在对A的开发中我们可以把注意力完全放在业务的实现上。
举例如下:
#pragma once
#ifndef _PROXY_H
#define _PROXY_H
class Subject
{
public:
virtual ~Subject();
virtual void Request()=0;
protected:
Subject();
private:
};
class ConcreteSubject:public Subject
{
public:
ConcreteSubject();
~ConcreteSubject();
void Request();
protected:
private:
};
class ProxyClass
{
public:
ProxyClass(void);
ProxyClass(Subject *sub);
~ProxyClass(void);
void Request();
protected:
private:
Subject *_sub;
};
#endif
//
#include "StdAfx.h"
#include "ProxyClass.h"
#include <iostream>
using namespace std;
Subject::Subject()
{
}
Subject::~Subject()
{
}
ConcreteSubject::ConcreteSubject()
{
}
ConcreteSubject::~ConcreteSubject()
{
}
void ConcreteSubject::Request()
{
cout<<"concrete subject ..request"<<endl;
}
ProxyClass::ProxyClass(void)
{
}
ProxyClass::ProxyClass(Subject *sub)
{
_sub=sub;
}
ProxyClass::~ProxyClass(void)
{
delete _sub;
}
void ProxyClass::Request()
{
cout<<"proxy request..."<<endl;
_sub->Request();
}
#include "stdafx.h"
#include "ProxyClass.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Subject *sub=new ConcreteSubject();
ProxyClass *p=new ProxyClass(sub);
p->Request();
return 0;
}