factory method pattern
Factory method pattern: Factory Method
Things are developing. With the continuous development of things, the original objects will continue to change, or increase or decrease. An encapsulation mechanism provided by
the factory method pattern isolates those volatile objects . At this time, the change of demand no longer affects the change of the previous object.
1. The role of the factory method pattern
- 1. Abstract product: the base class of all subclass products, giving an abstract interface or abstract class.
- 2. Concrete products: inherit the attributes and methods of abstract products, and have specific products with certain characteristics.
- 3. Abstract Factory: Contains the core business logic of the product and provides an abstract interface or class.
- 4. Concrete factory: Inherit the concrete factory of the abstract factory to realize the specific business logic.
Second, the factory method usage scenario
1. When the exact category of the object and its dependencies cannot be predicted.
2. In order to be able to extend the components in the code.
3. When reusing existing objects to save system resources.
3. Factory method case and code implementation
Huawei is a group that includes industries such as mobile phones, networks, watches, routing, and automobiles. At the beginning, Huawei did not have an automotive business. It started with network switches. Then when we define Huawei in the code, it is impossible to predict its future business and its dependencies . At this time, the factory method mode is applicable.
The corresponding factory method pattern roles are as follows:
1. Abstract product: Huawei product
2. Specific product: Huawei watch, Huawei car, Huawei router
3. Abstract factory: Huawei factory
4. Specific factory: Huawei mobile phone factory, Huawei car factory, Huawei router factory
3.1 Factory method code implementation
1. Example diagram/UML diagram
2. Abstract products
Define a product interface, which is the parent class of all products: HuaWeiProduct.java
, which defines constants: MANUFACTURER
(manufacturer) = HuaWei
; abstract interface: getManufacturer()
get the manufacturer; getProductName()
get the specific product name
public interface HuaWeiProduct {
/**
* 华为产品制造商必定都是华为
**/
String MANUFACTURER = "HuaWei";
/**
* 通过产品可以获取制造商
**/
String getManufacturer();
/**
* 通过产品可以获取产品名称
**/
String getProductName();
}
3. Specific products
1. Mobile phone products
The subclass of Huawei products implements the abstract interface of abstract products: HuaWeiPhone.java
.
public class HuaWeiPhone implements HuaWeiProduct {
@Override
public String getManufacturer() {
return HuaWeiProduct.MANUFACTURER;
}
@Override
public String getProductName() {
return "华为手机";
}
}
There will be many types of mobile phones in the future stage. Here, Mate products and Nova products are established, inheriting mobile phone product categories, and rewriting getProductName()
methods.
public class HuaWeiMatePhone extends HuaWeiPhone{
@Override
public String getProductName() {
return super.getProductName() + ":Mate系列手机";
}
}
public class HuaWeiNovaPhone extends HuaWeiPhone{
@Override
public String getProductName() {
return super.getProductName() + ":Nova系列手机";
}
}
2. Automotive products
The subclass of Huawei products implements the abstract interface of abstract products: HuaWeiCar.java
.
public class HuaWeiCar implements HuaWeiProduct {
@Override
public String getManufacturer() {
return HuaWeiProduct.MANUFACTURER;
}
@Override
public String getProductName() {
return "华为汽车";
}
}
There will be many types of cars in the future. Currently, there is only "Wenjie", which inherits the car product class and rewrites getProductName()
the method.
public class HuaWeiWenJieCar extends HuaWeiCar{
@Override
public String getProductName() {
return super.getProductName() + ":问界汽车";
}
}
4. Abstract Factory
Define a factory interface, the meaning of factory existence: production. HuaWeiFactory.java
, the abstract interface: produce()
produce product
public interface HuaWeiFactory<T> {
/**
* 生产方法
**/
T produce(Class<? extends T> classz);
}
5. Specific factory
1. Mobile phone factory
The method of implementing the abstract factory is the place where the product is actually created, and contains the business logic for creating the product. The abstract factory method is implemented here produce()
to create objects through reflection.
public class HuaWeiPhoneFactory implements HuaWeiFactory<HuaWeiPhone> {
/**
* 华为手机工厂生产手机
**/
@Override
public HuaWeiPhone produce(Class<? extends HuaWeiPhone> classz) {
try {
return classz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
2. Automobile factory
The method of implementing the abstract factory is the place where the product is actually created, and contains the business logic for creating the product. The abstract factory method is implemented here produce()
to create objects through reflection.
public class HuaWeiCarFactory implements HuaWeiFactory<HuaWeiCar> {
/**
* 华为汽车工厂生产汽车
**/
@Override
public HuaWeiCar produce(Class<? extends HuaWeiCar> classz) {
try {
return classz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
6. Create objects through factories
1. Instantiate mobile phone and car factories: HuaWeiPhoneFactory
, HuaWeiCarFactory
.
2. produce()
Pass in the specific products that need to be created through the method of the factory.
3. Output the manufacturer and product name of the specific product.
public static void main(String[] args) {
//华为手机工厂
HuaWeiFactory<HuaWeiPhone> huaWeiPhoneFactory = new HuaWeiPhoneFactory();
//华为汽车工厂
HuaWeiFactory<HuaWeiCar> huaWeiCarFactory = new HuaWeiCarFactory();
HuaWeiProduct huaWeiMateProduct = huaWeiPhoneFactory.produce(HuaWeiMatePhone.class);
HuaWeiProduct huaWeiNovaProduct = huaWeiPhoneFactory.produce(HuaWeiNovaPhone.class);
HuaWeiProduct huaWeiWenJieProduct = huaWeiCarFactory.produce(HuaWeiWenJieCar.class);
System.out.println(huaWeiMateProduct.getManufacturer()+huaWeiMateProduct.getProductName());
System.out.println(huaWeiNovaProduct.getManufacturer()+huaWeiNovaProduct.getProductName());
System.out.println(huaWeiWenJieProduct.getManufacturer()+huaWeiWenJieProduct.getProductName());
}
The result is as follows:
HuaWei华为手机:Mate系列手机
HuaWei华为手机:Nova系列手机
HuaWei华为汽车:问界汽车