Two, Factory Method (factory method mode)
Factory method pattern: Define an interface for creating objects, and let the subclass decide which class to instantiate. The Factory Method delays the instantiation of a class to the subclass.
To understand the factory method pattern, starting from the factory pattern, the factory pattern can be divided into three categories:
(1) Simple Factory Mode (Simple Factory)
(2) Factory Method (Factory Method)
(3) Abstract Factory mode (Abstract Factory)
Some books divide factory patterns into two categories: Factory Method and Abstract Factory. The simple factory pattern (Simple Factory) is regarded as a special case of the factory method pattern, and the two are classified into one category.
In an era when there were no factories, if a customer wanted a BMW, the customer would build one by themselves:
Product category:
// BMWX5.java
public class BMWX5 {
publicBMWX5(){
System.out.println("Make a BMW X5");
}
}
// BMWX6.java
public class BMWX6 {
publicBMWX6(){
System.out.println("Make a BMW X6");
}
}
Customer category:
// Comsumer.java
public class Comsumer {
publicstatic void main(String[] args) {
BMWX5x5 = new BMWX5();
BMWX6x6 = new BMWX6();
}
}
Later, there was an industrial revolution, and customers didn't need to make cars themselves, because customers opened a factory to make cars, and the factory could manufacture what kind of cars they wanted.
Product category:
// BMW.java
public abstract class BMW {
publicBMW(){
}
}
// BMWX5.java
public class BMWX5 extends BMW {
publicBMWX5() {
System.out.println("Make a BMW X5");
}
}
// BMWX6.java
public class BMWX6 extends BMW{
publicBMWX6(){
System.out.println("Make a BMW X6");
}
}
Factory class:
// Factory.java
public class Factory {
publicstatic BMW createBMW(String type) {
if(type== "X5")
returnnew BMWX5();
elseif(type == "X6")
returnnew BMWX6();
elsereturn null;
}
}
Customer category:
// Customer.java
public class Customer {
publicstatic void main(String[] args) {
Factoryfactory = new Factory();
BMWx5 = factory.createBMW("X5");
BMWx6 = factory.createBMW("X6");
}
}
宝马车系列越来越多,如320i、523i、30li等系列,一个工厂已经无法创建所有的宝马车系列。于是又单独分出来多个具体的工厂。每个具体工厂创建一种系列。即具体工厂类只能创建一个具体产品。但是宝马工厂还是个抽象。你需要指定某个具体的工厂才能生产车出来。
产品类:
// BMW.java
public abstract class BMW {
publicBMW(){
}
}
// BMWX5.java
public class BMWX5 extends BMW {
publicBMWX5() {
System.out.println("制造一辆宝马X5");
}
}
// BMWX6.java
public class BMWX6 extends BMW{
publicBMWX6(){
System.out.println("制造一辆宝马X6");
}
}
工厂类:
// FactoryBMW.java
public interface FactoryBMW {
BMWcreateBMW();
}
// FactoryBMWX5.java
public class FactoryBMWX5implements FactoryBMW{
publicBMWX5 createBMW() {
returnnew BMWX5();
}
}
// FactoryBMWX6.java
public class FactoryBMWX6 implements FactoryBMW{
publicBMWX6 createBMW() {
returnnew BMWX6();
}
}
客户类:
// Customer.java
public class Customer {
publicstatic void main(String[] args) {
FactoryBMWX5factoryBMWX5 = new FactoryBMWX5();
BMWX5x5 = factoryBMWX5.createBMW();
FactoryBMWX6factoryBMWX6 = new FactoryBMWX6();
BMWX6x6 = factoryBMWX6.createBMW();
}
}