Decorator

1. Definitions

Dynamically add some additional responsibilities to an object. The decorator pattern is more flexible than subclassing in terms of adding functionality.

2. Structure

write picture description here
- Component: The interface of component objects, which can be dynamically added with responsibilities.
- ConcreteComponent: A specific component object that implements the component object interface, usually the original object to be decorated, that is, you can add responsibilities to this object.
- Decorator: The abstract parent class of all decorators, you need to define an interface consistent with the component interface, and hold a Component object, which is actually holding a decorated object.
- ConcreteDecorator: The actual decoration object, which implements the functions to be added by the decoration object.

3. Examples

Let’s take a meal as an example. The average person doesn’t pay much attention to eating, so they come first. Those who are a little more careful should wash their hands when eating, and those who are more particular should not only wash their hands before eating, but also eat fruit or drink after eating. a soup or something.

Person

/**
 * Person(相当于 Component)
 * @author lijun
 * @since 2018-03-22 10:19
 */
public interface Person {
   public void  eat();
}

ConcretePerson

/**
 * 一般人(ConcreteComponent)
 * @author lijun
 * @since 2018-03-22 10:45
 */
public class ConcretePerson  implements Person {
    @Override
    public void eat() {
        System.out.println("吃饭。。。");
    }
}

Decorator

/**
 * 装饰对象
 * @author lijun
 * @since 2018-03-22 10:49
 */
public interface Decorator extends Person  {
}

DecoratorPersonA

/**
 * DecoratorPersonA(实际的装饰对象,实现具体要被装饰对象添加的功能)
 *
 * @author lijun
 * @since 2018-03-22 10:52
 */
public class DecoratorPersonA implements Decorator {
    private Person person;

    public DecoratorPersonA(Person person) {
        this.person = person;
    }

    /**
     * 添加 上菜
     */
    private void washHands() {
        System.out.println("洗手。。。");
    }

    @Override
    public void eat() {
        washHands();
        person.eat();
    }

}
/**
 * DecoratorPersonB(实际的装饰对象,实现具体要被装饰对象添加的功能)
 *
 * @author lijun
 * @since 2018-03-22 10:52
 */
public class DecoratorPersonB implements Decorator {

    private Person person;

    public DecoratorPersonB(Person person) {
        this.person = person;
    }


    private void addFruit() {
        System.out.println("吃水果。。。");
    }

    @Override
    public void eat() {
        person.eat();
        addFruit();
    }
}

transfer:

@Test
    public void  testPerson(){

        System.out.println("一般人吃饭");
        Person person = new ConcretePerson();
        person.eat();

        System.out.println("\t");
        System.out.println("稍微讲究点的人吃饭");
        Decorator decoratorA = new DecoratorPersonA(person);
        decoratorA.eat();


        System.out.println("\t");
        System.out.println("比较稍微讲究点的人吃饭");
        Decorator decoratorB = new DecoratorPersonB(decoratorA);
        decoratorB.eat();
    }

output

一般人吃饭
吃饭。。。

稍微讲究点的人吃饭
洗手。。。
吃饭。。。

比较稍微讲究点的人吃饭
洗手。。。
吃饭。。。
吃水果。。。

4. Essence

Dynamic composition.

5. Summary

  1. advantage
    • more flexible than inheritance
    • easier to reuse
    • Simplify high-level definitions
  2. shortcoming
    • will produce many fine-grained objects

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325582553&siteId=291194637