--- tags: Creational Patterns --- # 抽象工廠模式 Abstract Factory ## 大綱 1. 提供一個介面,用來建立相關的物件系列 2. 實作不同的抽象工廠,將提供不同的物件系列,內容以工廠方法做實作。 3. 決定了工廠就等於決定了會取得的物件系列。 ## 結構圖  ## 範例 ```javascript //決定醬料種類 interface Ingredient { public void getSauce(); } //新增兩個醬料的class class TWIngredient implements Ingredient { public void getSauce() { System.out.println("淋上台式醬料!"); } } class ITIngredient implements Ingredient { public void getSauce() { System.out.println("淋上義式醬料"); } } abstract class Restaurant{ public CookMeal mealOrder( ){ CookMeal meal; //處理肉的流程,然後會根據抽象的醬料淋上 ingredient.getSauce(); return meal; } abstract Ingredient getIngredient(); // 定義方法 } //假設有兩家餐廳要使用不同醬料 class TWRestaurant extends Restaurant { @Override public Ingredient getIngredient(){ // 實作方法-台式醬料 Ingredient ingredient = new TWIngredient(); return ingredient; } } class ITRestaurant extends Restaurant { @Override public Ingredient getIngredient(){ // 實作方法 -義士料理 Ingredient ingredient = new ITIngredient(); return ingredient; } } ``` > [name=閔致]
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up