头图

Allows to dynamically add new functions to an existing object without changing its structure, which is equivalent to a wrapper for an existing object, so the decorator pattern is also called the wrapper pattern.

The following four roles generally exist in the decorator pattern

  • Abstract component: the common parent class of concrete components and abstract decorators, the most basic component
  • Concrete components: implement methods declared in the abstract construct, and decorators can add additional responsibilities to it
  • Abstract decorator: inherits abstract components and can introduce concrete components through construction methods, its role is mainly to combine concrete components
  • Concrete decorators: inherit abstract decorators and decorate components differently.

The following uses an example of decorating a car to implement the decorator pattern

 public interface Car {
    /**
     * 汽车描述
     */
    void describe();
}

There are two specific implementation classes under the car, representing fuel vehicles and new energy vehicles respectively.

 public class EnergyCar implements Car{
    @Override
    public void describe() {
        System.out.print("新能源汽车");
    }
}
 public class FuelCar implements Car{
    @Override
    public void describe() {
        System.out.printf("燃油车");
    }
}

Abstract class for car color decoration

 public class CarColorDecorator implements Car{
    private final Car car;

    public CarColorDecorator(Car car) {
        this.car = car;
    }

    @Override
    public void describe(){
        car.describe();
    }
}

The abstract class of the specific car color, and then the corresponding components can be combined here

 public class CarColorDecorator implements Car{
    private final Car car;

    public CarColorDecorator(Car car) {
        this.car = car;
    }

    @Override
    public void describe(){
        car.describe();
    }
}

The implementation class of the specific car color, modify the corresponding method to increase the function

 public class CarRedColorDecorator extends CarColorDecorator{
    public CarRedColorDecorator(Car car) {
        super(car);
    }

    @Override
    public void describe() {
        System.out.printf("红色的");
        super.describe();
    }

}

test

 public class DecoratorTest {
    @Test
    public void test(){
        Car car = new CarRedColorDecorator(new FuelCar());
        car.describe();
    }
}
=====结果=====
红色的燃油车

It seems that the decorator pattern and the bridge pattern are very similar, so what is the difference between the two patterns?

  1. The bridging mode is to allow two abstract concrete implementations to be freely combined, and another abstract class is introduced into an abstract class, and the "bridge" between abstract classes provides convenience for its subclasses to cooperate with each other. . In the decorator pattern, an abstraction is a subclass of another abstraction, and its purpose is to allow the implementation class of the decorator abstract class to decorate the implementation class of the decorated abstract class.
  2. Bridging is implemented through the combination of abstract classes, and decorators are implemented through the combination + inheritance between abstractions, and bridging is relatively less coupled.

eacape
211 声望8 粉丝

JAVA 攻城狮


« 上一篇
桥接模式
下一篇 »
门面模式