装饰器模式(Decorator Pattern)是一种结构型设计模式,允许您在不改变现有对象结构的情况下,动态地将新功能附加到对象上。通过创建一个包装器类来扩展原始类的功能。这个包装器类具有与原始类相同的接口,并在内部持有一个指向原始对象的引用。通过将多个包装器链接在一起,可以递归地添加任意数量的功能。
- // 抽象基础组件
- abstract class Component {
- public abstract void operation();
- }
-
- // 具体基础组件
- class ConcreteComponent extends Component {
- public void operation() {
- System.out.println("执行具体操作");
- }
- }
-
- // 抽象装饰器
- abstract class Decorator extends Component {
- protected Component component;
-
- public Decorator(Component component) {
- this.component = component;
- }
-
- public void operation() {
- if (component != null) {
- component.operation();
- }
- }
- }
-
- // 具体装饰器A
- class ConcreteDecoratorA extends Decorator {
- public ConcreteDecoratorA(Component component) {
- super(component);
- }
-
- public void addedBehavior() {
- System.out.println("新增附加行为 A");
- }
-
- public void operation(){
- super.operation();
- addedBehavior();
- }
- }
-
- // 具体装饰器B
- class ConcreteDecoratorB extends Decorator{
- public ConcreteDecoratorB(Component comp){
- super(comp);
- }
-
- public void addedBehavior(){
- System.out.println("新增附加行为 B");
- }
-
- public void operation(){
- super.operation();
- addedBehavior();
- }
- }
- // 使用示例
- public class Main {
- public static void main(String[] args) {
- // 使用示例
- Component component = new ConcreteComponent();
- component = new ConcreteDecoratorA(component);
- component.operation();
- component = new ConcreteDecoratorB(component);
- component.operation();
- }
- }
在上述示例中,我们定义了一个抽象基础组件类Component,它包含一个抽象方法operation()。然后创建了具体的基础组件类ConcreteComponent,它实现了这个抽象方法。
接下来,我们创建了一个抽象装饰器类Decorator,它也继承自基础组件类,并持有一个指向基础组件对象的引用。在装饰器类中,我们通过调用基础组件对象的operation()方法来实现具体功能。
然后,我们创建了两个具体装饰器类ConcreteDecoratorA和ConcreteDecoratorB,它们分别扩展了抽象装饰器类并添加了额外的功能。在这些具体装饰器中,我们首先调用父类的operation()方法来执行原始操作,然后再执行附加行为。
最后,在客户端代码中,我们创建一个具体基础组件对象,并使用多个具体装饰器对象进行包裹和扩展。通过调用最外层装饰器对象的operation()方法来触发整个装饰过程上所有操作的执行。
需要注意,在这种方式下,每次增加新类型或结构变化时都需要修改相关子类。这可能会导致代码的脆弱性。
- // 基础组件
- interface Component {
- void operation();
- }
-
- // 具体基础组件
- class ConcreteComponent implements Component {
- public void operation() {
- System.out.println("执行具体操作");
- }
- }
-
- // 抽象装饰器
- abstract class Decorator implements Component {
- protected Component component;
-
- public Decorator(Component component) {
- this.component = component;
- }
-
- public void operation() {
- if (component != null) {
- component.operation();
- }
- }
- }
-
- // 具体装饰器A
- class ConcreteDecoratorA implements Component {
- private Component component;
-
- public ConcreteDecoratorA(Component comp){
- this.component = comp;
- }
-
- public void addedBehavior(){
- System.out.println("新增附加行为 A");
- }
-
- public void operation(){
- if(component != null){
- component.operation();
- }
- addedBehavior();
- }
- }
-
- // 具体装饰器B
- class ConcreteDecoratorB implements Component {
- private Component component;
-
- public ConcreteDecoratorB(Component comp){
- this.component = comp;
- }
-
- public void addedBehavior(){
- System.out.println("新增附加行为 B");
- }
-
- public void operation(){
- if(component != null){
- component.operation();
- }
- addedBehavior();
- }
- }
-
-
- public class Main {
- public static void main(String[] args) {
- // 使用示例
- Component component = new ConcreteComponent();
- component = new ConcreteDecoratorA(component);
- component = new ConcreteDecoratorB(component);
- component.operation();
- }
- }
在上述示例中,我们定义了一个基础组件接口Component,它包含一个方法operation()。
然后创建了具体的基础组件类ConcreteComponent,它实现了该接口并实现具体的业务逻辑。
接下来,我们创建了抽象装饰器类 Decorator ,它也实现了相同的接口。这个装饰器持有一个指向基础组件对象的引用,并在其内部调用基础组件对象对应方法。
然后,我们创建了两个具体装饰器类 ConcreteDecoratorA 和 ConcreteDecoratorB ,它们分别实现了该接口并扩展了功能。在这些具体装饰器中,我们首先调用基础组件对象的 operation() 方法来执行原始操作,然后再执行附加行为。
最后,在客户端代码中,我们创建一个具体基础组件对象,并使用多个具体装饰器对象进行包裹和扩展。通过调用最外层装饰器对象的 operation() 方法来触发整个装饰过程上所有操作的执行。
存在的问题:
- // 基础组件
- interface Component {
- void operation();
- }
-
- // 具体基础组件
- class ConcreteComponent implements Component {
- public void operation() {
- System.out.println("执行具体操作");
- }
- }
-
- // 抽象装饰器
- abstract class Decorator implements Component {
- protected Component component;
- protected Decorator nextDecorator;
-
- public void setNextDecorator(Decorator decorator) {
- this.nextDecorator = decorator;
- }
-
- public void operation() {
- if (component != null) {
- component.operation();
- }
-
- if (nextDecorator != null) {
- nextDecorator.operation();
- }
- }
- }
-
- // 具体装饰器A
- class ConcreteDecoratorA extends Decorator {
- public ConcreteDecoratorA(Component comp){
- this.component = comp;
- }
-
- public void addedBehavior(){
- System.out.println("新增附加行为 A");
- }
-
- public void operation(){
- if(component != null){
- component.operation();
- }
-
- addedBehavior();
-
- if(nextDecorator != null){
- nextDecorator.operation();
- }
- }
- }
-
- // 具体装饰器B
- class ConcreteDecoratorB extends Decorator {
- public ConcreteDecoratorB(Component comp){
- this.component = comp;
- }
-
- public void addedBehavior(){
- System.out.println("新增附加行为 B");
- }
-
- public void operation(){
- if(component != null){
- component.operation();
- }
- addedBehavior();
-
- if(nextDecorator!=null){
- nextDecorator.operation();
- }
- }
- }
-
-
-
- public class Main {
- public static void main(String[] args) {
- // 使用示例
- Component component = new ConcreteComponent();
-
- ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component);
- ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(component);
- decoratorA.setNextDecorator(decoratorB);
-
- decoratorA.operation();
- }
- }
存在问题:
使用动态装饰器链时可能会遇到以下问题: