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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值