<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 7, comments - 1, trackbacks - 0, articles - 0
    Problem:???Started with a simple SimUDuck App..
    ???Joe's company?makes a duck pond simulation game, SimUDuck, The game can show a large variety of duck species swimming and making quacking sounds.

    Initial Design:
    diagram.JPG
    But now some new functionality should be added, for example: we need some of the ducks to FLY.

    First Design:
    ???We add a method fly() into the Duck class. It seems worked, but something went horribly wrong because not all ducks can fly. so....

    Second Design:?Using inheritance and polymorphism
    ???Always override the fly() mehtod in the subclass where needed.

    test.JPG
    ???

    ???Drawbacks:?Everytime a new duck is added, you will be forced to look at and possibly override fly() and quack(). so is there a cleaner way of having only some of the duck types fly or quack?

    Third Design: Using interface!
    ???test1.JPG
    Drawbacks: It completely destroy code reuse for those behaviors.

    1. Design Principles: Identify the aspects of your application that vary and separate them form what stays the same!
    ?????????????????????????????? which means Encapsulate the parts that vary!
    2. Design Principles: Program to an interface, not an implementation! (interface here means supertype! including interface
    ???????????????????????????????and abstract class!.. making use of the polymorphism functionality).
    3. Design?Principles:??Favor composition over interface!?

    Strategy Pattern: Using Composition!
    test2.JPG

    Code implement:
    FlyBehavior.java
    public?interface?FlyBehavior{
    ???
    public?void?fly();
    }

    FlyWithWings.java
    public?class?FlyWithWings?implements?FlyBehavior{
    ???
    public?void?fly(){
    ??????System.out.println(
    "I'm?flying!!");
    ???}

    }

    FlyNoWay.java
    public?class?FlyNoWay?implements?FlyBehavior{
    ???
    public?void?fly(){
    ??????System.out.println(
    "I?can't?fly");
    ???}

    }

    Duck.java
    public?abstract?class?Duck{
    ???FlyBehavior?flyBehavior;
    ???
    public?Duck(){??????
    ???}

    ???
    ???
    public?abstract?void?display();
    ???
    ???
    public?void?performFly(){
    ??????flyBehavior.fly();
    ???}

    ???
    ???
    public?void?swim(){
    ??????System.out.println(
    "All?ducks?float,?even?decoys!");
    ???}

    }

    MallardDuck.java
    public?class?MallardDuck{
    ???
    public?MallardDuck(){
    ??????flyBehavior
    =new?FlyWithWings();??????
    ???}

    ???
    ???
    public?void?display(){
    ??????System.out.println(
    "I'm?a?real?mallard?duck");
    ???}

    }

    The Definition?of Strategy Pattern:?The?Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them intercahgeable. Strategy lets the algorithm vary indepanedtl from client?that use it
    ?
    Problems:
    1. It's weird to have a class that's jast a behavior: classes represent things both have state and methods. a flying behavior might have instance variables representing the attributes for the flying behavior.
    2.?Be care?of?Over-Design: implement your code first, then refractoring!


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 美景之屋4在线未删减免费 | 67194在线午夜亚洲| 中美日韩在线网免费毛片视频| 白白国产永久免费视频| 亚洲高清毛片一区二区| 国产嫩草影院精品免费网址| 色偷偷尼玛图亚洲综合| 免费无码又爽又刺激高潮的视频| 亚洲成a人片在线观看天堂无码| 在线免费观看一级片| 国产精品成人亚洲| 亚洲精品成人在线| 国产VA免费精品高清在线| 亚洲人成人一区二区三区| 成在人线av无码免费高潮喷水| 国产成人精品日本亚洲网站 | 亚洲免费观看网站| 成年女人免费视频播放77777| 亚洲精品理论电影在线观看| 成人毛片18女人毛片免费视频未| 亚洲成AV人片高潮喷水| 亚洲精品国产精品乱码不卡 | 五月婷婷综合免费| 亚洲大码熟女在线观看| 亚洲国产成人影院播放| 人人揉揉香蕉大免费不卡| 亚洲国产精品综合一区在线| 在线观看免费宅男视频| 一级视频在线免费观看| 日韩亚洲Av人人夜夜澡人人爽 | 日本特黄特色免费大片| 韩日电影在线播放免费版| 亚洲美免无码中文字幕在线| 成年女人18级毛片毛片免费观看| 色婷婷精品免费视频| 亚洲国产精品婷婷久久| 女人与禽交视频免费看| 伊人久久大香线蕉免费视频| 亚洲视频在线观看2018| 精品国产亚洲一区二区在线观看| 99精品视频免费在线观看|