亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

碼仔漫畫|重學設計模式之裝飾者模式

码仔带你学习设计模式

https://img3.sycdn.imooc.com/5fd0d6060001210607250492.jpg

https://img4.sycdn.imooc.com/5fd0d6070001f14907250492.jpg

https://img2.sycdn.imooc.com/5fd0d6070001111007250492.jpg

https://img1.sycdn.imooc.com/5fd0d6080001565d07250492.jpg

https://img1.sycdn.imooc.com//5fd0d69d00014e2007250492.jpg

https://img1.sycdn.imooc.com/5fd0d60900010f0707250492.jpg

https://img1.sycdn.imooc.com//5fd0d6be0001524b07250492.jpg

https://img1.sycdn.imooc.com/5fd0d60d0001d2ad07250492.jpg

https://img3.sycdn.imooc.com/5fd0d60e0001093507250492.jpg

https://img1.sycdn.imooc.com//5fd0d6de0001450607250492.jpg

https://img4.sycdn.imooc.com/5fd0d61a0001b12307250492.jpg

https://img3.sycdn.imooc.com/5fd0d61b0001989107250492.jpg

https://img1.sycdn.imooc.com//5fd0d7020001235207250492.jpg

https://img4.sycdn.imooc.com/5fd0d61c0001521806400432.jpg

https://img1.sycdn.imooc.com//5fd0d71f0001235207250492.jpg

https://img2.sycdn.imooc.com/5fd0d61d0001c6ce07250492.jpg

代码

抽象基类

public abstract class 英雄 {  public abstract int 攻击()  public abstract int 冷却()
   ........
}

实现基类

public class 李白 extends 英雄 {   @Override
   public int 冷却(){      return 5;
  }
   ........
}

Buff 抽象类

public abstract Buff extends 英雄 {   public abstract int 攻击()  public abstract int 冷却()
   ........
}

不同 Buff 的实现类

public 红buff extends Buff {   private 英雄 英雄;   public 红buff(英雄 英雄){        this.英雄 = 英雄;
    }     @Override
   public int 攻击() {     return 英雄.攻击() + 红buff额外攻击;
   }
   .........
 
}public 蓝buff extends Buff {   private 英雄 英雄;   public 蓝buff(英雄 英雄){        this.英雄 = 英雄;
    }    @Override
   public int 冷却() {
     return 英雄.冷却()  + 蓝buff额外减少的冷却值;
   }
   .........

}

客户端

public class Client {
    public static void main(String[] args) {        // 没有buff的英雄
        英雄 无buff李白 = new 李白();       
        // 打了红
        红buff 红buff李白 = new 红buff(无buff李白);        // 再来个篮
        篮buff 红蓝buff李白 = new 篮buff(红buff李白);

    }
}

注意:以上代码块用中文编写类名、变量名是为了让大家更好的理解,在实战过程中记得替换成相对应的英文。

总结

https://img1.sycdn.imooc.com//5fd0d736000182eb06400485.jpg

出自《Head first 设计模式》

PS:

内容是我之前写过的,改成了漫画的形式。

大家觉得不错就点个赞,如有建议可以欢迎留言讨论。


點擊查看更多內容
2人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
67
獲贊與收藏
1544

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消