设计模式之装饰模式(Decorator)_代码(二)

文章标题:转载:设计模式之装饰模式(Decorator)

文章链接:https://blog.csdn.net/elizabethxxy/article/details/118608823

代码:

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Person {

    private String name;

    public Person() {}

    public Person(String name) {
        System.out.println("in class Person, method Person, this=" + this + ", name=" + name);
        this.name = name;
    }

    public void show() {
        System.out.println("in class Person, method show, this=" + this + ", name=" + name);
        System.out.println(name + "的装扮:");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

// 服装类
public class Finery extends Person {

    protected Person component;

    public void Decorate(Person component) {
        System.out.println("in class Finery, method Decorate, this=" + this + ", component=" + component);
        this.component = component;
    }

    @Override
    public void show() {
        System.out.println("in class Finery, method show, component=" + component);
        if(component != null) {
            component.show();
        }
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class TShirts extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class TShirts, method show, this=" + this + ", component=" + component);
        System.out.println("TShirts ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Sneakers extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class Sneakers, method show, this=" + this + ", component=" + component);
        System.out.println("Sneakers ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Trouser extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class Trouser, method show, this=" + this + ", component=" + component);
        System.out.println("Trouser ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class LeatherShoes extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class LeatherShoes, method show, this=" + this + ", component=" + component);
        System.out.println("LeatherShoes ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Tie extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class Tie, method show, this=" + this + ", component=" + component);
        System.out.println("Tie ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Suits extends Finery {

    @Override
    public void show() {
        super.show();
        System.out.println("in class Suits, method show, this=" + this + ", component=" + component);
        System.out.println("Suits ");
    }
}

----

package com.study.javastudy.designpattern.decorator.dressingexample;

public class Client {

    public static void main(String[] args) {
        //adam的换装
        Person adam = new Person("adam");

        Suits a = new Suits();
        Tie b = new Tie();
        LeatherShoes c = new LeatherShoes();

        a.Decorate(adam);
        b.Decorate(a);
        c.Decorate(b);
        c.show();

        System.out.println("\n--------------");

        //bill的换装
        Person bill = new Person("bill");

        TShirts x = new TShirts();
        Trouser y = new Trouser();
        Sneakers z = new Sneakers();

        x.Decorate(bill);
        y.Decorate(x);
        z.Decorate(y);
        z.show();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值