java常用設(shè)計模式_第1頁
java常用設(shè)計模式_第2頁
java常用設(shè)計模式_第3頁
免費(fèi)預(yù)覽已結(jié)束,剩余4頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、工廠模式細(xì)分有三種,分別為:簡單工廠模式、工廠方法模式和抽象工廠模式。(現(xiàn)單個的講,最后再講這三個的區(qū)別)這篇文章主要通過一個農(nóng)場的實(shí)例來講解,這也是 java 與模式書中的例子,只不過我對一些部分進(jìn)行了簡化,一些部分進(jìn)行了擴(kuò)充,以幫助理解例子如下: 有一個農(nóng)場公司,專門向市場銷售各類水果有如下水果:葡萄( grape)草莓( strawberry )蘋果( apple)/*1 、簡單工廠模式 */這個比較簡單,寫一下源代碼源代碼中給出了必須的注釋代碼比書上的要簡單一些,排版也好看一些,只是為了讓新手更好的理解Fruit.java:/* 水果與其它植物相比有一些專門的屬性,以便與農(nóng)場的* 其它

2、植物區(qū)分開這里的水果假設(shè)它必須具備的方法:* 生長 grow() 收獲 harvest() 種植 plant()*/public interface Fruitvoid grow();void harvest();void plant();/*面是 Apple 類的函數(shù) Apple.java:*/* 蘋果是水果類的一種,因此它必須實(shí)現(xiàn)水果接口的所有方法即* grow()harvest()plant() 三個函數(shù)另外,由于蘋果是多年生植物,* 所以多出一個 treeAge 性質(zhì),描述蘋果的樹齡*/public class Apple implements Fruitprivate int tre

3、eAge;public void grow() / 蘋果的生長函數(shù)代碼 public void harvest() / 蘋果的收獲函數(shù)代碼 public void plant() / 蘋果的種植函數(shù)代碼 public int getTreeAge() return treeAge; public void setTreeAge(int treeAge) this.treeAge = treeAge; /*/*下面是 Grape 類的函數(shù) Grape.java:*/* 葡萄是水果類的一種,因此它必須實(shí)現(xiàn)水果接口的所有方法即* grow()harvest()plant() 三個函數(shù)另外,由于葡萄分

4、為有籽和無籽* 兩種,因此多出一個 seedless 性質(zhì),描述葡萄有籽還是無籽 */public class Grape implements Fruitprivate boolean seedless;public void grow() / 葡萄的生長函數(shù)代碼 public void harvest() / 葡萄的收獲函數(shù)代碼 public void plant() / 葡萄的種植函數(shù)代碼 public boolean getSeedless() return seedless;public void setSeedless(boolean seedless) this.seedless

5、 = seedless;*下 面 是 Strawberry 類 的 函 數(shù)Strawberry.java:*/ * 草莓是水果類的一種,因此它必須實(shí)現(xiàn)水果接口的所有方法即* grow()harvest()plant() 三個函數(shù)另外,這里假設(shè)草莓分為大棚草莓和一般*草莓(即沒有棚的草莓)因此草莓比一般水果多出一個性質(zhì)coteless,描述草莓* 是大棚草莓還是沒有大棚的草莓*/ public class Strawberry implements Fruitprivate boolean coteless;public void grow() / 草莓的生長函數(shù)代碼 public void h

