编程知识 cdmana.com

Code boy Comics: restudy the decorator mode of design pattern

Coder takes you to learn design patterns

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

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

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

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

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

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

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

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

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

http://img2.sycdn.imooc.com/5fd0d6de0001450607250492.jpg

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

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

http://img3.sycdn.imooc.com/5fd0d7020001235207250492.jpg

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

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

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

Code

Abstract base class

public abstract class  hero  {  public abstract int  attack ()  public abstract int  cooling ()
   ........
}

To implement the base class

public class  Li Bai  extends  hero  {   @Override
   public int  cooling (){      return 5;
  }
   ........
}

Buff abstract class

public abstract Buff extends  hero  {   public abstract int  attack ()  public abstract int  cooling ()
   ........
}

Different Buff Implementation class of

public  red buff extends Buff {   private  hero   hero ;   public  red buff( hero   hero ){        this. hero  =  hero ;
    }     @Override
   public int  attack () {     return  hero . attack () +  red buff Extra attack ;
   }
   .........
 
}public  blue buff extends Buff {   private  hero   hero ;   public  blue buff( hero   hero ){        this. hero  =  hero ;
    }    @Override
   public int  cooling () {
     return  hero . cooling ()  +  blue buff Additional reduced cooling value ;
   }
   .........

}

client

public class Client {
    public static void main(String[] args) {        //  No, buff The hero of 
         hero   nothing buff Li Bai  = new  Li Bai ();       
        //  It's red 
         red buff  red buff Li Bai  = new  red buff( nothing buff Li Bai );        //  Another basket 
         basket buff  Red and blue buff Li Bai  = new  basket buff( red buff Li Bai );

    }
}

Be careful : The above code block is written in Chinese 、 Variable names are for better understanding , In the actual combat process, remember to replace with the corresponding English .

summary

http://img2.sycdn.imooc.com/5fd0d736000182eb06400485.jpg

come from 《Head first Design patterns 》

PS:

I wrote about it before , It's in the form of a cartoon .

If you think it's good, just like it , If you have any suggestions, please leave a message to discuss .


Click to see more

版权声明
本文为[osc_ 43xj61td]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225111312334g.html

Scroll to Top