簡單工廠模式

          簡單工廠模式

          簡單工廠模式是類的創建模式,又叫靜態工廠方法模式,它由一個工廠對象決定創建出哪一種產品類的實例。工廠模式專門負責將大量有共同接口的類實例化,可以動態決定將哪一個類實例化,不必事先知道每次要實例化的是哪一個類

          ?

          簡單工廠模式的核心是工廠類,它含有必要的判斷邏輯,可以決定在什么時候創建哪一個產品類的實例。簡單工廠模式一般只有一個工廠方法,如下圖的 Factory 類的 factory() 方法。

          ?

          例:

          public ? class ?Factory{?

          ??????????????
          public ?Factory(){}?

          ??????????????
          public ? static ?art?factory(String?art){?

          ?????????????????????
          if (art.equals(“圓形”))?

          ????????????????????????????
          return ? new ?Round();?

          ?????????????????????
          else ? if (art.equals(“方形”))?

          ????????????????????????????
          return ? new ?Square();?

          ?????????????????????
          else ? if (art.equals(“三角形”))?

          ????????????????????????????
          return ? new ?Triangle();?
          ?????????????????????else?
          ??????????????????????????throw new? Exception("不能創建這樣的對象");

          }?

          }?

          ??

          public ? interface ?Art{?

          ???????
          public ? void ?draw();?

          ???????
          public ? void ?erase();?

          }?

          ??

          public ? class ?Round? implements ?Art{?

          ???????
          public ? void ?draw(){?

          ??????????????System.out.println(“畫一個圓形”);?

          }?

          public ? void ?erase(){?

          ???????System.out.println(“刪除一個圓形”);?

          }?

          }?

          ??

          public ? class ?Square? implements ?Art{?

          ???????
          public ? void ?draw(){?

          ??????????????System.out.println(“畫一個方形”);?

          }?

          public ? void ?erase(){?

          ???????System.out.println(“刪除一個方形”);?

          }?

          }?

          ??

          public ? class ?Triangle? implements ?Art{?

          ???????
          public ? void ?draw(){?

          ??????????????System.out.println(“畫一個三角形”);?

          }?

          public ? void ?erase(){?

          ???????System.out.println(“刪除一個三角形”);?

          }?

          }?

          ??

          public ? class ?Main{?

          ???????
          public ? static ? void ?main(String[]?args){?

          ??????????????Art?art?
          = ?Factory.factory(“圓形”);?

          ??????????????art.draw();?

          }?

          }?


          三種角色:
          ?????????工廠類角色:是簡單工廠方法模式的核心,含有與應用緊密聯系的邏輯。
          ?????????抽象產品
          ?????????具體產品

          posted on 2009-07-02 22:31 luofeng225 閱讀(219) 評論(0)  編輯  收藏 所屬分類: 設計模式

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          公告

          我曾經聽到這么一個故事: 一個年輕的程序員問一個老程序員(一個比較牛逼的公司的CTO) 年輕程序員: 你為什么這么牛X., 就好像沒有你不會的。老程序員: 積累的。年輕程序員: 怎么才能積累到您的程度呢。我每天都在積累。但是似乎都沒有感覺到進步。老程序員: 我從20歲開始做到了一件事情,直到今天,而且盡可能地保證不間斷。年輕程序員: 到底是什么? 老程序員: 我每天保證自己有2個小時在學習新的東西。

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          收藏夾

          Java Website

          java技術博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元氏县| 洱源县| 塔河县| 法库县| 顺昌县| 临汾市| 靖边县| 大连市| 寿阳县| 韶山市| 浦东新区| 崇文区| 英德市| 深水埗区| 昭通市| 珲春市| 兴国县| 台东市| 麻江县| 确山县| 永清县| 佛坪县| 大化| 莲花县| 崇仁县| 邓州市| 星子县| 绿春县| 德阳市| 延边| 无锡市| 舟山市| 梁山县| 长垣县| 玉田县| 抚州市| 拜城县| 澄迈县| 阳曲县| 中宁县| 黄石市|