初探装饰器模式

装饰器模式:动态地将责任附加到对象上,允许用户向现有对象添加新功能而不改变其结构。若要扩展功能,装饰器提供了比继承更有弹性的替代方案。

场景:假如有这样一个抽象装备类

package pattern.decorator;

public abstract class Equipment {
    int power;//战斗力
    String desc = "";//介绍

    public String getDesc(){
        return desc;
    }

    //获取战斗力
    public abstract int getPower();
}

然后实现一个武器类继承上面的装备类

package pattern.decorator;

public class Weapon extends Equipment {

    public Weapon(){
        desc = "屠龙宝刀";
        power = 1000000;
    }

    @Override
    public int getPower() {
        return power;
    }
}

一段时间后,装备增加了进阶,时装,强化等功能,这些功能都可以提升战斗力,现在希望计算提升后的战斗力。

先不管其他方法是怎么实现这个需求的,让我们来看看装饰器模式是怎么设计的,并且不会改变上面的Weapon类。

 

首先,设计一个装备的装饰器抽象类

package pattern.decorator;

public abstract class Decorator extends Equipment {
    //显示都装饰了什么
    public abstract String getDesc();
}

然后实现强化,进阶,时装的装饰类

强化(强化一次战力提升1000):

package pattern.decorator;

public class StrengthDecorator extends Decorator {

    Equipment equipment;//需要被装饰的装备
    int strength = 0;//强化了多少

    public StrengthDecorator(Equipment equipment,int strength){
        this.equipment = equipment;
        this.strength = strength;
    }

    @Override
    public String getDesc() {
        return "强化了"+strength+"次"+","+equipment.getDesc();
    }

    @Override
    public int getPower() {
        return equipment.getPower() + 1000*strength;
    }
}

进阶(进阶一次战力提升2000):

package pattern.decorator;

public class AdvanceDecorator extends Decorator {

    Equipment equipment;//需要被装饰的装备
    int advance = 0;//进阶了多少

    public AdvanceDecorator(Equipment equipment, int advance){
        this.equipment = equipment;
        this.advance = advance;
    }

    @Override
    public String getDesc() {
        return "进阶了"+advance+"次"+","+equipment.getDesc();
    }

    @Override
    public int getPower() {
        return equipment.getPower() + 2000*advance;
    }
}

时装(根据不同的时装提升战力):

package pattern.decorator;

public class FactionDecorator extends Decorator {

    Equipment equipment;//需要被装饰的装备
    String faction = "";//进阶了多少
    int add = 0;

    public FactionDecorator(Equipment equipment, int faction){
        this.equipment = equipment;
        if(faction==1){
            this.faction = "春节时装";
            add = 5000;
        }else{
            this.faction = "普通时装";
            add = 2500;
        }
    }

    @Override
    public String getDesc() {
        return faction+","+equipment.getDesc();
    }

    @Override
    public int getPower() {
        return equipment.getPower() + add;
    }
}

最后实现个测试类看看装饰器模式是怎么运作的


package pattern.decorator;

public class ShowController {
public static void main(String[] args) {
Equipment weapon = new Weapon();
System.out.println(weapon.getDesc()+",战力:"+weapon.getPower());
weapon = new AdvanceDecorator(weapon,10);
System.out.println(weapon.getDesc()+",战力:"+weapon.getPower());
weapon = new FactionDecorator(weapon,1);
System.out.println(weapon.getDesc()+",战力:"+weapon.getPower());
weapon = new StrengthDecorator(weapon,15);
System.out.println(weapon.getDesc()+",战力:"+weapon.getPower());
}
}
 

运行结果:

屠龙宝刀,战力:1000000
进阶了10次,屠龙宝刀,战力:1020000
春节时装,进阶了10次,屠龙宝刀,战力:1025000
强化了15次,春节时装,进阶了10次,屠龙宝刀,战力:1040000

posted @ 2018-08-09 15:40  YzdFly  阅读(166)  评论(0编辑  收藏  举报