<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!


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲人成图片小说网站| 亚洲国产成人久久精品99| 少妇太爽了在线观看免费视频| 国产免费播放一区二区| 久久国产乱子精品免费女| 在免费jizzjizz在线播| 日韩a级毛片免费视频| 亚洲精品国产高清嫩草影院| 综合自拍亚洲综合图不卡区| 亚洲AV综合色区无码二区爱AV| 亚洲av无码一区二区三区天堂| 国产福利免费视频| 免费观看AV片在线播放| 亚洲av无码乱码在线观看野外| 美女又黄又免费的视频| 99国产精品视频免费观看| 免费二级毛片免费完整视频| 久久香蕉国产线看观看亚洲片| 2020国产精品亚洲综合网| 成人妇女免费播放久久久| 毛片免费在线观看网址| 亚洲熟妇中文字幕五十中出| 一区二区三区无码视频免费福利| 国产小视频免费观看| jiz zz在亚洲| 亚洲黄色免费观看| 亚洲人成欧美中文字幕| 美丽的姑娘免费观看在线播放| 亚洲成a人不卡在线观看| a级毛片毛片免费观看久潮喷| 免费成人黄色大片| 国产日韩AV免费无码一区二区| 91在线亚洲精品专区| 免费黄色小视频网站| 亚洲人成网站18禁止久久影院 | 亚洲美女在线国产| 99久久国产精品免费一区二区| 亚洲精品中文字幕乱码| 国产成人一区二区三区视频免费| 亚洲成av人片在线观看天堂无码 | 久久精品国产亚洲AV无码偷窥 |