[JAVA] Introduction to Design Patterns (Factory Method)

This article summarizes the Factory Method. According to wikipedia, "By replacing the constructors of other classes with your own methods that can be overridden by subclasses, you can push application-specific object creation out to subclasses and improve class reusability." Reference: Factory Method pattern

** Main characters **

NO name role
1 Author(Creator) Define an interface for instantiating a product
2 Creator's concrete class Instantiate the product.
3 Product(Product) Product(Objects you want to create with a design pattern)Define the interface of
4 Specific class of product Product implementation class

In this pattern, the interface for instantiating a class is defined in the upper abstract class, and instantiation is performed in the subclass that implements the abstract class. This pattern is sometimes referred to as "let the subclass decide which class to instantiate." This means that the creator's concrete class has no knowledge of the product (necessary information is inherited from the creator), and simply deciding the subclass to use determines the product to use (the concrete class of the product) automatically. is.

** Implement the pattern ** Classes represent regional pizzerias. Create a class to hold pizza data and a class for each store.

** Product **

Pizza.java


import java.time.LocalDateTime;
public class Pizza {
	String name;
	String dough;
	String sauce;

    public void accept() {
    	System.out.println(LocalDateTime.now() + "From the customer" + this.name + "I received your order");
    };
    public void prepare() {
    	System.out.println("in preparation···");
    };
    public void bake() {
    	System.out.println("Bake");
    };
    public void cut() {
    	System.out.println("Cut");
    };
    public void box() {
    	System.out.println("Pack");
    }
	public String getName() {
		return name;
	}
}

** Concrete class of product **

TokyoCheesePizza.java


public class TokyoCheesePizza extends Pizza{
	public TokyoCheesePizza() {
		name = "Pizza using cheese made in Tokyo";
		dough = "Tokyo fabric";
		sauce = "Tokyo source";
	}
}

OsakaCheesePizza.java


public class OsakaCheesePizza extends Pizza {
	public OsakaCheesePizza() {
		name = "Pizza using cheese made in Osaka";
		dough = "Osaka fabric";
		sauce = "Osaka sauce";
	}
	public void box() {
		System.out.println("We will deliver it in a special box at the store in Osaka");
	}
}

Author

Order.java


public abstract class Order {
	public Pizza OrderPizza(String type) {
		Pizza pz = createPizza(type);
		pz.accept();
		pz.prepare();
		pz.bake();
		pz.cut();
		pz.box();
		return pz;
	}
	abstract Pizza createPizza(String type);
}

** Creator's concrete class **

TokyoOrder.java


public class TokyoOrder extends Order {
	public Pizza createPizza(String type) {
		if (type.equals("cheese")) return new TokyoCheesePizza();
		else return new Pizza();
	}
}

OsakaOrder.java


public class OsakaOrder extends Order {
	public Pizza createPizza(String type) {
		if (type.equals("cheese")) return new OsakaCheesePizza();
		else return new Pizza();
	}
}

** Main class **

Main.java


public class Main {
	public static void main (String args[]) {
		Order tk = new TokyoOrder();
		Order os = new OsakaOrder();	
		Pizza tkpz = tk.OrderPizza("cheese");
		System.out.println(tkpz.getName() + "I ordered");
		Pizza ospz = os.OrderPizza("cheese");
		System.out.println(ospz.getName() + "I ordered");
	}
}

result


2017-05-22T17:02:41.553 We received an order from a customer for a pizza made with cheese made in Tokyo.
in preparation···
Bake
Cut
Pack
I ordered a pizza made with cheese made in Tokyo
2017-05-22T17:02:41.553 We received an order for pizza using cheese made in Osaka from a customer.
in preparation···
Bake
Cut
We will deliver it in a special box at the store in Osaka
I ordered a pizza using cheese made in Osaka

In this example, the pattern was applied with the pizza itself as the product. Replacing pizzeria orders with creators and pizzas with products. As the types of pizza increase, the number of product classes increases, the number of creator branches increases, and the impact of changes is narrowed down to subclasses (○○ Order.java, ○○ Pizza.java).

Recommended Posts

Introduction to Design Patterns (Factory Method)
Introduction to Design Patterns (Abstract Factory)
Introduction to design patterns (introduction)
Introduction to Design Patterns (Builder)
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
Introduction to Design Patterns (Strategy)
Introduction to algorithms with java-Shakutori method
Java beginner design pattern (Factory Method pattern)
Important design patterns to improve maintainability
Design patterns to enjoy with frequently used Java libraries --Factory pattern
Introduction to Ruby 2
Introduction to SWING
Method to search
Factory Method Pattern
Java Design Patterns
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Factory Method pattern
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Design pattern ~ Abstract Factory ~
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Introduction to Android Layout
static factory method part 1
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
[Rails] devise introduction method
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Design pattern ~ Template Method ~
Introduction to Keycloak development
Introduction to RSpec 4. Create test data with Factory Bot
Study GoF design patterns
16 Corresponds to method invocation
Introduction to javac command
Item 51: Design method signatures carefully
Read design patterns in Ruby
Introduction to RSpec 5. Controller specs
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
(Dot installation) Introduction to Java8_Impression
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~