设计模式(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;
}

四 、优缺点总结

优点

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

缺点

  • 过度使用会增加微小对象数量,带来堆内存开销
  • 调用链过长影响性能,必要时可用责任链模式优化
相关推荐
程序员贺加贝27 分钟前
列表导出不够用-SaaS-ERP-单据详情导出的-Provider-模板与文档型-Excel-设计
java·后端·设计模式·架构·excel
INGNIGHT1 小时前
1584.连接所有点的最小费用(最小生成树&并查集union find)
c++·leetcode
wabs6661 小时前
关于二叉树【力扣144.二叉树的前序遍历的思考】
数据结构·c++·算法·leetcode·二叉树
zmzb01031 小时前
C++课后习题训练记录Day199
开发语言·c++
人邮异步社区1 小时前
如何系统地学习 C++ 语言?
开发语言·c++·学习
欧特克_Glodon1 小时前
OpenCV计算机视觉开发入门与实践<二十>:非线性变换灰度变换
c++·人工智能·opencv·计算机视觉
1000世界小札7 小时前
《大话数据结构》第9章精读:归并排序与快速排序完整 C++ 实现
数据结构·c++·算法
2601_9561219710 小时前
背包基础篇(01、完全、分组、多重、混合)
c++·算法·动态规划
fpcc10 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
ShineWinsu15 小时前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20