Java装饰器设计模式实现

装饰器模式是一种结构性设计模式,它允许你在运行时动态地扩展一个对象的功能。在装饰器模式中,你可以将一个对象包装在另一个对象中,从而给这个对象添加新的行为或责任。下面是一个 Java 实现的示例:

第一个实现

假设我们有一个抽象类Shape,它定义了一个方法draw,它将在具体的形状类中实现。

public abstract class Shape {
    public abstract void draw();
}

现在我们需要添加一个新的行为,即给形状添加边框。我们可以使用装饰器模式来实现这个功能。

首先,我们创建一个抽象的装饰器类ShapeDecorator,它扩展了Shape类,并将其构造函数的参数设置为Shape对象。

public abstract class ShapeDecorator extends Shape {
    protected Shape decoratedShape;

    public ShapeDecorator(Shape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }

    public void draw() {
        decoratedShape.draw();
    }
}

接下来,我们创建一个具体的装饰器类BorderDecorator,它扩展了ShapeDecorator类,并在draw方法中添加了边框的逻辑。

public class BorderDecorator extends ShapeDecorator {
    public BorderDecorator(Shape decoratedShape) {
        super(decoratedShape);
    }

    public void draw() {
        decoratedShape.draw();
        setBorder(decoratedShape);
    }

    private void setBorder(Shape decoratedShape){
        System.out.println("Adding Border to Shape");
    }
}

现在,我们可以创建具体的形状类,例如CircleRectangle

public class Circle extends Shape {
    public void draw() {
        System.out.println("Shape: Circle");
    }
}

public class Rectangle extends Shape {
    public void draw() {
        System.out.println("Shape: Rectangle");
    }
}

最后,我们可以使用装饰器模式将BorderDecorator对象包装在CircleRectangle对象中,从而给这些对象添加边框行为。

Shape circle = new Circle();
Shape rectangle = new Rectangle();

Shape circleWithBorder = new BorderDecorator(new Circle());
Shape rectangleWithBorder = new BorderDecorator(new Rectangle());

circle.draw();
rectangle.draw();

circleWithBorder.draw();
rectangleWithBorder.draw();

输出:

Shape: Circle
Shape: Rectangle
Shape: Circle
Adding Border to Shape
Shape: Rectangle
Adding Border to Shape

在这个示例中,我们通过装饰器模式成功地为形状添加了新的行为,而不需要修改原始的形状类。

第二个实现案例

首先定义一个接口,它表示将要被装饰的对象:

public interface Component {
    void operation();
}

然后定义一个具体的实现类:

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

接下来定义一个抽象装饰器类,它持有一个 Component 对象,并且与 Component 有相同的接口:

public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

然后定义具体的装饰器类,它通过继承装饰器类并添加额外的行为来实现装饰:

public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addBehavior();
    }

    private void addBehavior() {
        System.out.println("ConcreteDecoratorA.addBehavior() called.");
    }
}

public class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addBehavior();
    }

    private void addBehavior() {
        System.out.println("ConcreteDecoratorB.addBehavior() called.");
    }
}

最后,在客户端代码中使用装饰器模式:

public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        component = new ConcreteDecoratorA(component);
        component = new ConcreteDecoratorB(component);

        component.operation();
    }
}

输出结果为:

ConcreteComponent.operation() called.
ConcreteDecoratorA.addBehavior() called.
ConcreteDecoratorB.addBehavior() called.

可以看到,通过装饰器模式,我们成功地将行为动态地添加到对象中,同时保持了对象的接口不变。这种模式可以在运行时动态地添加和删除装饰器,从而对对象进行细粒度的控制。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值