首页 > 代码库 > 装饰模式

装饰模式

People.java类

package decorate;


public class People{
    private String name;
    
    public People() {
    }
    public People(String name) {
        this.name = name;
    }

    public void operation() {
        System.out.println("开始装饰:"+name);
    }
}

Finery.java类

package decorate;

public class Finery extends People{
    private People people;
    
    public void decorate(People people) {
        this.people = people;
    }

    public void operation(){
        people.operation();
    }
}

TshirtFinery.java类

package decorate;

public class TshirtFinery extends Finery {

    @Override
    public void operation() {
        super.operation();
        this.show();

    }
    public void show(){
        System.out.println("大T衫");
    }

}

BigTrouserFinery.java类

package decorate;

public class BigTrouserFinery extends Finery {
    @Override
    public void operation() {
        super.operation();
        this.show();
        
    }
    public void show(){
        System.out.println("大裤衩");
    }
}

ShoeFinery.java类

package decorate;

public class ShoeFinery extends Finery {
    @Override
    public void operation() {
        super.operation();
        this.show();
        
    }
    public void show(){
        System.out.println("长袜子");
    }
}

Business.java类

package decorate;

public class Business {
    public static void main(String[] args) {
        People p = new People("桂清");
        TshirtFinery t = new TshirtFinery();
        BigTrouserFinery b = new BigTrouserFinery();
        ShoeFinery s = new ShoeFinery();
        t.decorate(p);
        b.decorate(t);
        s.decorate(b);
        s.operation();

    }
}

 

装饰模式