Decorator mode of design mode

Posted by pcjackson06 on Fri, 01 Nov 2019 20:48:42 +0100

Decorator mode

There are two main roles in decorator mode:

  • Decorator (jacket, hat)
  • Decorated object (Lao Wang)

The decorator and the object to be decorated have two characteristics, which are also the key to the decorator mode:

  • They implement the same interface
  • Decorated objects are used in the decorator

Use:

/**
 * <p>
 *Define an interface
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public interface Person {
    /**
     * Calculate cumulative consumption
     * @return
     */
    public Double cost();

    /**
     * Output information
     */
    public void show();
}
/**
 * <p>
 *Lao Wang (the decorated object) implements the defined interface
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public class LaoWang implements Person{
    @Override
    public Double cost() {
        //No consumption, consumption is 0
        return 0.0;
    }

    @Override
    public void show() {
        System.out.println("Lao Wang, I'm spending now"+this.cost());
    }
}
/**
 * <p>
 *The decorator superclass implements the same interface Person as the decorated object:
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public abstract class ClothesDecorator implements Person{
    /**
     * The object to be decorated in the decorator is passed in the construction method
     */
    protected Person person;

    public ClothesDecorator(Person person){
        this.person=person;
    }
}
/**
 * <p>
 *Specific decoration, jacket
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public class Jacket extends ClothesDecorator{

    public Jacket(Person person){
        /**
         * super Point to your superclass
         */
        super(person);
    }
    @Override
    public Double cost() {
        //Buy a jacket
        return person.cost()+100;
    }

    @Override
    public void show() {
        person.show();
        System.out.println("Lao Wang, I've spent another jacket 100 now. I've used it accumulatively"+this.cost());
    }
}
/**
 * <p>
 *Specific decorations, hats
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public class Hat extends ClothesDecorator{

    public Hat(Person person){
        //Point to your superclass
        super(person);
    }

    @Override
    public Double cost() {
        return person.cost()+200;
    }

    @Override
    public void show() {
        person.show();
        System.out.println("Lao Wang, I've spent another 200 hats. I've used them in total"+this.cost());
    }
}

test

/**
 * <p>
 *Program entry
 * </p>
 *
 * @author aodeng-Low profile Panda
 * @since 19-7-11
 */
public class LearnMain {
    public static void main(String[] args) {
        //Create an old king
        Person laoWang=new LaoWang();
        //Lao Wang bought a jacket
        laoWang=new Jacket(laoWang);
        //Lao Wang bought a hat
        laoWang=new Hat(laoWang);
        laoWang.show();
/*
        Console output:
        Lao Wang, I now consume 0.0
        Lao Wang, I've consumed another jacket 100, and I've used 100.0 in total
        Lao Wang, I've spent another 200 hats, 300.0 in total*/
    }
}

Source code

Source address: https://github.com/java-aodeng/hope

Links:

This article is released by the low-key panda one article multiple sending operation! Welcome to public address: low key panda.

Topics: Java github