2015-01-15 203 views
0

the documentation about DAO說:如何正確將工廠方法應用於DAO工廠?

當底層存儲不受從一個 實現改變到另一個,這種策略可以使用本 工廠方法模式,以產生數目由所需要的DAO的實施 申請。這種情況的類圖如圖9.3所示。

下圖9.3本身:

enter image description here

是什麼原因導致的誤解是,我們怎能申請工廠方法生產DAO小號?據我瞭解,在這種情況下,DAO可能看起來像

public interface DAOFactory{ 

    public DAO1 getDAO1(); 
    public DAO2 getDAO2(); 

} 

但它不是一種工廠方法。這將是一個抽象工廠,因爲我們生產的是一系列對象而不是單個對象。你不能應歸結爲時說

這種策略可以使用 Factory Method模式產生數由 應用程序所需的DAO來實現他們的意思。這種情況的類圖如圖9.3所示。

+0

的DAOFActory shoudl有1種方法類似這樣的公共DAOBaseClass getDAO(BaseEntity實體),返回DAO實例爲每個實體 – StanislavL

+0

嘛。 ..您的界面DAOFactory是一個[抽象工廠](http://en.wikipedia.org/wiki/Abstract_factory_pattern),它聲明瞭兩個[工廠方法](http://en.wikipedia.org/wiki/Factory_method_pattern) 。 – Seelenvirtuose

+0

@StanislavL那麼當他們談到Factory方法時他們是什麼意思? –

回答

1

你是對的。這是誤導。 圖片描述使用工廠方法肯定是抽象工廠設計模式的一個例子。工廠只有一個實施。

有一個工廠方法和抽象工廠這裏的區別一個很好的解釋:Differences between Abstract Factory Pattern and Factory Method

文檔中所示的圖描繪了抽象工廠模式。

我能想象下使用的DAO工廠方法

// the Service contains the business logic and uses DAOs to access data 
class BaseService { 

    public Object doSomething() { 
     return getDao().retrieveObject("identifier"); 
    } 

    // Factory method producing DAOs 
    public IDao getDao() { 
     return new BaseDao(); 
    } 

} 

// The DAO interface 
interface IDao { 
    Object retrieveObject(String identifier); 
} 


// Another Service that overwrite the type of produced DAOs 
class SmartService extends Service { 

    // overwrite the factory method 
    public IDao getDao() { 
     return new SmartDao(); 
    } 
}