Post

Factory Method Pattern

Factory Method Pattern에 대해 설명하는 페이지입니다.

Factory Method Pattern

Tags
Design Pattern, Java

Introduction

  • Purpose
    • Exposes a method for creating objects, allowing subclasses to control the actual creation process.
  • Use When
    • A class will not know what classes it will be required to create.
    • Subclasses may specify what objects should be created.
    • Parent classes wish to defer creation to their subclasses.
  • Characteristics
    • Uses inheritance to decide the object to be instantiated.
    • Defines an interface for creating an object, but lets subclasses decide which class to instantiate.
    • Lets a class defer instantiation to subclasses.

How to Use (Example)

  • Product

    1
    2
    3
    4
    5
    6
    7
    
    public abstract class MyProduct {
        String name;
    
        public String getName() {
            return name;
        }
    }
    
  • Concrete Products

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    public class ProductA extends MyProduct {
        public ProductA() {
            name = "ProductA";
        }
    }
    
    public class ProductB extends MyProduct {
        public ProductB() {
            name = "ProductB";
        }
    }
    
    public class ProductC extends MyProduct {
        public ProductC() {
            name = "ProductC";
        }
    }
    
    public class ProductD extends MyProduct {
        public ProductD() {
            name = "ProductD";
        }
    }
    
  • Creator

    1
    2
    3
    4
    5
    6
    7
    8
    
    public abstract class MyCreator {
        public MyProduct orderProduct(String type) {
            Product product = createProduct(type);
            return product;
        }
    
        protected abstract MyProduct createProduct(String type);
    }
    
  • Concrete Creators

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    public class CreatorAB extends MyCreator {
        public MyProduct createProduct(String type) {
            Product product;
            if (type.equals("A")) {
                product = new ProductA();
            } else if (type.equals("B")) {
                product = new ProductB();
            }
            return product;
        }
    }
    
    public class CreatorCD extends MyCreator {
        public MyProduct createProduct(String type) {
            Product product;
            if (type.equals("C")) {
                product = new ProductC();
            } else if (type.equals("D")) {
                product = new ProductD();
            }
            return product;
        }
    }
    
This post is licensed under CC BY 4.0 by the author.