外汇交易网站开发,做电子书的网站很有名后来被关闭了,运营的三个核心要素,wordpress及时聊天装饰模式#xff08;Decorator Pattern#xff09;是一种结构型设计模式#xff0c;它允许向现有对象动态地添加新功能#xff0c;同时又不改变其结构。装饰模式通过将对象放入包装器中来实现#xff0c;在包装器中可以动态地添加功能。 
在装饰模式中#xff0c;通常会有…装饰模式Decorator Pattern是一种结构型设计模式它允许向现有对象动态地添加新功能同时又不改变其结构。装饰模式通过将对象放入包装器中来实现在包装器中可以动态地添加功能。 
在装饰模式中通常会有四个角色 
Component组件定义一个对象接口可以动态地给这些对象添加职责。ConcreteComponent具体组件实现Component接口并定义需要被装饰的类。Decorator装饰器持有一个Component对象的引用并定义一个与Component接口一致的接口。ConcreteDecorator具体装饰器扩展Decorator类覆盖其中的方法以添加新的功能。 
#include iostream// Component组件
class Coffee {
public:virtual void makeCoffee()  0;
};// ConcreteComponent具体组件
class SimpleCoffee : public Coffee {
public:void makeCoffee() override {std::cout  Making simple coffee  std::endl;}
};// Decorator装饰器
class CoffeeDecorator : public Coffee {
protected:Coffee* coffee;public:CoffeeDecorator(Coffee* coffee) : coffee(coffee) {}void makeCoffee() override {if (coffee) {coffee-makeCoffee();}}
};// ConcreteDecorator具体装饰器
class MilkDecorator : public CoffeeDecorator {
public:MilkDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}void makeCoffee() override {if (coffee) {coffee-makeCoffee();addMilk();}}void addMilk() {std::cout  Adding milk  std::endl;}
};// ConcreteDecorator具体装饰器
class SugarDecorator : public CoffeeDecorator {
public:SugarDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}void makeCoffee() override {if (coffee) {coffee-makeCoffee();addSugar();}}void addSugar() {std::cout  Adding sugar  std::endl;}
};int main(int argc, char *argv[])
{// 创建一个简单的咖啡对象Coffee* simpleCoffee  new SimpleCoffee();// 使用装饰器为咖啡添加牛奶Coffee* milkCoffee  new MilkDecorator(simpleCoffee);// 制作带牛奶的咖啡milkCoffee-makeCoffee();// 使用装饰器为牛奶咖啡添加糖Coffee* milkSugarCoffee  new SugarDecorator(milkCoffee);milkSugarCoffee-makeCoffee();delete simpleCoffee;delete milkCoffee;delete milkSugarCoffee;return 0;
}/*
在这个示例中Coffee 是一个抽象基类SimpleCoffee 是具体的咖啡类CoffeeDecorator 是装饰器类MilkDecorator 是具体的装饰器类用于添加牛奶。通过装饰器模式我们可以动态地给咖啡对象添加新的功能而不需要修改原有的类结构。
*/ 
觉得有帮助的话打赏一下呗。。