zoukankan      html  css  js  c++  java
  • 【DP-装饰器】做菜的时候- 先放生姜后放盐,不想放胡椒

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

    • package club.interview.design_pattern.chapt8_decorator;
      
      /**
       * 做菜的时候- 先放生姜后方盐,不想放胡椒
       * <p>
       * 扩展:
       * 1. 可以看看Mybatis的缓存接口  - (org.apache.ibatis.cache.Cache)
       * 2. 如何放入自己的需求中。参考缓存,通过配置可以自由组合 同步的,lru的,fifo的
       *
       * @author QuCheng on 2020/6/19.
       */
      public interface IVegetable {
      
          /**
           * 如何让蔬菜做的更好吃
           */
          void cook();
      
          // <begin>----------------------测试类--------------------- //
      
          /**
           * 测试类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();
          }
          // ------------------------测试类---------------------<end> //
      
      
          // <begin>----------------------具体蔬菜--------------------- //
      
          class Cabbage implements IVegetable {
              /**
               * 如何让蔬菜做的更好吃
               */
              @Override
              public void cook() {
                  System.out.println("卷心菜...下锅啦");
              }
          }
      
          class Potato implements IVegetable {
              /**
               * 如何让蔬菜做的更好吃
               */
              @Override
              public void cook() {
                  System.out.println("土豆...下锅啦");
              }
          }
      
          // ----------------------具体蔬菜---------------------<end> //
      
          // <begin>---------------蔬菜装饰-------------------------- //
      
          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();
              }
          }
      
          // ----------------------蔬菜装饰---------------------<end> //
      
      
      }
  • 相关阅读:
    PJzhang:CVE-2020-1472微软NetLogon权限提升漏洞~复现
    PJzhang:vulnhub靶机sunset系列SUNSET:DECOY
    PJzhang:vulnhub靶机sunset系列SUNSET:TWILIGHT
    PJzhang:vulnhub靶机sunset系列SUNSET:SUNRISE
    PJzhang:vulnhub靶机sunset系列SUNSET:MIDNIGHT
    PJzhang:vulnhub靶机sunset系列SUNSET:SOLSTICE
    PJzhang:whatweb指纹扫描工具样例
    HTTP状态码解读
    【并发】线程与进程的区别
    MYSQL之性能优化 ----MySQL性能优化必备25条
  • 原文地址:https://www.cnblogs.com/nightOfStreet/p/13162593.html
Copyright © 2011-2022 走看看