天天看点

大话设计模式(4)装饰模式

一、场景及UML图

场景:又称修饰模式,就是对类起修饰作用。如类“人”,用类“衣服”来修饰。

大话设计模式(4)装饰模式

关键词:Decorator()函数/setComponent()函数。

二、概念

装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

三、说明

角色:

(1)Component是定义一个对象,可以给这些对象动态地添加职责。

(2)ConcreteComponent是定义了一个具体的对象,也可以给这个对象增加一些职责。

(3)Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但是对于Component来说,是无需知道Decorator的存在的。

(4)至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的作用。

什么时候用:

(1)需要在内部组装完成再显示出来的情况。

(2)类似于建造者模式,但是建造者模式的要求见到的过程必须是稳定的,而装饰模式的建造过程是不稳定的。

(3)我们需要把所需的功能按正确的顺序串联起来进行控制。

优点:

(1)把类的装饰功能从类中搬移去除,这样可以简化原有的类。

(2)有效地把类的核心职责和装饰功能区分开来,而且可以去除相关类中重复的装饰逻辑。

和建造者模式的区别?

建造者模式要求建造的过程必须是稳定的,而装饰模式的建造过程是不稳定的,可以有各种各样的组合方式。

四、实现

(1)Conponent即ConcreteComponent类:此处为Person类

   此处没有单独设立被装饰对象类

#ifndef PERSON_H
#define PERSON_H
 
#include <string>
#include <iostream>
//ConcreteComponent即Component
class Person
{
private:
	std::string name;
public:
	Person(){};
	Person(std::string name)
	{
		this->name=name;
	}
	virtual void Show()
	{
		std::cout<<"装饰的"<<name<<std::endl;
	}
};
 
#endif
           

(2)Decorator及ConcreteDecorator:此处为Finery及其子类

#ifndef FINERY_H
#define FINERY_H
 
#include <iostream>
#include "Person.h"
 
//Decorator类
class Finery:public Person
{
protected:
	Person* component;
public:
	void Decorator(Person* component)
	{
		this->component=component;
	}
	void Show()
	{
		if(component!=NULL)
			component->Show();
	}
};
 
//下面是一系列ConcreteDecorator类
class TShirts:public Finery
{
public:
	void Show()
	{
		std::cout<<"大T恤  ";
		Finery::Show();
	}
 
};
 
//ConcreteDecorator类
class BigTrouser:public Finery
{
public:
	void Show()
	{
		std::cout<<"垮裤  ";
		Finery::Show();
	}
 
};
 
//ConcreteDecorator类
class Sneakers:public Finery
{
public:
	void Show()
	{
		std::cout<<"破球鞋  ";
		Finery::Show();
	}
 
};
 
//ConcreteDecorator类
class Suit:public Finery
{
public:
	void Show()
	{
		std::cout<<"西装  ";
		Finery::Show();
	}
 
};
 
//ConcreteDecorator类
class Tie:public Finery
{
public:
	void Show()
	{
		std::cout<<"领带  ";
		Finery::Show();
	}
 
};
 
//ConcreteDecorator类
class LeatherShoes:public Finery
{
public:
	void Show()
	{
		std::cout<<"皮鞋  ";
		Finery::Show();
	}
	 
};
 
 
#endif
           

(3)客户端:main

#include "Finery.h"
#include <string>
#include <iostream>
 
//客户端
void main()
{
	Person* xc=NULL;
	xc=new Person("小菜");
 
	std::cout<<"第一种装扮:"<<std::endl;
	
	Sneakers* pqx=NULL;
	pqx=new Sneakers();
	BigTrouser* kk=NULL;
	kk=new BigTrouser();
	TShirts* dtx=NULL;
	dtx=new TShirts();
 
	pqx->Decorator(xc);
	kk->Decorator(pqx);
	dtx->Decorator(kk);
 
	dtx->Show();
 
	std::cout<<"第二种装扮:"<<std::endl;
 
	LeatherShoes* px=NULL;
	px=new LeatherShoes();
	Tie* ld=NULL;
	ld=new Tie();
	Suit* xz=NULL;
	xz=new Suit();
 
	px->Decorator(xc);
	ld->Decorator(px);
	xz->Decorator(ld);
 
	xz->Show();
 
	if(xc!=NULL)
	{
		delete xc;
		xc=NULL;
	}
	if(pqx!=NULL)
	{
		delete pqx;
		pqx=NULL;
	}
	if(kk!=NULL)
	{
		delete kk;
		kk=NULL;
	}
	if(dtx!=NULL)
	{
		delete dtx;
		dtx=NULL;
	}
	if(px!=NULL)
	{
		delete px;
		px=NULL;
	}
	if(ld!=NULL)
	{
		delete ld;
		ld=NULL;
	}
	if(xz!=NULL)
	{
		delete xz;
		xz=NULL;
	}
 
	system("pause");
}
           

五、输出

大话设计模式(4)装饰模式

继续阅读