装饰模式是一种结构型设计模式,允许动态地向对象添加新功能,而不会影响其他对象。装饰模式通过使用一系列装饰类,将额外的行为或责任以层叠的方式附加到对象上。
FileStream
)可以通过装饰器动态添加功能(如缓冲、加密)。#include <iostream>
#include <memory>
using namespace std;
// 抽象组件
class Component {
public:
virtual void operation() const = 0;
virtual ~Component() = default;
};
// 具体组件
class ConcreteComponent : public Component {
public:
void operation() const override {
cout << "ConcreteComponent operation" << endl;
}
};
// 抽象装饰
class Decorator : public Component {
protected:
shared_ptr<Component> component;
public:
explicit Decorator(shared_ptr<Component> comp) : component(move(comp)) {}
void operation() const override {
component->operation();
}
};
// 具体装饰A
class ConcreteDecoratorA : public Decorator {
public:
explicit ConcreteDecoratorA(shared_ptr<Component> comp) : Decorator(move(comp)) {}
void operation() const override {
Decorator::operation();
addedBehavior();
}
void addedBehavior() const {
cout << "ConcreteDecoratorA added behavior" << endl;
}
};
// 具体装饰B
class ConcreteDecoratorB : public Decorator {
public:
explicit ConcreteDecoratorB(shared_ptr<Component> comp) : Decorator(move(comp)) {}
void operation() const override {
Decorator::operation();
addedBehavior();
}
void addedBehavior() const {
cout << "ConcreteDecoratorB added behavior" << endl;
}
};
// 客户端代码
int main() {
shared_ptr<Component> component = make_shared<ConcreteComponent>();
shared_ptr<Component> decoratorA = make_shared<ConcreteDecoratorA>(component);
shared_ptr<Component> decoratorB = make_shared<ConcreteDecoratorB>(decoratorA);
decoratorB->operation();
return 0;
}
using System;
// 抽象组件
public abstract class Component {
public abstract void Operation();
}
// 具体组件
public class ConcreteComponent : Component {
public override void Operation() {
Console.WriteLine("ConcreteComponent operation");
}
}
// 抽象装饰
public abstract class Decorator : Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public override void Operation() {
component.Operation();
}
}
// 具体装饰A
public class ConcreteDecoratorA : Decorator {
public ConcreteDecoratorA(Component component) : base(component) { }
public override void Operation() {
base.Operation();
AddedBehavior();
}
private void AddedBehavior() {
Console.WriteLine("ConcreteDecoratorA added behavior");
}
}
// 具体装饰B
public class ConcreteDecoratorB : Decorator {
public ConcreteDecoratorB(Component component) : base(component) { }
public override void Operation() {
base.Operation();
AddedBehavior();
}
private void AddedBehavior() {
Console.WriteLine("ConcreteDecoratorB added behavior");
}
}
// 客户端代码
class Program {
static void Main() {
Component component = new ConcreteComponent();
Component decoratorA = new ConcreteDecoratorA(component);
Component decoratorB = new ConcreteDecoratorB(decoratorA);
decoratorB.Operation();
}
}
特性 | 装饰模式 | 继承 | 扩展方式 |
---|---|---|---|
动态添加功能 | √(动态) | ×(静态,编译时) | 动态添加功能 |
开放-封闭原则 | 遵守 | 可能违反(增加新功能需要修改子类) | 遵守 |
对象数量 | 多层装饰增加对象数量 | 子类数量可能过多 | 对象数量 |
使用场景 | 功能动态变化时更适合 | 功能固定时更适合 | 使用场景 |
欢迎关注、点赞、收藏!更多系列内容可以点击专栏目录订阅,感谢支持,再次祝大家祉猷并茂,顺遂无虞!
若将文章用作它处,请一定注明出处,商用请私信联系我!