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


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


    網站導航:
     
    主站蜘蛛池模板: 一级黄色毛片免费看| 精品在线免费视频| 成全视频免费观看在线看| 亚洲乱码国产一区网址| www成人免费视频| 亚洲愉拍99热成人精品热久久| 无忧传媒视频免费观看入口| 亚洲成?v人片天堂网无码| 色屁屁在线观看视频免费| 亚洲成A人片在线观看中文| 免费无码国产V片在线观看| 亚洲欧洲日产国码高潮αv| 国产精品免费久久久久久久久| 曰韩亚洲av人人夜夜澡人人爽| 日韩av无码免费播放| 亚洲成a人片在线观看中文动漫| 99精品热线在线观看免费视频| 亚洲视频免费观看| 成人一a毛片免费视频| 国产精品亚洲AV三区| 国产亚洲午夜高清国产拍精品| 99久久免费国产特黄| 337p日本欧洲亚洲大胆艺术| 国产美女在线精品免费观看| 韩国亚洲伊人久久综合影院| 国产av无码专区亚洲国产精品| 日韩免费电影网址| 亚洲中文字幕久久久一区| 亚洲无线一二三四区手机| 最近中文字幕mv免费高清在线| 中文字幕在线日亚洲9| 亚洲色偷偷色噜噜狠狠99网| 免费真实播放国产乱子伦| 久久精品免费一区二区三区| 亚洲一区二区免费视频| 免费亚洲视频在线观看| 久久香蕉国产线看免费| 在线观看亚洲专区| 少妇中文字幕乱码亚洲影视| 免费黄色网址入口| 国产情侣久久久久aⅴ免费|