装饰者模式-java

概念:

装饰者模式允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

装饰模式中的角色:

抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象
具体构件(Concrete Component)角色:实现抽象构件,通过装饰者角色为其添加一些职责
抽象装饰(Decorator)角色:继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能
具体装饰(Concrete Decorator)角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任

实现案例:

//抽象构件角色(抽象手机类)
public abstract class Phone {
    private int price;
    private String desc;
    public Phone() {
    }
    public Phone(int price, String desc) {
        this.price = price;
        this.desc = desc;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
    //计算手机的价格
    public abstract int cost();
}
//具体构件角色 (小米11手机)
public class XM11Phone extends Phone {
    public XM11Phone() {
        super(2000, "小米11手机");
    }
    @Override
    public int cost() {
        return getPrice();
    }
}
//抽象装饰者角色(装饰者类)
//需要继承或实现抽象构件,并包含具体构件的实例
public abstract class Garnish extends Phone {
    private Phone phone;
    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public Garnish(Phone phone, int price, String desc) {
        super(price, desc);
        this.phone = phone;
    }
}
// 具体装饰者类角色(手机内存条)
public class RAM extends Garnish {

    public RAM(Phone phone) {
        super(phone, 500, "添加4G内存");
    }

    @Override
    public int cost() {
        return getPrice() + getPhone().cost();
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " " + getPhone().getDesc();
    }
}
// 具体装饰者角色(手机套儿)
public class PhoneCase extends Garnish {
  
    public PhoneCase(Phone phone) {
        super(phone, 50, "添加手机套儿");
    }

    @Override
    public int cost() {
        return getPrice() + getPhone().cost();
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " " + getPhone().getDesc();
    }
}
public class Test {
    public static void main(String[] args) {
        Phone xm11Phone = new XM11Phone();
        System.out.println(xm11Phone.getDesc() + " " + xm11Phone.cost() + "元");
        //给手机添加一个内存
        xm11Phone = new RAM(xm11Phone);
        System.out.println(xm11Phone.getDesc() + " " + xm11Phone.cost() + "元");
        //给手机添加一个套儿
        xm11Phone = new PhoneCase(xm11Phone);
        System.out.println(xm11Phone.getDesc() + " " + xm11Phone.cost() + "元");
    }
}
打印输出结果:
image.png

当需要增加新的手机(小米12)时,只需要新增一个具体构件角色(XM12Phone);当需要增加一些其他配件,比如手机膜时,只需要增加一个具体装饰者类即可,变得非常灵活。

优点:

1、装饰类和被装饰类可以独立发展,不会相互耦合
2、装饰模式是继承的一个替代模式(装饰者是动态的附加责任,继承是静态的附加责任)

缺点:

多层装饰比较复杂

使用场景:

1、扩展一个类的功能。
2、动态增加功能,动态撤销

扩展:

jdk源码:BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter 等包装类使用了装饰者模式
BufferedWriter 使用装饰者模式对Writer子实现类进行了增强,添加了缓冲区,提高了写数据的效率

静态代理和装饰者模式的区别
相同点:
1、都要实现与目标类相同的业务接口
2、在两个类中都要声明目标对象
3、都可以在不修改目标类的前提下增强目标方法
不同点:
1、目的不同装饰者是为了增强目标对象,静态代理是为了保护和隐藏目标对象)
2、获取目标对象构建的地方不同装饰者是由外界传递进来,可以通过构造方法传递。静态代理是在代理类内部创建,以此来隐藏目标对象)

你可能感兴趣的:(装饰者模式-java)