Java装饰器模式,装饰器模式通常通过创建一个接口和一个或多个实现了该接口的类来开始,然后创建装饰器类,这些类也实现了相同的接口

1、定义一个接口Component

public interface Component {  
    void operation();  
}

2、创建一个实现了Component接口的简单类SimpleComponent

public class SimpleComponent implements Component {  
    @Override  
    public void operation() {  
        System.out.println("SimpleComponent operation.");  
    }  
}

3、创建一个装饰器基类Decorator,它也实现了Component接口,并持有一个对Component对象的引用

public abstract class Decorator implements Component {  
    protected Component component;  
  
    public Decorator(Component component) {  
        this.component = component;  
    }  
  
    @Override  
    public void operation() {  
        if (component != null) {  
            component.operation();  
        }  
    }  
}

4、创建一个具体的装饰器类,例如ConcreteDecoratorA,它添加了一些额外的功能

public class ConcreteDecoratorA extends Decorator {  
    public ConcreteDecoratorA(Component component) {  
        super(component);  
    }  
  
    @Override  
    public void operation() {  
        super.operation(); // 调用被装饰对象的operation方法  
        addedFunctionA(); // 添加额外的功能A  
    }  
  
    private void addedFunctionA() {  
        System.out.println("ConcreteDecoratorA added function.");  
    }  
}

5、编写一个测试类来展示如何使用装饰器

public class DecoratorPatternDemo {  
    public static void main(String[] args) {  
        // 创建一个简单组件  
        Component simpleComponent = new SimpleComponent();  
        // 使用装饰器A装饰简单组件  
        Component decoratedComponent = new ConcreteDecoratorA(simpleComponent);  
        // 调用被装饰后的组件的operation方法  
        decoratedComponent.operation();  
    }  
}

在这里插入图片描述

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我来回答你的问题。 首先,装饰器模式是一种结构型模式,它允许动态地向对象添加行为或功能。这种模式是通过创建一个包装对象来实现的,该对象包含原始对象并提供了额外的功能。 以下是一个使用Java实现装饰器模式的例子: ```java interface Component { void operation(); } class ConcreteComponent implements Component { @Override public void operation() { System.out.println("ConcreteComponent operation"); } } class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { component.operation(); } } class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } @Override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA operation"); } } class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } @Override public void operation() { super.operation(); System.out.println("ConcreteDecoratorB operation"); } } ``` 在这个例子中,`Component`是一个接口,定义了一个`operation`方法。`ConcreteComponent`实现了`Component`接口,并实现了`operation`方法。`Decorator`是一个抽象实现了`Component`接口并维护了一个指向`Component`的引用。`ConcreteDecoratorA`和`ConcreteDecoratorB`都扩展了`Decorator`并实现了它们自己的`operation`方法,同时在调用`super.operation()`之后添加了自己的功能。 下面是装饰器模式的优缺点及应用场景: 优点: 1. 装饰器模式允许向对象动态添加行为,而不会影响其他对象。 2. 装饰器模式比继承更加灵活,因为它允许在运行时添加或删除功能。 3. 装饰器模式遵循开放/关闭原则,因为它允许添加新功能而不影响现有代码。 4. 装饰器模式允许按需添加或删除功能。 缺点: 1. 装饰器模式会导致许多小对象的创建,这些对象可能会影响性能。 2. 装饰器模式可能会使代码变得更加复杂,因为它需要使用多个对象来完成一个任务。 应用场景: 1. 当需要向对象动态添加功能时,可以使用装饰器模式。 2. 当不能使用继承来扩展对象时,可以使用装饰器模式。 3. 当需要按需添加或删除对象的功能时,可以使用装饰器模式。 4. 当需要保持的封闭性但又需要灵活性时,可以使用装饰器模式。 希望这个回答可以帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Acmen-zym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值