instantiate. Factory Method lets a class defer instantiation to subclasses. (為建立的物件定一個介面,但是是讓子類別去決定要讓哪一個類別要實體化,工廠方法讓一個類別的實體化推遲到子類別。)
Factory method pattern依靠繼承(inheritance),將建立object的工作委託給子類別實作的factory method。
參與角色(Participants)
- Product(抽象產品):定義了factory method要建立object的interface,定義這類產品有甚麼方法。
- ConcreteProduct(具體產品):實作(implement)Product interface。
- Creator(抽象工廠):宣告factory method,這個method要回傳一個Product type。Creator也可以定義一個預設的factory method,回傳一個預設的ConcreteProduct object。
- ConcreteCreator(實體工廠):override factory method,實作要回傳哪一種ConcreteProduct instance,包含了實體化object的business logic。
簡單的Java實作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConcreteCreatorA implements Creator { | |
@Override | |
public Product createProdut(){ | |
return new ProductA(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConcreteCreatorB implements Creator { | |
@Override | |
public Product createProdut(){ | |
return new ProductB(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Creator { | |
// this is a Factory Method. | |
Product createProdut(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Product { | |
String getProductName(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductA implements Product{ | |
public String getProductName() { | |
return "Product A"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductB implements Product{ | |
public String getProductName() { | |
return "Product B"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestFactoryMethod{ | |
public static void main(String[] args) { | |
ConcreteCreatorA creatorA = new ConcreteCreatorA(); | |
ConcreteCreatorB creatorB = new ConcreteCreatorB(); | |
Product productA = creatorA.createProdut(); | |
Product productB = creatorB.createProdut(); | |
} | |
} |
好處:
- 客戶只需要知道如何使用factory method來建立Product object,不需要關心要如何建立。
- 如果建立這個object很複雜的話,可以將建立的過程隱藏起來。
- 很容易的增加Product類別,假設要增加一個ProductC,只要實作ProductC,以及ConcreteCreatorC就可以了。
壞處:
- 類別數增加,增加系統開銷。
沒有留言:
張貼留言