6、arvest() / 草莓的收獲函數(shù)代碼 public void plant() / 草莓的種植函數(shù)代碼 public boolean getCoteless() return coteless; public void setCoteless(boolean coteless) this. coteless = coteless; FruitGardener 類農(nóng)場的園丁也是系統(tǒng)的一部分,自然要有一個合適的類來代表,我們用apple)來表示 FruitGardener 類會根據(jù)客戶端的要求,創(chuàng)建出不同的水果對象,比如蘋果( 葡萄(grape)或草莓(strawberry )的實(shí)例代碼如下所示

7、:FruitGardener.java:/* 通過下面的表態(tài)工廠方法,可以根據(jù)客戶的需要,創(chuàng)建出不同的水果對象*如果提供的參數(shù)是 apple則通過return new Apple()創(chuàng)建出蘋果實(shí)例*如果是提供的參數(shù)是grape則創(chuàng)建葡萄實(shí)例,這正是簡單工廠方法之精髓*/public class FruitGardenerpublic static Fruit factory(String which) throws BadFruitExceptionif (which.equalsIgnoreCase(apple) return new Apple(); else if (which.equa

8、lsIgnoreCase(strawberry) return new Strawberry(); else if (which.equalsIgnoreCase(grape) return new Grape(); else throw new BadFruitException(Bad fruit request); 簡單工廠方法的優(yōu)點(diǎn)是當(dāng)在系統(tǒng)中引入新產(chǎn)品時不必修改客戶端,但需要個修改工廠 類,將必要的邏輯加入到工廠類中工廠方法模式就克服了以上缺點(diǎn),下面談?wù)劰S 方法模式/*2 、工廠方法模式 */由于水果接口以及 grape類strawberry類apple類的代碼都和上面的一樣,所以

9、下 面相關(guān)的源碼去掉了注釋Fruit.java:public interface Fruitvoid grow();void harvest();void plant();Apple.java:public class Apple implements Fruitprivate int treeAge;public void grow() / 蘋果的生長函數(shù)代碼 public void harvest() / 蘋果的收獲函數(shù)代碼 public void plant() / 蘋果的種植函數(shù)代碼 public int getTreeAge() return treeAge; public void

10、 setTreeAge(int treeAge) this.treeAge = treeAge; Grape.java:public class Grape implements Fruitprivate boolean seedless;public void grow() / 葡萄的生長函數(shù)代碼 public void harvest() / 葡萄的收獲函數(shù)代碼 public void plant() / 葡萄的種植函數(shù)代碼 public boolean getSeedless() return seedless; public void setSeedless(boolean seedl

11、ess) this.seedless = seedless; Strawberry.java:public class Strawberry implements Fruitprivate boolean coteless;public void grow() / 草莓的生長函數(shù)代碼 public void harvest() / 草莓的收獲函數(shù)代碼 public void plant() / 草莓的種植函數(shù)代碼 public boolean getCoteless() return coteless; public void setCoteless(boolean coteless) thi

12、s. coteless = coteless; * 下面的源碼就是工廠方法模式的重點(diǎn)了,在簡單工廠模式中,將這里將 FruitGardener 定 義為一個類,即園丁要管理園里的所有水果,如果園丁哪天病了,水果都不能管理了 在工廠方法模式中將 FruitGardener 定義為一個接口,而將管理水果的角色劃分得更細(xì), 比如有葡萄園丁草莓園丁蘋果園丁等等具體角色實(shí)現(xiàn) FruitGardener 接口的工廠 方法源碼如下所示:接口 FruitGardener 的源碼:public interface FruitGardenerFruit factory();蘋果園丁類 AppleGardener.

13、java 的源碼:public class AppleGardener implements FruitGardenerpublic Fruit factory() return new Apple(); 葡萄園丁類 GrapeGardener.java 的源碼:public class GrapeGardener implements FruitGardener public Fruit factory() return new Grape(); 草莓園丁類 StrawberryGardener.java 的源碼: public class StrawberryGardener implem

14、ents FruitGardener public Fruit factory() return new Strawberry(); 由以上源碼可以看出,使用工廠方法模式保持了簡單工廠模式的優(yōu)點(diǎn),克服了其缺點(diǎn) 當(dāng)在系統(tǒng)中引入新產(chǎn)品時,既不必修改客戶端,又不必修改具體工廠角色可以較好 的對系統(tǒng)進(jìn)行擴(kuò)展*/現(xiàn)在工廠再次大發(fā)展,要引進(jìn)塑料大棚技術(shù),在大棚里種植熱帶( Tropical )和亞熱帶的 水果和蔬菜(Veggie)其中水果分為 TropicalFruit 和 NorthernFruit,蔬菜分為 TropicalVeggie 和 NorthernVeggie 園丁包括 TropicalGa

15、rdener 和 NorthernGardener 也就是說, TropicalGardener 專門管理 TropicalFruit 和 TropicalGardener, NorthernGardener 專門/* 管理 NorthernFruit 和 NorthernVeggie 抽象工廠模式在這個例子中的源碼*Fruit.java:public interface Fruit NorthernFruit.java:public class NorthernFruit implements Fruitprivate String name;public NorthernFruit(Str

16、ing name) public String getName() return name; public void setName(String name) = name; TropicalFruit.java:public class TropicalFruit implements Fruitprivate String name;public TropicalFruit(String name) public String getName() return name; public void setName(String name) = name

17、; Veggie.java:public interface Veggie TropicalVeggie.java:public class TropicalVeggie implements Veggie private String name;public TropicalVeggie(String name) public String getName() return name; public void setName(String name) = name; NorthernVeggie.java:public class NorthernVeggie imple

18、ments Veggie private String name;public NorthernVeggie(String name) public String getName() return name; public void setName(String name) = name; Gardener.java:public interface Gardener Fruit createFruit(String name);Veggie createVeggie(String name); TropicalGardener.java:public class Trop

19、icalGardener implements Gardener public Fruit createFruit(String name) return new TropicalFruit(name); public Veggie createVeggie(String name) return new TropicalVeggie(name); NorthernGardener.java:public class NorthernGardener implements Gardener public Fruit createFruit(String name) return new NorthernFruit(name); public Veggie createVeggie(String name) return new NorthernVeggie(name); 為了簡單起見,這里只講一下增加新產(chǎn)品(族)時該系統(tǒng)如何擴(kuò)展(關(guān)于產(chǎn)品族相關(guān)知識,請看此書的相關(guān)章節(jié),不過不懂產(chǎn)品族也沒有關(guān)系,這里寫得很簡單,肯定能 看懂)比如現(xiàn)在要增加

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論