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