设计模式c++实现:装饰(decorator)模式(代码片段)

skyman_2001 skyman_2001     2022-12-11     171

关键词:

转载请注明,来自:http://blog.csdn.net/skyman_2001

class Component

public:
	Component() 
	virtual ~Component() 

	virtual void operation() = 0;
	virtual Component *remove() = 0;
;

class ConcreteComponent: public Component

public:
	ConcreteComponent() 
	~ConcreteComponent() 

	void operation()
	
		std::cout <<"Concrete component operation"<< std::endl;
	

	Component *remove()
	
		return this;
	
;
class Decorator : public Component

public:
	Decorator(Component *comp) : m_component(comp) 
	virtual ~Decorator() 
		 
	virtual void operation()
	
		m_component->operation();
	

	Component *remove()
	
		return m_component;
	
		 
private:
	Component *m_component;
;

class Decor1: public Decorator

public:
	Decor1(Component *comp) : Decorator(comp)
	virtual ~Decor1() 

	virtual void operation()
	
		Decorator::operation();
		addedBehavior();
	

private:	
	void addedBehavior()
	
		std::cout << "Decor1 addedBehavior" << std::endl;
	
;

class Decor2: public Decorator

public:
	Decor2(Component *comp) : Decorator(comp) 
	virtual ~Decor2() 

	virtual void operation()
	
		Decorator::operation();
		addedBehavior();
	

private:	
	void addedBehavior()
	
		std::cout << "Decor2 addedBehavior" << std::endl;
	
;

int _tmain(int argc, _TCHAR* argv[])

	Component* concrete = new ConcreteComponent();
	Component* decored1 = new Decor1(concrete);
	Component* decored2 = new Decor2(decored1);
	decored2->operation();

	cout << "---------------" << endl;

	Component* temp = decored2->remove();
	temp->operation();

	cout << "---------------" << endl;

	temp = temp->remove();
	temp->operation();

	cout << "---------------" << endl;

	temp = temp->remove();
	temp->operation();

	delete decored2;
	delete decored1;
	delete concrete;

	return 0;

运行结果:

Concrete component operation
Decor1 addedBehavior
Decor2 addedBehavior
---------------
Concrete component operation
Decor1 addedBehavior
---------------
Concrete component operation
---------------
Concrete component operation


c++设计模式学习笔记:decorator装饰模式

目录简介动机(Motivation)模式定义结构(Structure)要点总结笔记结语简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识... 查看详情

c++之装饰(decorator)模式

...或者多重继承出自己想要的类型。装饰模式是一种结构型设计模式,其可以动态的为一个类增加职责(相对于继承)。1装饰模式部件(Component)声明封装器和被封装对象的公用接口。具体部件(ConcreteComponent)类是被封装对象所... 查看详情

设计模式之装饰模式(decorator)详解及代码示例(代码片段)

一、装饰模式的定义  装饰(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。二、装饰模式优缺点  装饰(Decorator)模式的主... 查看详情

设计模式之装饰模式decorator

  代码实现publicinterfaceICar{voidmove();}抽象构建角色//具体构件角色(真实对象,被装饰角色)classCarimplementsICar{@Overridepublicvoidmove(){System.out.println("陆地上跑");}}具体构建角色//装饰角色classSuperCarimplementsICar{privateI 查看详情

设计模式入门之装饰器模式decorator

//装饰模式定义:动态地给一个对象加入一些额外的职责。//就添加功能来说,装饰模式比生成子类更为灵活//这也提现了面向对象设计中的一条基本原则,即:尽量使用对象组合。而不是对象继承//Component:组件对象的接口。能... 查看详情

装饰者模式c++实现(代码片段)

参考书籍《HeadFirst设计模式》设计模式和设计原则       装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者模式提供了比继承更有弹性的替代方案。装饰者模式遵循的设计原则:      类应该对扩展开... 查看详情

设计模式之——decorator模式

Decorator模式又叫装饰者模式,这种模式是为了满足Java开发的“面向扩展开放,面向修改闭源”的开发原则设计出来的。在装饰者模式中,不修改源类的代码,却能修改源类中方法的功能。下面就以Angelababy化妆为例,详细介绍一... 查看详情

设计模式之装饰模式(decorator)

装饰模式是指对象在运行的过程中不断的增加对象,不断的增加功能,而不改变原来对象,只是将对象进行多一层的封装,这个原来类留有接口与这个新的类进行连接,下面我们来假设有一个基类来做继承的角色类,这里大家都... 查看详情

装饰模式decorator

第三章 装饰模式Decorator 1.1什么是装饰模式?装饰模式Decorator,动态的给一些对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活1.2装饰模式Decorator的结构图 Component是定义一个对象接口,可以给这些对... 查看详情

设计模式--装饰模式(decorator)

装饰模式(Decorator): 动态的给一个对象加入一些额外的职能,就添加功能来说,装饰模式比生成子类更为灵活。实例:ConcreteComponent:让Decorator对象为自己加入功能。有时候使用ConcreteComponent的派生类提供核心功能。在这样的... 查看详情

decorator装饰(结构型)

...装饰类多了不好管理,顺序也会乱;二:模式图  三:实现代码简单例子:1、定义操作的接口类;   查看详情

设计模式-装饰器模式

UML图:Component功能接口,里面的Operation方法为具体要实现的功能ConcreteComponent接口具体的实现类或者说是实现主体Decorator装饰器类,一般是个抽象类和Component是聚合关系,里面包含了一个Component的引用ConcreteDecorator具体的装饰器... 查看详情

设计模式之装饰者模式c++实现(代码片段)

参考书籍《HeadFirst设计模式》设计模式和设计原则       装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者模式提供了比继承更有弹性的替代方案。装饰者模式遵循的设计原则:      类应该对扩展开... 查看详情

decorator装饰模式(代码片段)

装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。装饰模式的结构装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象... 查看详情

设计模式--装饰模式decorate(结构型)

一、装饰模式动态地给一个对象添加额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。有时我们希望给某个对象而不是整个类添加一些功能。二、UML图1.Component(概念中提到的对象接口),也就是“被装饰者”。2.Con... 查看详情

c++装饰器模式的实现(代码片段)

...象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。意图:动态地给一个对象添加一些额外的 查看详情

java设计模式之decorator模式

...持类库的可重用性可扩展性和灵活性其中使用到了大量的设计模式本文将介绍JDK的I/O包中使用到的Decorator模式并运用此模式实现一个新的输出流类  Decorator模式简介  Decorator模式又名包装器(Wrapper)它的主要用途在于给一个对... 查看详情

结构型模式之装饰

...直接给出GOF著作中的Decorator模式结构图:实现案例是学习设计模式的好办法,GOF一书中给了一个例子,就是“图形用户界面”设计的问题,一个图形用户界面工具箱允许你对任意一个用户界面组件添加一些特性,例如边框,或是... 查看详情