装饰器模式

定义

指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式。

由OCP原则可知:我们希望对扩展性的开放,对修改的封闭,而装饰器模式很好地契合了这一原则。

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

加的责任。

如下:当我们需要表示一个Person穿了什么衣服的时候,我们可以很灵活地通过Decorator添加,只需要简单传入person即可。比如new PersonWithCoat(person)就代表构造了一个穿着Coat的人。

public abstract class Person{
    String name;
    int age;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
}

//PersonWithWearing类为Person类的Decorator

public class PersonWithWearing extends Person{
    //原本的Person类作为装饰器的一个基本属性
    Person person;
    String wearing;
    public PersonWithWearing(Person person,String wearing){
    this.person=person;
    this.wearing=wearing; 
    }
}

//PersonWithCoat类为PersonWithWearing类的实例

public class PersonWithCoat extends PersonWithWearing{
    public PersonWithCoat(Person person){
        super(person,"coat");
    }
}


使用场景
当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。
不能采用继承的情况主要有两类:
第一类是系统中存在大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目
呈爆炸性增长;
第二类是因为类定义不能继承(如 final 类)
在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
当对象的功能要求可以动态地添加,也可以再动态地撤销时。
好处
装饰者模式可以带来比继承更加灵活性的扩展功能,使用更加方便,可以通过组合不同的装饰者对象
来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美的遵循开闭
原则,继承是静态的附加责任,装饰者则是动态的附加责任。
装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以
动态扩展一个实现类的功能。
装饰者的一般写法:由抽象装饰角色继承抽象构件角色,并把抽象构件角色当作参数传入抽象装饰角色。典型结构如下所示:

 

举个具体的例子:
假如我们现在要设计一个快餐服务的程序,设计了抽象类FastFood,有实例FriedRice和FriedNoodles,现在我们要增加配料,如鸡蛋、培根。如果我们直接使用继承,就会产生FriedRiceWithEgg、FriedRiceWithBacon、FriedNoodlesWithEgg、FriedNoodlesWithBacon等类,一旦以后要引入更多的配料,很容易引起类爆炸。
现在根据装饰器思想,我们可以设计一个装饰器类Garnish继承FastFood类,它把FastFood类作为参数传入自己的构造器,实现对FastFood的扩展。
具体代码如下:
FastFood类:
public abstract class FastFood {
    float price;
    String desc;
    public FastFood(float price,String desc){
        this.price=price;
        this.desc=desc;
    }

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

    public void setDesc(String desc){
        this.desc=desc;
    }

    public float getPrice(){
        return price;
    }

    public String getDesc(){
        return desc;
    }

    public abstract float cost();
}

Garnish类:

public abstract class Garnish extends FastFood{
    private FastFood fastFood;

    public FastFood getFastFood(){
        return fastFood;
    }

    public void setFastFood(FastFood fastFood){
        this.fastFood=fastFood;
    }

    public Garnish(FastFood fastFood,float price,String desc){
        super(price,desc);
        this.fastFood=fastFood;
    }
}

Garnish类的实际思想是,把父类整体当作参数传入自己的构造器,并在构造器参数列表中添加其他想要添加的新元素,实现对父类型的拓展。

Garnish的实现类Egg和Bacon:

public class Egg extends Garnish{
    public Egg(FastFood fastFood){
        super(fastFood,1, "鸡蛋");
    }

    public float cost(){
        return getPrice()+getFastFood().getPrice();
    }

    public String getDesc(){
        return super.getDesc()+getFastFood().getDesc();
    }
}
public class Bacon extends Garnish{
    public Bacon(FastFood fastFood){
        super(fastFood,2, "培根");
    }

    public float cost(){
        return getPrice()+getFastFood().getPrice();
    }

    public String getDesc(){
        return super.getDesc()+getFastFood().getDesc();
    }
}

Egg和Bacon类通过在自己的单参构造器中利用Super调用父类的有参构造器,实现传入一个参数,得到此拓展后的此参数的目的。

FastFood的实现类FriedRice和FriedNoodles:

public class FriedRice extends FastFood{
    public FriedRice(){
        super(10,"炒饭");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}
public class FiredNoodles extends FastFood{
    public FiredNoodles(){
        super(12,"炒面");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}

测试类Client:

public class Client {
    public static void main(String[] args) {
        //点一份炒饭
        FastFood food=new FriedRice();
        System.out.println(food.getPrice()+" "+food.cost()+"元");

        System.out.println("--------------");
        //点一份加鸡蛋的炒饭
        FastFood food1=new FriedRice();
        //把自身传入修饰器,增加鸡蛋
        food1=new Egg(food1);
        System.out.println(food1.getDesc()+" "+food1.cost()+"元");

        System.out.println("--------------");
        //点一份加培根的炒面
        FastFood food2=new FiredNoodles();
        //把自身传入修饰器,增加培根
        food2=new Bacon(food2);

        System.out.println(food2.getDesc()+" "+food2.cost()+"元");

    }
}

测试结果:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值