tory320

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            10 隨筆 :: 0 文章 :: 1 評(píng)論 :: 0 Trackbacks

          Design Principle
          Identify the aspects of your application that vary and separate them from what stays the same.
          Here's another way to think about this principle: take the parts that vary and encapsulate them, so that later you can alter or extends the parts that vary without affecting those that don't.
          As simple as this concept is, it forms the basis for almost every design pattern. All patterns provide a way to let some part of a system vary independently of all other parts.

          Each set of class will hold all the implementations of their respective behavior. For instance, we might have one clss that implements quarking, another implements squaking, and another that implements silence.

          To separate thest behaviors from the Duck class, we'll pull both methods out of the duck class and create a new set of class to represent each behavior.

          This is in contrast to the way we were doing things before, where a behavior either came from a concrete implementation in the suprerclass Duck, or by providing a specialized implementation in the sub class itself. In both cases we were relying on an implementation. We were locked into using that specific implemetation and there was no room for changing out the behavior.

          And the same is true for the duck's flying behavior.

          Okay, now that we've done the deep dive on the duck simulator design, it's time to come back up for air and take a look at the big picture.

          Below is the entire reworked class structure. We have everything you'd expect: ducks extending Duck. fly behavior implementing FlyBehavior and quack behavior implementing QuackBehavior.

          Notice also that we've started to describe things a little differntly. Instead of thinking of the duck behaviors as a set of behaviors, we'll start thinking of them ad a family of algorithms. Think about it: in the SimUDuck design, the algorithms represent things a duck would do , but we could just as easily use the same techniques for a set of classes that implement the ways to compute state sales tax by different states.

          posted on 2008-03-07 17:58 tory 閱讀(138) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 敖汉旗| 田林县| 咸宁市| 称多县| 砀山县| 繁昌县| 徐闻县| 贵溪市| 滨海县| 玉屏| 江口县| 宁阳县| 鸡东县| 泽普县| 兴宁市| 同德县| 沾化县| 德令哈市| 沁阳市| 修文县| 石楼县| 安徽省| 揭阳市| 岳池县| 青龙| 锡林浩特市| 罗平县| 科尔| 曲水县| 渭源县| 石河子市| 应用必备| 建昌县| 扎兰屯市| 郎溪县| 突泉县| 炉霍县| 铅山县| 秦安县| 英山县| 宜兰市|