大话设计模式c++实现——装饰模式

本文通过一个具体的例子详细介绍了装饰模式的概念及其实现方式。装饰模式允许在不改变原有类的情况下为其添加新的功能,使得功能扩展更加灵活。文中通过一个装扮人物的例子展示了如何利用装饰模式逐步为对象添加不同的职责。
// designpattern6_decorator.cpp : 定义控制台应用程序的入口点。
//装饰模式:
//
//装饰模式是为已有的功能动态的添加更多功能的一种方式。
//有效的把类的核心功能和装饰功能区分开了。
//
/*装饰模式:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。有时我们希望给某个对象而不是整个类添加一些功能。
比如有一个手机,允许你为手机添加特性,比如增加挂件、屏幕贴膜等。一种灵活的设计方式是,将手机嵌入到另一对象中,由这个对象完成特性的添加,我们
称这个嵌入的对象为装饰。这个装饰与它所装饰的组件接口一致,因此它对使用该组件的客户透明。     装饰模式提供了更加灵活的向对象添加职责的方式。可以
添加和分离的方法,用装饰在运行时刻增加和删除职责。装饰模式提供了一种“即用即付” 的方法来添加职责。它并不试图在一个复杂的可定制的类中支持所
有可预见的特征,相反,你可以定义一个简单的类,并且用装饰类给它逐渐地添加功能。可以从简单的部件组合出复杂的功能*/


#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;

class Person
{
public:
Person(){}

Person(string name)
{
this->name = name;
//cout << name << endl;
}

virtual void Show()
{
cout << "装扮的" << name << endl;
}

//为了后面判断是否为空(add myself)
bool operator!=(const Person comp)const { return name != comp.name; }

bool operator==(const Person comp)const { return name == comp.name; }

bool IsEnpty(){ return name ==""; }

private:
string name;
};

//服饰类Decorator
class Finery :public Person
{
protected:
Person* component;
public:
void Decorate(Person &component)//此处必须是指针或引用才能实现动态联编
{
this->component = &component;


}


void Show() override
{
//if (component!=NULL)
if (~component->IsEnpty())
component->Show();
}
};

//具体服饰类:component->Show();或者Finery::Show();都行
class TShirts :public Finery
{
public:
void Show() override
{
cout << "大T血 ";
component->Show();
}
};


class BigTrouser :public Finery
{
public:
void Show() override
{
cout << "垮裤 ";
component->Show();
}
};

class Sneakers :public Finery
{
public:
void Show() override
{
cout << "破球鞋 ";
component->Show();
//Finery::Show();
}
};

class Tie :public Finery
{
public:
void Show() override
{
cout << "领带 ";
component->Show();
}
};

int main(int argc, char* argv[])
{
Person* xc = new Person("小菜");
cout << endl << "第一种装扮: ";

Sneakers *pqx = new Sneakers();
BigTrouser* kk = new BigTrouser();
TShirts* dtx = new TShirts();

pqx->Decorate(*xc);

kk->Decorate(*pqx);

dtx->Decorate(*kk);
dtx->Show();
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]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值