文章出處

最近工作比較忙,所以沒有怎么寫博客,這幾天將集中學習一下(厲風行)講解的設計模式的相關知識,并對主要的代碼進行介紹。

言歸正傳,接下來介紹最簡單也是最基礎的簡單工廠設計模式。

什么是簡單工廠?

簡單工廠模式屬于類的創建型模式,又叫做靜態工廠方法模式。通過專門定義一個類來負責創建其他類的實例,創建的實例通常都具有共同的父類。

假如我們有幾種水果,蘋果,香蕉,我們希望獲得這些水果。

首先我們聲明一個水果接口。

1 public interface Fruit {
2     public void Get();
3 }
View Code

然后我們需要讓蘋果,香蕉的去實現這個接口。

1 public class Apple implements Fruit{
2     public void Get()
3     {
4         System.out.println("Apple");
5     }
6 }
View Code

在主類中我們可以通過,直接new一個對象,來創建蘋果香蕉,然后調用其Get方法。但是這樣做并不是我們想要完成的效果,為了盡可能的降低耦合。

可以通過創建一個水果的簡單工廠,完成水果對象的獲取。

以下的幾種方式均可。

 1 import java.lang.invoke.CallSite;
 2 
 3 public class FruitFactory {
 4 //    public static Fruit GetApple(){
 5 //        return new Apple();
 6 //    }
 7 //    
 8 //    public static Fruit GetBanana(){
 9 //        return new Banana();
10 //    }
11     
12 //    public static Fruit GetFruit(String type)
13 //    {
14 //        if(type.equals("Apple"))
15 //        {
16 //            return new Apple();
17 //        }else if(type.equals("Banana"))
18 //        {
19 //            return new Banana();
20 //        }
21 //        return null;        
22 //    }
23     
24 //    public static Fruit GetFruit(String type) throws InstantiationException, IllegalAccessException
25 //    {
26 //        if(type.equalsIgnoreCase("apple"))
27 //        {
28 //            return (Fruit)Apple.class.newInstance();
29 //        }else if(type.equalsIgnoreCase("banana"))
30 //        {
31 //            return (Fruit)Banana.class.newInstance();
32 //        }else{
33 //            System.out.println("找不到對應的水果");
34 //            return null;
35 //        }    
36 //    }
37     
38     public static Fruit GetFruit(String type) throws InstantiationException, IllegalAccessException
39     {
40         try {
41             Class fruit=Class.forName(type);
42             return (Fruit)fruit.newInstance();
43         } catch (ClassNotFoundException e) {
44             // TODO Auto-generated catch block
45             e.printStackTrace();
46         }
47         return null;
48     }
49 }
View Code

這樣主方法,也就是我們的客戶端,就可以通過直接給出要獲得的對象的名稱,直接獲得想要的對象。

 1 public class MainClass {
 2     public static void main(String[] args) throws InstantiationException, IllegalAccessException {
 3 //        Fruit apple=FruitFactory.GetApple();
 4 //        Fruit banana=FruitFactory.GetBanana();
 5 //        apple.Get();
 6 //        banana.Get();
 7         
 8         Fruit apple=FruitFactory.GetFruit("Apple");
 9         Fruit banana=FruitFactory.GetFruit("Banana");
10         apple.Get();
11         banana.Get();
12     }
13 }
View Code

水果工廠就是一個簡單工廠,如果我們這時候要創建葡萄類,只需讓其實現Fruit接口即可,這是在客戶端就可以通過葡萄對象的名稱,獲得葡萄對象,而在葡萄類中的相關功能,在主客戶端中都不用考慮。

簡單工廠的優點:

在這個模式中,工廠類是整個模式的關鍵所在。它包含必要的判斷邏輯,能夠根據外界給定的信息,決定究竟應該創建哪個具體類的對象。用戶在使用時可以直接根據工廠類去創建所需的實例,而無需了解這些對象是如何創建以及如何組織的。有利于整個軟件體系結構的優化。

缺點:

簡單工廠模式的缺點也正體現在其工廠類上,由于工廠類集中了所有實例的創建邏輯,所以“高內聚”方面做的并不好。另外,當系統中的具體產品類不斷增多時,可能會出現要求工廠類也要做相應的修改,拓展性不是很好。

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()