大话设计模式——装饰模式

本文介绍装饰模式的概念及其在软件设计中的应用。装饰模式允许在不改变原有对象的基础上动态地增加其功能,通过组合不同的装饰类来扩展核心功能。文章提供了装饰模式的实现代码示例。

装饰模式:

动态地给一个对象添加一些额外的职责。

优点:把类中的装饰功能从类中搬移出去,这样可以简化原有的类。有效地把类的核心功能和装饰功能区分开了。

解决的问题:

已经开发完毕的对象,后期由于业务需要,对旧的对象需要扩展特别多的功能,这时候使用给对象动态地添加新的状态或者行为(即装饰模式)方法,而不是使用子类静态继承。

 

        装饰模式是为已有功能动态地添加更多功能的一种模式。当系统需要新功能时,一般做法是向旧的类中添加新的代码,这些新

加的代码通常影响了原有类的核心职责或行为,在主类中加入新的字段、方法或是逻辑,从而增加了主类的复杂性,而这些新加入

的代码仅仅是为了满足一些只在某种特定情况下才会发生的特殊行为的需要。装饰模式提供了一个非常好的解决方案,它把每个要

装饰的功能放在单独的类中,并让这个类包装它所有要装饰的对象,这样当需要执行特殊行为时,客户代码就可以在运行时根据需

要有选择性地,按顺序的使用装饰功能包装的对象了。

代码如下:

Component.h
#ifndef COMPONENT_H
#define COMPONENT_H

class Component
{
public:
	virtual void Operation() = 0;
};

#endif
Concretecomponent.h
#include"Component.h"

#ifndef CONCRETECOMPONENT_H
#define CONCRETECOMPONENT_H

class Concretecomponent	: public Component
{
public:
	 void Operation();
};

#endif
Concretecomponent.cpp
#include<iostream>
#include"ConcreteComponent.h"
using namespace std;

void Concretecomponent::Operation()
{
	cout<<"具体对象的操作"<<endl;
}
Decorator.h
#include"Component.h"

#ifndef DECORATOR_H
#define DECORATOR_H

class Decorator	: public Component
{
protected:
	Component* component;
public:
	void Operation();
	void SetComponent(Component* _component);
};

#endif
Decorator.cpp
#include<iostream>
#include"Decorator.h"
using namespace std;

void Decorator::Operation()
{
	if(component != NULL)
		component->Operation();
}

void Decorator::SetComponent(Component* _component)
{
	this->component = _component;
}
ConcreteDecoratorA.h

#include"Decorator.h"

#ifndef CONCRETEDECORATORA_H
#define CONCRETEDECORATORA_H

class ConcreteDecoratorA : public Decorator
{
public:
	void Operation();
private:
	void AddedBehavior();
};

#endif
ConcreteDecoratorA.cpp
#include<iostream>
#include"ConcreteDecoratorA.h"
using namespace std;

void ConcreteDecoratorA::Operation()
{
	Decorator::Operation();
	AddedBehavior();
	cout<<"The ConcreteDecoratorA Called"<<endl;
}

void ConcreteDecoratorA::AddedBehavior()
{
	cout<<"ConcreteDecoratorA AddedBehavior"<<endl;
}
ConcreteDecoratorB.h
#include"Decorator.h"

#ifndef CONCRETEDECORATORB_H
#define CONCRETEDECORATORB_H

class ConcreteDecoratorB : public Decorator
{
public:
	void Operation();
private:
	void AddedBehavior();
};

#endif
ConcreteDecoratorB.cpp
#include<iostream>
#include"ConcreteDecoratorB.h"
using namespace std;

void ConcreteDecoratorB::Operation()
{
	Decorator::Operation();
	AddedBehavior();
	cout<<"ConcreteDecoratorB Operation "<<endl;
}

void ConcreteDecoratorB::AddedBehavior()
{
	cout<<"ConcreteDecoratorB AddedBehavior Called"<<endl;
}
main.cpp
#include<iostream>
#include"ConcreteComponent.h"
#include"ConcreteDecoratorA.h"
#include"ConcreteDecoratorB.h"
using namespace std;

int main()
{
	Concretecomponent c;
	ConcreteDecoratorA a;
	ConcreteDecoratorB b;
	a.SetComponent(&c);
	b.SetComponent(&a);
	b.Operation();
	return 0;
}
### C++装饰模式的设计与实现 #### 装饰模式概述 装饰模式是一种结构型设计模式,允许在运行时动态地给对象添加行为和责任。这种模式提供了比静态继承更灵活的方式来扩展功能[^2]。 #### 关键组件说明 - **抽象构件 (Component)** 定义了一个接口或抽象,用于声明所有具体构件以及装饰器共同的操作方法。所有具体的构件和装饰器都将基于此接口工作[^4]。 - **具体构件 (Concrete Component)** 实现了由`Component`定义的接口,代表基本的对象实例,在不附加任何额外的行为下完成特定的任务。 - **抽象装饰 (Decorator)** 继承自`Component`的同时也持有一个指向`Component`型的成员变量。它不仅能够代理原有对象的方法调用,还可以在此基础上加入新的逻辑处理。 - **具体装饰 (Concrete Decorator)** 扩展了`Decorator`的功能,通过重载某些方法来改变原对象的行为或是为其增添新特性。每一个具体的装饰者都可以独立存在,并且可以相互叠加使用以达到累积的效果。 #### 示例代码展示 下面是一个简单的C++程序展示了如何利用装饰模式为文本字符串加上不同风格的边框: ```cpp #include <iostream> #include <string> // 抽象构件:定义一个通用接口 class Shape { public: virtual void draw() const = 0; }; // 具体构件:实现了Shape接口的一个简单矩形形状 class Rectangle : public Shape { public: void draw() const override { std::cout << "Drawing a rectangle." << std::endl; } }; // 抽象装饰:持有对另一个Shape对象的引用 class BorderDecorator : public Shape { protected: Shape* shape; public: explicit BorderDecorator(Shape* s) : shape(s) {} ~BorderDecorator() override { delete shape; } void draw() const override { shape->draw(); } // 默认只是转发请求给内部持有的shape对象 }; // 具体装饰A:为图形添加虚线边框 class DashedBorderDecorator : public BorderDecorator { public: using BorderDecorator::BorderDecorator; void draw() const override { std::cout << "Adding dashed border..." << std::endl; BorderDecorator::draw(); } }; // 具体装饰B:为图形添加实心边框 class SolidBorderDecorator : public BorderDecorator { public: using BorderDecorator::BorderDecorator; void draw() const override { std::cout << "Adding solid border..." << std::endl; BorderDecorator::draw(); } }; int main(){ auto *rectangle = new Rectangle(); // 可以自由组合不同的装饰层 auto *dashed_rectangle = new DashedBorderDecorator(rectangle); auto *solid_dashed_rectangle = new SolidBorderDecorator(dashed_rectangle); solid_dashed_rectangle->draw(); delete solid_dashed_rectangle; return 0; } ``` 在这个例子中,`Rectangle`作为最基础的具体构件;而`DashedBorderDecorator` 和 `SolidBorderDecorator` 则分别扮演着两种不同型的具体装饰角色。它们可以在不影响其他部分的情况下轻松地被应用到任意数量的不同形状上[^3]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值