팩토리 메서드 패턴
위키백과, 우리 모두의 백과사전.
UML로 표현된 팩토리 메서드
LePUS3로 표현된 펙토리 메서드
팩토리 메서드 패턴(Factory method pattern)은 객체지향 디자인 패턴이다. 다른 생성 패턴들처럼, 이 패턴도 생성하려는 객체의 클래스를 정확히 지정하지 않으면서 객체를 만드는 문제를 다룬다. 팩토리 메서드 패턴은 이 문제를 객체를 만드는 또 다른 메서드를 정의하여 처리한다. 일반적으로, 팩토리 메서드라는 용어는 객체를 만드는 것이 주 목적인 메서드를 지칭하는데 주로 사용한다.
목차 |
예[편집]
C#[편집]
Pizza example:
public abstract class Pizza { public abstract decimal GetPrice(); public enum PizzaType { HamMushroom, Deluxe, Seafood } public static Pizza PizzaFactory(PizzaType pizzaType) { switch (pizzaType) { case PizzaType.HamMushroom: return new HamAndMushroomPizza(); case PizzaType.Deluxe: return new DeluxePizza(); case PizzaType.Seafood: return new SeafoodPizza(); } throw new System.NotSupportedException("The pizza type " + pizzaType.ToString() + " is not recognized."); } } public class HamAndMushroomPizza : Pizza { private decimal price = 8.5M; public override decimal GetPrice() { return price; } } public class DeluxePizza : Pizza { private decimal price = 10.5M; public override decimal GetPrice() { return price; } } public class SeafoodPizza : Pizza { private decimal price = 11.5M; public override decimal GetPrice() { return price; } } // Somewhere in the code ... Console.WriteLine( Pizza.PizzaFactory(Pizza.PizzaType.Seafood).GetPrice().ToString("C2") ); // $11.50 ...
자바스크립트[편집]
Pizza example:
//Our pizzas function HamAndMushroomPizza(){ var price = 8.50; this.getPrice = function(){ return price; } } function DeluxePizza(){ var price = 10.50; this.getPrice = function(){ return price; } } function SeafoodPizza(){ var price = 11.50; this.getPrice = function(){ return price; } } //Pizza Factory function PizzaFactory(){ this.createPizza = function(type){ switch(type){ case "Ham and Mushroom": return new HamAndMushroomPizza(); case "DeluxePizza": return new DeluxePizza(); case "Seafood Pizza": return new SeafoodPizza(); default: return new DeluxePizza(); } } } //Usage var pizzaPrice = new PizzaFactory().createPizza("Ham and Mushroom").getPrice(); alert(pizzaPrice);
루비[편집]
Pizza example:
class HamAndMushroomPizza def price 8.50 end end class DeluxePizza def price 10.50 end end class SeafoodPizza def price 11.50 end end class PizzaFactory def create_pizza(style='') case style when 'Ham and Mushroom' HamAndMushroomPizza.new when 'DeluxePizza' DeluxePizza.new when 'Seafood Pizza' SeafoodPizza.new else DeluxePizza.new end end end # usage factory = PizzaFactory.new pizza = factory.create_pizza('Ham and Mushroom') pizza.price #=> 8.5 # bonus formatting formatted_price = sprintf("$%.2f", pizza.price) #=> "$8.50" # one liner sprintf("$%.2f", PizzaFactory.new.create_pizza('Ham and Mushroom').price) #=> "$8.50"
파이썬[편집]
# Our Pizzas class Pizza: HAM_MUSHROOM_PIZZA_TYPE = 0 DELUXE_PIZZA_TYPE = 1 SEAFOOD_PIZZA_TYPE= 2 def __init__(self): self.__price = None def getPrice(self): return self.__price class HamAndMushroomPizza(Pizza): def __init__(self): self.__price = 8.50 class DeluxePizza(Pizza): def __init__(self): self.__price = 10.50 class SeafoodPizza(Pizza): def __init__(self): self.__price = 11.50 class PizzaFactory: def createPizza(self, pizzaType): if pizzaType == Pizza.HAM_MUSHROOM_PIZZA_TYPE: return HamAndMushroomPizza() elif pizzaType == Pizza.DELUXE_PIZZA_TYPE: return DeluxePizza() elif pizzaype == Pizza.SEAFOOD_PIZZA_TYPE: return SeafoodPizza() else: return DeluxePizza() # Usage pizzaPrice = PizzaFactory().createPizza(Pizza.HAM_MUSHROOM_PIZZA_TYPE).getPrice() print "$%.02f" % pizzaPrice
| 이 글은 컴퓨터 과학에 관한 토막글입니다. 서로의 지식을 모아 알차게 문서를 완성해 갑시다. |