簡單工廠模式

          簡單工廠模式

          簡單工廠模式是類的創(chuàng)建模式,又叫靜態(tài)工廠方法模式,它由一個工廠對象決定創(chuàng)建出哪一種產(chǎn)品類的實(shí)例。工廠模式專門負(fù)責(zé)將大量有共同接口的類實(shí)例化,可以動態(tài)決定將哪一個類實(shí)例化,不必事先知道每次要實(shí)例化的是哪一個類

          ?

          簡單工廠模式的核心是工廠類,它含有必要的判斷邏輯,可以決定在什么時候創(chuàng)建哪一個產(chǎn)品類的實(shí)例。簡單工廠模式一般只有一個工廠方法,如下圖的 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("不能創(chuàng)建這樣的對象");

          }?

          }?

          ??

          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();?

          }?

          }?


          三種角色:
          ?????????工廠類角色:是簡單工廠方法模式的核心,含有與應(yīng)用緊密聯(lián)系的邏輯。
          ?????????抽象產(chǎn)品
          ?????????具體產(chǎn)品

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

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

          導(dǎo)航

          統(tǒng)計

          公告

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

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          收藏夾

          Java Website

          java技術(shù)博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南召县| 云霄县| 临夏县| 盖州市| 礼泉县| 吴桥县| 兰考县| 张掖市| 民勤县| 洪洞县| 玉田县| 托克逊县| 镇江市| 墨竹工卡县| 拉萨市| 遂平县| 都昌县| 内江市| 来宾市| 武宁县| 澄城县| 基隆市| 临邑县| 东海县| 中超| 石棉县| 当涂县| 蒲江县| 保定市| 南平市| 普定县| 通道| 介休市| 内乡县| 宁远县| 博罗县| 邻水| 宜黄县| 科技| 安宁市| 文登市|