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


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


    網站導航:
     
    主站蜘蛛池模板: 久青草视频97国内免费影视| 暖暖免费中文在线日本| 亚洲黄色在线观看网站| 亚洲中文字幕无码av在线| 亚洲国产系列一区二区三区| 免费在线观看一区| av无码东京热亚洲男人的天堂| 国产aⅴ无码专区亚洲av麻豆 | 亚洲尹人九九大色香蕉网站| 亚洲色大成网站www久久九| 草久免费在线观看网站| 在免费jizzjizz在线播| 四虎免费影院4hu永久免费| 亚洲AV无码国产精品麻豆天美 | 男女猛烈无遮掩视频免费软件| 四虎影视免费永久在线观看| 美女黄频免费网站| 一级毛片在线观看免费| 亚洲Av无码乱码在线znlu| 91亚洲国产成人久久精品| a级成人毛片免费图片| 免费观看美女裸体网站| 亚洲阿v天堂在线| 精品在线视频免费| 性生交片免费无码看人| 亚洲精品夜夜夜妓女网| 亚洲av无码专区在线电影天堂 | 日本人的色道免费网站| 亚洲中文字幕第一页在线| 免费精品99久久国产综合精品| 亚洲成a人片在线观看久| 中国一级特黄的片子免费| 亚洲高清最新av网站| 三上悠亚在线观看免费| 亚洲国产成人超福利久久精品| 国产精品无码一二区免费| 亚洲欧美中文日韩视频| 成年女人免费视频播放体验区 | 噜噜综合亚洲AV中文无码| 亚洲一区二区女搞男| igao激情在线视频免费|