Design Patterns -Decorator mode

The Decorator (decorative) mode belongs to the structural model.
For example, when it requires three different additional features, you can create three derived classes. However, if it requires both of two characteristics or any combination of the various characteristics when the class inherits the method is no longer appropriate.
It is not allowed to add new functionality to an existing object through inheritance, without changing its structure.

An example (fun Blue Moon)

Not long ago Nick endorsement of the "fun Blue Moon" ad fire, "I Department of Cha-hui, is Laikan my brothers ..." brainwashed now, just use this game to explain the decorator pattern.

Play the game everyone knows the legend of this kind of game core gameplay is to buy equipment, Daguai, upgrade, buy equipment such repeatedly.

Just registered account to enter the game players assume that there is only one big pants, worth 500 gold coins, with the brush strange upgrade, the body of a piece of equipment also increased, this time we need to know how much gold the value of the equipment body.

Define what a player

public interface Gamer {
    /**
     * 获取目前的装备
     * @return
     */
    String getEquip();

    /**
     * 获取目前身上装备的价格
     * @return
     */
    int getPrice();
}

Define specific Master Professional players

public class MasterGamer implements Gamer {
    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return "大裤衩";
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return 5;
    }
}

The new Master players go only big pants, play equipment thanks.

Traditional inheritance achieve

Equipment "Master Mace"

public class TruncheonMasterGamer extends MasterGamer{
    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return super.getEquip()+",法师权杖";
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return super.getPrice()+50;
    }
}

Continue to equip "magic cloak"

public class CloakTruncheonMasterGamer extends TruncheonMasterGamer{
    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return super.getEquip()+",魔法斗篷";
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return super.getPrice()+80;
    }
}

Note that this is already equipped before the "Master Mace" of up to inherit.

Computing equipment prices

CloakTruncheonMasterGamer gamer = new CloakTruncheonMasterGamer();
System.out.println("当前装备:"+gamer.getEquip()+"\n装备总价值:"+gamer.getPrice());

Output

当前装备:大裤衩,法师权杖,魔法斗篷
装备总价值:135

Decorator achieve

image
Universal declaration decorative base class "equipment"

public abstract class Equip implements Gamer {
    private Gamer gamer;

    public Equip(Gamer gamer) {
        this.gamer = gamer;
    }
    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return gamer.getEquip();
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return gamer.getPrice();
    }
}

Specific decorator "Master Mace"

public class Truncheon extends Equip {
    public Truncheon(Gamer gamer) {
        super(gamer);
    }

    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return super.getEquip()+",法师权杖";
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return super.getPrice()+50;
    }
}

Specific decorator "magic cloak"

public class Cloak extends Equip {
    public Cloak(Gamer gamer) {
        super(gamer);
    }

    /**
     * 获取目前的装备
     *
     * @return
     */
    @Override
    public String getEquip() {
        return super.getEquip()+",魔法斗篷";
    }

    /**
     * 获取目前身上装备的价格
     *
     * @return
     */
    @Override
    public int getPrice() {
        return super.getPrice()+80;
    }
}

Computing equipment prices

//创建一个法师玩家
Gamer gamer = new MasterGamer();
//给法师玩家装备法师权杖
gamer = new Truncheon(gamer);
//给法师玩家装备魔法斗篷
gamer = new Cloak(gamer);
System.out.println("当前装备:"+gamer.getEquip()+"\n装备总价值:"+gamer.getPrice());

Output

当前装备:大裤衩,法师权杖,魔法斗篷
装备总价值:135

Compared

The above example is relatively simple, traditional inheritance implementation and Decorator realize the difference is not very obvious, but think carefully about'll still find some differences:

  • Traditional inherit the implementation is not free, there is no "componentized" feature. Player's equipment can be freely combined, disassemble, and this is characteristic for the inheritance can only be achieved through a variety of sub-class combination. Like the example above, the equipment "Master Mace" and "magic cloak" need to go on the basis of inheritance has "Master Mace" basis.
  • Decorator achieve such properties and subsidiary body separately, without the presence of a single (Gamer Equip class which declares the object). Equipment and players are separated, they can be individually equipped with any equipment to the players, but also free to remove the equipment.

to sum up

In this design pattern can not only extend the functionality of a class, may be a dynamic addition, dynamic revoked. But the disadvantage is multi-layered decorative use relatively complex. The specific functions essentially segregation of duties (e.g. distinguished responsibilities core components and additional properties) decrease subclass inherits directly coupling the parent class.


You can obtain the relevant code here: Design mode -Decorator mode

Guess you like

Origin www.cnblogs.com/xuxiaojian/p/11468734.html