Java design patterns described in (09): decorative pattern

This article Source: GitHub · Click here || GitEE · Click here

A living scene

1, scene description

Monkey King Seventy-like changes, each change of his gave him an additional skill. When he becomes a fish, you can go swimming in the water; when he turned into a bird, you can fly in the sky.

2, illustrating scenes

3, code implementation

public class C01_InScene {
    public static void main(String[] args) {
        TheGreatestSage greatestSage = new Monkey();
        TheGreatestSage fish = new Fish(greatestSage);
        fish.shapeDesc();
        // 这里虽然是鱼形态,但是本体是悟空,所以可以直接变化
        TheGreatestSage bird = new Bird(fish);
        bird.shapeDesc();
    }
}
// 顶级接口
interface TheGreatestSage {
    // 定义一个描述形态的方法
    void shapeDesc ();
}
// 悟空本尊
class Monkey implements TheGreatestSage{
    @Override
    public void shapeDesc() {
        System.out.println("Monkey.move()");
    }
}
// 包装一层变化的描述
class Change implements TheGreatestSage {
    private TheGreatestSage greatestSage;
    Change(TheGreatestSage greatestSage){
        this.greatestSage = greatestSage;
    }
    @Override
    public void shapeDesc() {
        greatestSage.shapeDesc();
    }
}
// 具体的变化形态
class Fish extends Change{
    public Fish(TheGreatestSage greatestSage) {
        super(greatestSage);
    }
    public void shapeDesc(){
        System.out.println("鱼在水中游...");
    }
}
class Bird extends Change{
    public Bird(TheGreatestSage greatestSage) {
        super(greatestSage);
    }
    public void shapeDesc(){
        System.out.println("鸟在空中飞...");
    }
}

Second, decorative patterns

1, the basic concept

Decorative pattern, also known as packaging (Wrapper) mode. Decorative pattern to the client transparently extend the function of the object, it is an alternative inheritance relationships.
Decorative pattern of customers in a transparent manner in order to dynamically attach the additional responsibilities to an object. In other words, the client does not feel decorative objects in front and the rear trim any different. In the case of decorative patterns can not use to create more subclasses, the object function to be expanded.

2, the core role

  • Abstract component role

    It gives an abstract interface to regulate ready to receive additional responsibilities.

  • Specific component role

    The definition of a class to receive additional responsibilities.

  • Decorative role

    Examples of a member holding the object, and define a consistent interface to the interface with the abstract member.

  • Specific decorative role

    Responsible for component object "labeled" additional responsibilities.

3, illustrates a pattern

4, the source code to achieve

// 抽象构件角色
interface Component {
    void sampleOperation();
}
// 具体构件角色
class ConcreteComponent implements Component{
    @Override
    public void sampleOperation() {
        System.out.println("业务相关代码");
    }
}
//装饰角色
class Decorator implements Component{
    private Component component;
    public Decorator(Component component){
        this.component = component;
    }
    @Override
    public void sampleOperation() {
        //委派给构件进行处理
        component.sampleOperation();
    }
}
// 具体装饰角色
class ConcreteDecoratorA extends Decorator{
    public ConcreteDecoratorA(Component component) {
        super(component);
    }
    @Override
    public void sampleOperation() {
        super.sampleOperation();
        System.out.println("A.处理相关业务的代码");
    }
}
class ConcreteDecoratorB extends Decorator{
    public ConcreteDecoratorB(Component component) {
        super(component);
    }
    @Override
    public void sampleOperation() {
        super.sampleOperation();
        System.out.println("B.处理相关业务方法");
    }
}

5, the simplified model

  • Simplified abstract class

If only one class ConcreteComponent, consider removing abstract Component class (interface), the Decorator ConcreteComponent as a subclass.

  • Simplify decoration

If only one ConcreteDecorator class, then there is no need for a separate Decorator class, and can merge Decorator and ConcreteDecorator responsibility as a class.

6, translucent description

1), purely decorative patterns hard to find. Intention of decorative patterns without changing the interface is the premise of enhancements to the class.

2), when enhancements often require new methods disclosed.

3), which led to the realization of most of the decorative patterns are "translucent", rather than completely transparent. In other words, the interface allows decorative patterns to change, add a new method. This means that clients can declare a variable of type ConcreteDecorator, so have the methods ConcreteDecorator class can be called.

4), a translucent decorative pattern is interposed between the adapter mode and a decorative pattern. Is intended to alter the mode adapter interface, you may be rewritten by one or several methods, or add new methods to enhance the functionality classes.

5), in fact most of the decorative patterns are translucent decorative patterns, such as decorative patterns also called half decorative, semi adapter mode.

Third, the practical application

1, JDK in IO streams

1) Basic Description

The most classic application of decorative patterns in the Java language is the design of Java I / O library. Few single stream object is created to provide the desired functionality IO streams by creating multiple overlapping objects, decorative pattern is the basic mode Java I / O library.

2), IO flow diagram described

  • Abstract component role

Played by InputStream. This is an abstract class that provides a unified interface for a variety of subtypes.

  • Specific component role

Played by FileInputStream, StringBufferInputStream and other categories. Which implements the interface defined in the abstract component role.

  • Abstract decorative role

Played by FilterInputStream. It implements the interface specified InputStream.

  • Specific decorative role

Played by several categories, namely BufferedInputStream, DataInputStream and not common to the two classes LineNumberInputStream, PushbackInputStream.

Fourth, the advantages and disadvantages of summary

1 advantages, mode

(1) decorative pattern and purpose of inheritance is to extend the function of the object, but the decorative pattern can provide more flexibility than inheritance. Decoration mode allows the system to dynamically determine "labeled" needs a "decorative", or remove an unwanted "decoration." Inheritance is different, inheritance is static, the system is running on it before the decision.

(2) By using different combinations and permutations of these specific decoration and the decoration, the engineer can create a lot of combinations of different behaviors.

2 advantages, mode

Due to the use of decorative patterns, you can use inheritance requires less than the number of classes. Using fewer classes, of course, that the design is easy. But, on the other hand, the use of decorative patterns produce more than inheritance of objects.

Fifth, the source code address

GitHub地址:知了一笑
https://github.com/cicadasmile/model-arithmetic-parent
码云地址:知了一笑
https://gitee.com/cicadasmile/model-arithmetic-parent

Guess you like

Origin www.cnblogs.com/cicada-smile/p/11432933.html
Recommended