1. 程式人生 > >10.設計模式&OO原則概覽

10.設計模式&OO原則概覽

  1. 程式碼

    1. Duck

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/17 上午8:11
       */
      public abstract class Duck {
          private Flyable flyable;
          private Quackable quackable;
      
          public Flyable getFlyable() {
              return flyable;
          }
      
          public void setFlyable
      (Flyable flyable) { this.flyable = flyable; } public Quackable getQuackable() { return quackable; } public void setQuackable(Quackable quackable) { this.quackable = quackable; } public void swim() { System.out.println("swimming."); } public
      abstract void display(); public void quack() { System.out.println("original quack."); } public void fly() { System.out.println("original fly."); } }
    2. MallrdDuck

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/17 上午8:12
       */
      public class MallrdDuck extends Duck { @Override public void display() { System.out.println("green"); } @Override public void quack() { this.getQuackable().quack(); } @Override public void fly() { this.getFlyable().fly(); } }
    3. RubberDuck

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/17 上午8:17
       */
      public class RubberDuck extends Duck {
          @Override
          public void display() {
              System.out.println("yellow");
          }
      
          @Override
          public void quack() {
              this.getQuackable().quack();
          }
      
          @Override
          public void swim() {
              super.swim();
          }
      
          @Override
          public void fly() {
              this.getFlyable().fly();
          }
      }
      
      
    4. Flyable

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * Created by presleyli on 2018/10/17.
       */
      public interface Flyable {
          void fly();
      }
      
      
    5. CanFly

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/19 上午7:32
       */
      public class CanFly implements Flyable {
          @Override
          public void fly() {
              System.out.println("I can fly...");
          }
      }
      
      
    6. CannotFly

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/19 上午7:32
       */
      public class CannotFly implements Flyable {
          @Override
          public void fly() {
              // do nothing.
          }
      }
      
      
    7. Quackable

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * Created by presleyli on 2018/10/17.
       */
      public interface Quackable {
          void quack();
      }
      
      
    8. CanQuack

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/19 上午7:33
       */
      public class CanQuack implements Quackable{
          @Override
          public void quack() {
              System.out.println("I can quack...");
          }
      }
      
      
    9. CannotQuack

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/19 上午7:33
       */
      public class CannotQuack implements Quackable{
          @Override
          public void quack() {
              // do nothing.
          }
      }
      
      
    10. Main

      package com.desmond.codebase.designpattern.overview.v4;
      
      /**
       * @author presleyli
       * @date 2018/10/17 上午8:14
       */
      public class Main {
          public static void main(String[] args) {
              MallrdDuck mallrdDuck = new MallrdDuck();
              mallrdDuck.setFlyable(new CanFly());
              mallrdDuck.setQuackable(new CannotQuack());
              mallrdDuck.fly();
              mallrdDuck.quack();
      
              RubberDuck rubberDuck = new RubberDuck();
              rubberDuck.setFlyable(new CanFly());
              rubberDuck.setQuackable(new CanQuack());
              rubberDuck.fly();
              rubberDuck.quack();
      
          }
      }
      
      
    11. Output:

      I can fly...
      I can fly...
      I can quack...