Abstract Factoryãã¿ã¼ã³
Head First ãã¶ã¤ã³ãã¿ã¼ã³ã®ã¡ã¢ã
Abstract Factoryãã¿ã¼ã³
å ·è±¡ã¯ã©ã¹ãæå®ãããã¨ãªããä¸é£ã®é¢é£ãªãã¸ã§ã¯ãã
ä¾åãªãã¸ã§ã¯ããä½æããããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãæä¾ããã
Abstract Factoryã¨Factory Method
å¤ãã®å ´åAbstract Factoryã®ã¡ã½ããã¯Factory Methodã¨ãã¦å®è£
ãããã
Abstract Factoryã¯ä¸é£ã®å
·è±¡ã¯ã©ã¹ãä½æããå½¹å²ãæ
ãããããã®å®è£
ã«ã¯Factory Methodã¯ãã£ã¦ã¤ãã®æ¹æ³ã
- Abstract Factory
ã°ã«ã¼ãããªãä¸é£ã®ã¯ã©ã¹ã使ç¨ãããå ´åã«ã¤ããã
Abstract Factoryã¯ãªãã¸ã§ã¯ãã³ã³ãã¸ã·ã§ã³ãå©ç¨ããããªãã¸ã§ã¯ãçæããã¡ã¯ããªã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ã¡ã½ããã«å®è£
ããã
- Factory Method
ã¤ã³ã¹ã¿ã³ã¹åããå¿
è¦ã®ããå
·è±¡ã¯ã©ã¹ããã¯ã©ã¤ã¢ã³ãã³ã¼ããåé¢ããå ´åããå¿
è¦ã¨ãªãã¯ã©ã¹ãäºåã«ããããªãå ´åã«ã¤ããã
Factory Methodã¯ç¶æ¿ãå©ç¨ããããªãã¸ã§ã¯ãçæããã¡ã¯ããªã¡ã½ãããå®è£
ãããµãã¯ã©ã¹ã«å§è²ããã
Abstract Factoryã®å®è£
ãã¶åºã®ä¸é£ã®é£æãå®è£ ããå ´å
// å ¨ã¦ã®é£æãä½æããã¤ã³ã¿ã¼ãã§ã¼ã¹ public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); } // ãã¥ã¼ã¨ã¼ã¯é£æãã¡ã¯ã㪠public class NYPizzaIngredientFactory extends PizzaIngredientFactory { public Dough createDough() { return new ThinCrustDough(); } public Sauce createSauce() { return new MarinaraSauce(); } public Sauce createCheese() { return new ReggianoCheese(); } } //æ½è±¡Pizzaã¯ã©ã¹ãç¶æ¿ãããã¼ãºã㶠public class CheezePizza extends Pizza { PizzaIngredientFactory ingredientFactory; public CheezePizza(PizzaIngredientFactory ingredientFactory) { // é£æãã¡ã¯ããªãæ ¼ç´ãã this.ingredientFactory = ingredientFactory; } void prepare() { // é£æãç²å¾ãã dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); } }