Java beginner design pattern (Factory Method pattern)

Introduction

This is Qiita's first post. I would like to do my best to summarize the 23 types of GoF design patterns that appear in the thorough capture of Java design patterns. We would appreciate it if you could point out not only the mistakes in the content but also other things.

What is the Factory Method pattern?

The Factory Method pattern is a design pattern used when you want to pair one subclass with another. For example, when creating a function to notify the attributes (name, age, address, number of floors of a room) of a certain person (Mr. Suzuki), information about the person (name / age) and information about the place (address / floor of the room) Suppose that you create an abstract class ClassA that notifies "name" and "age", and an abstract class ClassB that notifies "address" and "floor floor of the room" as follows.

ClassA.java


abstract class ClassA{
     abstract String getName();
     abstract int    getAge();
}

ClassB.java


abstract class ClassB{
     abstract String getAddress();
     abstract int getFloor();
}

By inheriting these two abstract classes, you will be able to create a class that notifies each person's information. Example: Class that notifies Mr. Suzuki's information

ClassA1.java


class ClassA1 extends ClassA{
    String getName(){return "Suzuki";}
    int    getAge(){return 28;}
}

ClassB1.java


class ClassB1 extends ClassB{
     String getAddress(){return "Chiba";}
     int    getFloor(){return 1;}
}

Example: Class that notifies Mr. Miyao's information

ClassA2.java


class ClassA2 extends ClassA{
     String getName(){return "Miyao";}
     int    getAge(){return 23;}
}

ClassB2.java


class ClassB2 extends ClassB{
     String getAddress(){return "Tokyo";}
     int    getFloor(){return 2;}
}

By increasing the number of subclasses that inherit the abstract class in this way, it is possible to extend it so that information on various people can be retrieved. However, as the number of subclasses increases, the subclasses that should be used in pairs (ClassA1 and ClassB1 and ClassA2 and ClassB2 in the above example) must be implemented correctly.

The Factory Method pattern is used in such cases. That is, when creating a subclass object that must be used as a pair, instead of calling the constructor of each subclass directly, one of the pairs is created by calling the constructor, and the other is "of the generated pair". Let "one object" create it. To do this, prepare a "method to create a paired subclass object" (factory method) in one class. In this way, the Factory Method pattern is a pattern that prepares a method that creates a pair of objects. If you use the Factory Method pattern, you only need to call the method that creates the paired object, so the implementer does not need to be aware of the combination of subclasses used in the pair. (In the above example, you don't need to know that the ClassA1 pair is ClassB1.)

Applying the Factory Method pattern to this example gives:

ClassA.java


abstract class ClassA{
      abstract String getName();
      abstract int    getAge();
      abstract ClassB createClassB();//Factory method
}

Added a factory method createClassB () to ClassA that creates and returns one subclass of ClassB that is a pair. The factory method implementation is defined in the subclass as follows.

ClassA1.java


class ClassA1 extends ClassA{
     String getName(){return "suzuki";}
     int    getAge(){return 20;}
     ClassB1 createClassB1(){return new ClassB1();}
}

ClassA2.java


class ClassA2 extends ClassA{
     String getName(){return "Miyao";}
     int    getAge(){return 23;}
     ClassB2 createClassB2(){return new ClassB2();}
}

ClassA1 creates a paired ClassB1 object, and ClassA2 creates a paired ClassB2 object. By having one class take charge of creating the object of the paired class in this way, the implementer does not need to be aware of the paired subclass when using ClassA1 and ClassA2.

The main class that uses the created class is as follows.

Main.java


class Main{
   public static void main(String args...){
       ClassA classA = new ClassA2();
 
       ClassB classB = classA.createClassB();
    
       System.out.println(classA.getName());   // Miyao
       System.out.println(classA.getAge());    // 23
       System.out.println(classB.getAddress());// Tokyo
       System.out.println(classB.getFloor());  // 2
   }
}

The implementer simply calls createClassB () and does not need to know that the subclass paired with ClassA2 is ClassB2.

Recommended Posts

Java beginner design pattern (Factory Method pattern)
Design pattern ~ Factory Method ~
Java design pattern
Factory Method pattern
Design pattern ~ Abstract Factory ~
Java design pattern summary
[Design pattern] Java core library
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Java method
java (method)
java beginner 4
java beginner 3
[Java beginner] == operator and equals method
Introduction to Design Patterns (Factory Method)
C # chewed design pattern: Template Method
Java method
[Java] method
[Java] method
Design patterns to enjoy with frequently used Java libraries --Factory pattern
[Beginner] Java method / class / external library [Note 23]
[Design pattern] Common logic with Template Method
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
java callback pattern
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Java8 method reference
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Iterator ~
[Java] forEach method
Java Design Patterns
Java Beginner Exercises
Design pattern ~ Facade ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
java8 method reference
Design pattern ~ Adapter ~
[Java] Random method
[Java] split method
Design pattern ~ Flyweight ~
Java pattern memo
Java Exercise "Beginner"
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Method name of static factory method learned from Java 8
[Java beginner] println method without collection type specification
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
C ++ design pattern (TemplateMethod pattern)
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]