设计模式(c++)-结构型模式-装饰器模式

设计模式(C++)-结构型模式-装饰器模式

一、装饰器模式概述

装饰器模式(Decorator Pattern)是一种结构型设计模式,核心目标是在不修改原有类的基础上,动态地给对象添加新功能的行为。它通过创建一系列"装饰器"类来包装原始对象,形成一个可递归嵌套的对象链,从而实现功能的灵活组合。

核心原则:对扩展开放,对修改关闭(OCP)。用组合代替继承,避免"子类爆炸"。

二、装饰器模式UML类图

三、代码实现

cpp 复制代码
/*Decorator模式(装饰器模式)
 装饰器模式(Decorator Pattern)是一种结构型设计模式,核心目标是在不修改原有类的基础上,
 动态地给对象添加新功能的行为。它通过创建一系列"装饰器"类来包装原始对象,形成一个
 可递归嵌套的对象链,从而实现功能的灵活组合。
 核心原则:对扩展开放,对修改关闭(OCP)。用组合代替继承,避免"子类爆炸"。
*/
#pragma once
#include <string>
#include <memory>
//1.IComponent:抽象接口
class IComponent {
public:
	virtual ~IComponent() = default;
	virtual void operation(std::string&data)const = 0;
};
//2.接口实现
class TextProcessor :public IComponent {
public:
	void operation(std::string&data)const override;
};

//3.Decorator:抽象装饰器基类
class Decorator :public IComponent {
protected:
	std::unique_ptr<IComponent> wrapped_; //被包装的对象
public:
	explicit Decorator(std::unique_ptr<IComponent>comp) :wrapped_(std::move(comp)) {}
	//默认转发,字类可重写
	void operation(std::string&data)const override;
};

//4.ConcreateDecoratorA:添加前缀
class PrefixDecorator :public Decorator {
	std::string prefix_;
public:
	PrefixDecorator(std::unique_ptr<IComponent>comp, std::string prefix)
		:Decorator(std::move(comp)), prefix_(prefix) {}
	void operation(std::string&data)const override;
};

//5.ConcreateDecoratorB:反转字符串
class ReverseDecorator :public Decorator {
public:
	//使用基类构造函数
	using Decorator::Decorator;
	void operation(std::string&data)const override;
};

//6:统计长度装饰器
class LengthStateDecorator :public Decorator {
public:
	using Decorator::Decorator;
	void operation(std::string&data)const override;
};

void testDecorator();

//Decorator.cc
#include "Decorator.h"
#include <iostream>
#include <cctype>

//接口实现
void TextProcessor::operation(std::string&data)const {
	std::cout << "[TextProcessor] Processing: " << data << std::endl;
	//基础处理:转大写
	for (auto&c : data) c = std::toupper(c);
}

//抽象装饰器 //默认转发,子类可重写
void Decorator::operation(std::string&data)const {
	if (wrapped_) wrapped_->operation(data);
}

//添加前缀装饰器
void PrefixDecorator::operation(std::string&data)const {
	data = prefix_ + ">>" + data;
	Decorator::operation(data);//调用父类接口转发
}

//反转字符串
void  ReverseDecorator::operation(std::string&data)const {
	Decorator::operation(data); //先执行原操作
	std::reverse(data.begin(), data.end());
}

//统计长度装饰器
void LengthStateDecorator::operation(std::string&data)const {
	Decorator::operation(data);
	std::cout << "[Length] After processing:" << data.length() << "chars" << std::endl;
}

//调用测试
void testDecorator() {

	std::cout << "=================Decorator Begin===============" <<std::endl;
	std::string input = "hello world";
	std::cout << "=== 原始处理===" << std::endl;
	auto processor = std::make_unique<TextProcessor>();
	auto copy1 = input;
	processor->operation(copy1);
	std::cout << "Result: " << copy1 << std::endl;

	std::cout << "=== 装饰器链:前缀->大写->反转===" << std::endl;
	auto decorated = std::make_unique<ReverseDecorator>(
		std::make_unique<PrefixDecorator>(
			std::make_unique<TextProcessor>(),
			"Start"
			)
		);
	auto copy2 = input;
	decorated->operation(copy2);
	std::cout << "Result: " << copy2 << std::endl;

	std::cout << " === 加上统计装饰器===" << std::endl;
	auto withStats = std::make_unique<LengthStateDecorator>(
		std::make_unique<ReverseDecorator>(
		std::make_unique<PrefixDecorator>(
			std::make_unique<TextProcessor>(),
			"Demo")
		));
	auto copy3 = input;
	withStats->operation(copy3);
	std::cout << "Final: " << copy3 << std::endl;
	std::cout << "=================Decorator End===============" << std::endl;
}

四 、优缺点总结

优点

  • 比继承更灵活:避免为每项功能组合创建子类
  • 运行时动态装配:可随时增减装饰器
  • 单一职责:每个装饰器只关注一种增强逻辑
  • 类型透明:客户端无需区分原始对象与 装饰器

缺点

  • 过度使用会增加微小对象数量,带来堆内存开销
  • 调用链过长影响性能,必要时可用责任链模式优化
相关推荐
Queenie_Charlie2 小时前
二叉树_
c++·二叉树·简单树结构
良木生香2 小时前
【C++初阶】:STL——String从入门到应用完全指南(3)
c语言·开发语言·数据结构·c++·算法
两年半的个人练习生^_^2 小时前
每日一学:设计模式之适配器模式
java·设计模式·适配器模式
磊 子2 小时前
类模板与派生1
java·开发语言·c++
郝学胜-神的一滴3 小时前
深入理解 epoll_wait:高性能 IO 多路复用核心解密
linux·服务器·开发语言·c++·网络协议
c++之路3 小时前
C++ 面向对象编程(OOP)
开发语言·c++
白菜欣3 小时前
Linux权限
linux·运维·c++
我爱cope3 小时前
【从0开始学设计模式-12| 代理模式】
设计模式·代理模式
君生我老4 小时前
C++ 红黑树
c++