【DP-装饰器】做菜的时候- 先放生姜后方盐,不想放胡椒

需求:自由为蔬菜搭配作料,可任意组合

  • package club.interview.design_pattern.chapt8_decorator;
    
    /**
     * 做菜的时候- 先放生姜后方盐,不想放胡椒
     * 

    * 扩展: * 1. 可以看看Mybatis的缓存接口 - (org.apache.ibatis.cache.Cache) * 2. 如何放入自己的需求中。参考缓存,通过配置可以自由组合 同步的,lru的,fifo的 * * @author QuCheng on 2020/6/19. */ public interface IVegetable { /** * 如何让蔬菜做的更好吃 */ void cook(); // ----------------------测试类--------------------- // /** * 测试类main * * @param args args */ static void main(String[] args) { // 自由搭配 new GingerDecorator(new SaltDecorator(new PepperDecorator(new Potato()))).cook(); System.out.println("------------"); new SaltDecorator(new PepperDecorator(new GingerDecorator(new Cabbage()))).cook(); } // ------------------------测试类--------------------- // // ----------------------具体蔬菜--------------------- // class Cabbage implements IVegetable { /** * 如何让蔬菜做的更好吃 */ @Override public void cook() { System.out.println("卷心菜...下锅啦"); } } class Potato implements IVegetable { /** * 如何让蔬菜做的更好吃 */ @Override public void cook() { System.out.println("土豆...下锅啦"); } } // ----------------------具体蔬菜--------------------- // // ---------------蔬菜装饰-------------------------- // class GingerDecorator implements IVegetable { private IVegetable vegetable; public GingerDecorator(IVegetable vegetable) { this.vegetable = vegetable; } /** * 如何让蔬菜做的更好吃 */ @Override public void cook() { System.out.println("加点生姜吧"); vegetable.cook(); } } class PepperDecorator implements IVegetable { private IVegetable vegetable; public PepperDecorator(IVegetable vegetable) { this.vegetable = vegetable; } /** * 如何让蔬菜做的更好吃 */ @Override public void cook() { System.out.println("加点胡椒吧"); vegetable.cook(); } } class SaltDecorator implements IVegetable { private IVegetable vegetable; public SaltDecorator(IVegetable vegetable) { this.vegetable = vegetable; } /** * 如何让蔬菜做的更好吃 */ @Override public void cook() { System.out.println("加点盐吧"); vegetable.cook(); } } // ----------------------蔬菜装饰--------------------- // }

     

 

你可能感兴趣的:(【DP-装饰器】做菜的时候- 先放生姜后方盐,不想放胡椒)