————— The next day —————

— — — — — —

What are the core roles of the decorator pattern?

1. The Component interfaces

In our example above, the Component interface is equivalent to the car interface, and all wrapped classes, wrapper classes, inherit from this interface.

2. ConcreteComponent class

ConcreteComponent is the wrapped implementation class. In this example, Mercedes, BMW, and Tesla all fall into this role.

3. Decorator abstract class

All wrapper classes inherit from the Decorator abstract class, which in turn implements the Component interface, in order to achieve multiple layers of nested wrapping.

4. The ConcreteDecorator class

Specific packaging classes, used to extend the functionality of the packaged class, such as the example of the autopilot function, flight function extension.

What is the relationship between these four core characters? We can express this using a UML class diagram of the decorator pattern:

The first is the car interface, the Component role, which defines the run behavior:

public interface Car {

void run();

}

Next up is the implementation class for various cars, the ConcreteComponent role, which has different implementations of the run behavior:

Public class BenzCar implements Car{@override public void run() {system.out.println (” BenzCar implements Car “); ); }} public class BmwCar implements Car{@override public void run() {system.out.println (” BmwCar implements Car “); ); }} public class implements Car{@override public void run() {system.out.println (); ); }}

Here is the abstract class of the Decorator, the Decorator role, which contains the decorated member object:

public class CarDecorator implements Car { protected Car decoratedCar; public CarDecorator(Car decoratedCar){ this.decoratedCar = decoratedCar; } public void run(){ decoratedCar.run(); }}

One might wonder why a decorator class implements a Car interface. This is where the decorator pattern is flexible.

Inheriting from the Car interface allows each decorator itself to be wrapped by a more enclosing decorator by passing a Car object as an argument to the enclosing decorator’s constructor.

Next up is the ConcreteDecorator implementation class, the ConcreteDecorator role. These decorators also implement the behavior of run, calling the run method of the wrapped object and doing some extended operations (such as autopilot, flight) :

public class AutoCarDecorator extends CarDecorator { public AutoCarDecorator(Car decoratedCar){ super(decoratedCar); } @Override public void run(){ decoratedCar.run(); autoRun(); } private void autoRun(){system.out.println (” turn on autopilot “); } } public class FlyCarDecorator extends CarDecorator { public FlyCarDecorator(Car decoratedCar){ super(decoratedCar); } @Override public void run(){ decoratedCar.run(); fly(); } private void fly(){system.out.println (” open flying car mode “); }}

Finally, there is our client class. The client class is responsible for creating the wrapped object and decorator and deciding how to wrap and execute it:

public class Client { public static void main(String[] args) { Car benzCar = new BenzCar(); Car bmwCar = new BmwCar(); Car teslaCar = new TeslaCar(); New AutoCarDecorator(benzCar) = new AutoCarDecorator(benzCar); New FlyCarDecorator(new AutoCarDecorator(bmwCar)); // Create FlyCarDecorator flyAutoBmwCar; benzCar.run(); bmwCar.run(); teslaCar.run(); autoBenzCar.run(); flyAutoBmwCar.run(); }}

Take input streams as an example. In order to meet different input scenarios, JDK designs a variety of input streams, including ByteArrayInputStream, FileInputStream, and so on.

These input streams all inherit from a common abstract class: InputStream.

At the same time, to extend the functionality of these input streams, the JDK designed a decorator class, FilterInputStream. This class inherits from InputStream and “combines” the InputStream member objects.

A number of decorator subclasses are derived from the FilterInputStream class, including BufferedInputStream, DataInputStream, and others, which provide additional functionality such as BufferedInputStream buffering and reading Java basic data types from the input stream, respectively.

Like this article friends, welcome to pay attention to the wechat public account programmer Xiao Grey, watch more exciting content