Decorator_Pattern

The "Decorator Pattern" is like an onion.

The abstract class of decorator and the ConcreteComponent both extend the abstract class called component.

Meanwhile,the decorator composites the component.

If we want to overwrite(override) some functions through subclass,we can use inheritance.

If we just need to override(overload) some functions,we can modify it in the class directly.

if we must add sth between peers,what we actually need to do is use composation.

The specific codes are as follows:

package decorator_pattern;

enum CharacterType {// default is permitted to use in the whole package.
    introverted, extraverted
}

public abstract class PersonComponent {
    protected String name;// the default value is empty.
    protected CharacterType character;

    public PersonComponent(String name, CharacterType character) {
        this.name = name;
        this.character = character;
    }

    public abstract void show();
}

package decorator_pattern;

public class Tom extends PersonComponent {
    public Tom(String name, CharacterType character) {
        super(name, character);
        // TODO Auto-generated constructor stub
    }

    public void show() {
        System.out.println("Hello everyone,my name is " + super.name);
        System.out.println("and my character is " + super.character);
    }
}

package decorator_pattern;

public class Harry extends PersonComponent {
    public Harry(String name, CharacterType character) {
        super(name, character);
        // TODO Auto-generated constructor stub
    }

    public void show() {
        System.out.println("Hello everyone,my name is " + super.name);
        System.out.println("and my character is " + super.character);
    }
}
 

package decorator_pattern;

public abstract class FineryDecorator extends PersonComponent {
    protected PersonComponent person;

    // prohibit construct outside.
    // keep decorator acting on concrete person all the time.
    // but if we use private,the subclass won't be permitted.
    // and the abstract class cann't construct concrete object naturally.
    protected FineryDecorator(String name, CharacterType character) {
        super(name, character);
        // TODO Auto-generated constructor stub
    }

    public void SetPerson(PersonComponent person) {
        this.person = person;
    }

    @Override
    public void show() {
        if (person != null) {
            person.show();
        }
    }

}

package decorator_pattern;

public class TShirt extends FineryDecorator {
    public TShirt(String name, CharacterType character) {
        super(name, character);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void show() {
        super.show();
        System.out.println("This is my Tshirt.");
    }
}

package decorator_pattern;

public class BigTrouser extends FineryDecorator {
    public BigTrouser(String name, CharacterType character) {
        super(name, character);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void show() {
        super.show();
        System.out.println("This is my BigTrouser.");
    }
}

package decorator_pattern;

public class Main {
    public static void main(String args[]) {
        BigTrouser trouser = new BigTrouser(null, null);
        TShirt shirt = new TShirt(null, null);
        Tom t = new Tom("Tom", CharacterType.extraverted);
        shirt.SetPerson(t);
        trouser.SetPerson(shirt);
        trouser.show();
        BigTrouser trouserNew = new BigTrouser(null, null);
        TShirt shirtNew = new TShirt(null, null);
        Harry h = new Harry("Harry", CharacterType.introverted);
        shirtNew.SetPerson(h);
        trouserNew.SetPerson(shirtNew);
        trouserNew.show();
    }

}
This is a general introduction to the 23 design patterns:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214

猜你喜欢

转载自blog.csdn.net/GZHarryAnonymous/article/details/82711190