Java设计模式实现之三--装饰模式

待装饰的主类

public class Person {
    public Person() {
    }
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public void show(){
        System.out.println("装饰的"+name);
    }
}

服饰类

public class Finery extends Person{
    protected Person component;
    public void setComponent(Person component){
        this.component=component;
    }
    public void show(){
        if(component!=null){
            component.show();
        }
    }
}

实现服饰方法的子类

public class LeatherShoes extends Finery{
    public void show(){
        System.out.println("皮鞋 ");
        super.show();
    }
}
public class BigTrouser extends Finery{
    public void show(){
        System.out.println("垮裤 ");
        super.show();
    }
}
public class Sneakers extends Finery{
    public void show(){
        System.out.println("破球鞋 ");
        super.show();
    }
}
public class Suit  extends Finery{
    public void show(){
        System.out.println("西装 ");
        super.show();
    }
}
public class Tie extends Finery{
    public void show(){
        System.out.println("领带 ");
        super.show();
    }
}
public class TShirts extends Finery{
    public void show(){
        System.out.println("大T恤 ");
        super.show();
    }
}

主测试类

public class Program {
    public static void main(String[] args){
        Person xc=new Person("小菜");
        System.out.println("第一种装扮");
        Sneakers sneakers=new Sneakers();
        BigTrouser bigTrouser=new BigTrouser();
        TShirts tShirts=new TShirts();
        sneakers.setComponent(xc);
        bigTrouser.setComponent(sneakers);
        tShirts.setComponent(bigTrouser);
        tShirts.show();
    }
}

githup源码地址
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值