模式筆記一:如何學(xué)習(xí)模式

          啥叫模式? Patterns in solutions come from patterns in problems.
          針對(duì)某一類(lèi)經(jīng)常出現(xiàn)的問(wèn)題所采取的行之有效的解決方案
          "A pattern is a solution to a problem in a context."

          "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice."(Christopher Alexander -- A Pattern Language)

          模式的四個(gè)基本要素:
          1. 模式名稱(chēng)pattern name
          2. 問(wèn)題problem
          3. 解決方案solution
          4. 效果consequences

          如何描述設(shè)計(jì)模式(十大特點(diǎn))
          1. 意圖:描述,別名
          2. 動(dòng)機(jī):場(chǎng)景
          3. 適用性: 什么情況下
          4. 結(jié)構(gòu): 類(lèi)圖, 序列圖
          5. 參考者
          6. 協(xié)作
          7. 效果
          8. 實(shí)現(xiàn)
          9. 應(yīng)用
          10. 相關(guān)模式

          在實(shí)踐中學(xué)習(xí)是最佳的方式, 所以先要掌握每個(gè)模式的十大特點(diǎn),更加重要的是在實(shí)際應(yīng)用中學(xué)習(xí), 在水中學(xué)會(huì)游泳
          以迭代器模式為例, Java中有一個(gè)Iterator接口
           1 public interface Iterator
           2 {
           3   /**
           4    * Tests whether there are elements remaining in the collection. In other
           5    * words, calling <code>next()</code> will not throw an exception.
           6    *
           7    * @return true if there is at least one more element in the collection
           8    */
           9   boolean hasNext();
          10 
          11   /**
          12    * Obtain the next element in the collection.
          13    *
          14    * @return the next element in the collection
          15    * @throws NoSuchElementException if there are no more elements
          16    */
          17   Object next();
          18 
          19   /**
          20    * Remove from the underlying collection the last element returned by next
          21    * (optional operation). This method can be called only once after each
          22    * call to <code>next()</code>. It does not affect what will be returned
          23    * by subsequent calls to next.
          24    *
          25    * @throws IllegalStateException if next has not yet been called or remove
          26    *         has already been called since the last call to next.
          27    * @throws UnsupportedOperationException if this Iterator does not support
          28    *         the remove operation.
          29    */
          30   void remove();
          31 }
          32 
          假如你的類(lèi)中有一些聚集關(guān)系, 那么考慮增加一個(gè)iterator方法,以實(shí)現(xiàn)下面這個(gè)接口
          public interface Iterable
          {
            
          /**
             * Returns an iterator for the collection.
             *
             * 
          @return an iterator.
             
          */
            Iterator iterator ();
          }

          返回你自己實(shí)現(xiàn)的ConcreteIterator類(lèi), 這個(gè)ConcreteIterator當(dāng)然是實(shí)現(xiàn)了Iterator接口的
          你會(huì)發(fā)現(xiàn)在遍歷和迭代類(lèi)中的這個(gè)成員的聚集元素時(shí)會(huì)有不同的感覺(jué), 因?yàn)檫@個(gè)Iterator與實(shí)現(xiàn)是分離的.
          你的類(lèi)終歸是給自己或別人使用的,在調(diào)用者的眼里, 非常簡(jiǎn)單, 管你里面是怎么實(shí)現(xiàn)的呢,
          反正我知道你能給我一個(gè)迭代器就夠了, 這里面就體現(xiàn)了面向接口編程的好處. 也就是按契約編程

          posted on 2008-08-10 21:07 fantasyamin 閱讀(258) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 板桥市| 蓬莱市| 儋州市| 兴和县| 曲靖市| 鹤壁市| 新干县| 巴楚县| 双流县| 上思县| 镇远县| 沐川县| 综艺| 昌宁县| 南江县| 信宜市| 镇江市| 邯郸市| 镇安县| 延津县| 怀宁县| 天祝| 渭南市| 奉新县| 英吉沙县| 淮滨县| 杭锦后旗| 阿克苏市| 乌恰县| 阳西县| 湖州市| 桂林市| 时尚| 巩义市| 迁西县| 客服| 青岛市| 九寨沟县| 乐都县| 文山县| 贵州省|