Abstract Factory
Abstract Factory Pattern에 대해 설명하는 페이지입니다.
Abstract Factory
Tags
Design Pattern, Java
Introduction
- Purpose
- Provide an interface that delegates creation calls to one or more concrete classes in order to deliver specific objects.
- Use When
- The creation of objects should be independent of the system utilizing them.
- Systems should be capable of using multiple families of objects.
- Families of objects must be used together.
- Libraries must be published without exposing implementation details.
- Concrete classes should be decoupled from clients.
- Characteristics
- Delegates object creation to a factory object.
- Uses composition and delegation.
Participants
- AbstractFactory
- Declares an interface for operations that create abstract product objects.
- ConcreteFactory
- Implements the operations to create product objects.
- AbstractProduct
- Declares an interface for a type of product object.
- ConcreteProduct
- Defines a product object to be created by the corresponding concrete factory.
- Implements the AbstractProduct interface.
- Client
- Uses only interfaces declared by AbstractFactory and AbstractProduct classes.
How to Use (Example)
Abstract Factory
1 2 3 4 5
public interface IngredientFactory { public IngredientA createIngredientA(); public IngredientB createIngredientB(); public IngredientC createIngredientC(); }
Concrete Factory
1 2 3 4 5 6 7 8 9 10 11 12 13
public class MyIngredientFactory implements IngredientFactory { public IngredientA createIngredientA() { return new MyIngredientA(); } public IngredientB createIngredientB() { return new MyIngredientB(); } public IngredientC createIngredientC() { return new MyIngredientC(); } }
Abstract Product
1 2 3 4 5 6 7
public abstract class Product { IngredientA ingredientA; IngredientB ingredientB; IngredientC ingredientC; public abstract void prepare(); }
Concrete Product
1 2 3 4 5 6 7 8 9 10 11 12 13
public class MyProduct extends Product { MyIngredientFactory myIngredientFactory; public MyProduct(MyIngredientFactory myIngredientFactory) { this.myIngredientFactory = myIngredientFactory; } public void prepare() { ingredientA = myIngredientFactory.createIngredientA(); ingredientB = myIngredientFactory.createIngredientB(); ingredientC = myIngredientFactory.createIngredientC(); } }
This post is licensed under CC BY 4.0 by the author.