Factory method model

August 22, 2018 19:34:48

Factory method model

definition

If I want to marry you, I must ask your parents, which is called your parents' order.

The Factory Method pattern defines an interface for creating objects and lets subclasses decide which class to instantiate. Factory methods delay instantiation of a class to its subclasses—— Design patterns: the foundation of Reusable Object-Oriented Software

Usage scenario

Based on the simple factory mode, the factory method mode gives the choice of judgment logic to the client, so that the client can decide which specific factory class to use. This is the disadvantage of factory method compared with simple factory mode. In this case, you need to know what specific factory classes are in advance. Compared with the straightforward new object, you don't need to remember the specific product class. The server hides the creation details of the specific product class. In the specific factory class, you don't have to create the specific product class. You can also do some other things, which is in line with the object-oriented encapsulation.

Adding products to the simple factory mode requires modifying the internal judgment logic, which violates one of the six modes of design mode Opening and closing principle , that is, it is open to extension and closed to modification. The factory method mode plus products only needs to add corresponding specific factory objects, which conforms to the opening and closing principle, which is one of the advantages of the factory method mode. Personal view: the factory method pattern is suitable for the scenario where specific product objects are often replaced (i.e. added and deleted), and takes advantage of this advantage. For example, you are a designer when the clothing season changes. When the new products are put on the shelves and new products are added, the boss chooses a new factory. The sales merchant only needs to obtain new products from the factory. The old products are taken off the shelves and the contract with the old factory expires. That's it.

role

After the internal logic judgment of the factory role in the simple factory mode is handed over to the client, the details of the created object are encapsulated to the specific factory classes, which inherit from a common interface. Simple factory mode to view: Simple factory mode.

The factory method mode has the following four roles:

  • Abstract factory role (IFactory): the public interface of a specific factory, which defines the products returned by the factory method
  • Concrete factory role (ConcreteFactoryImpl): inherit the factory public interface, implement factory methods, and return specific product instances during runtime
  • Abstract product role (IProduct)
  • Specific product role (ConcreteProductImpl)

Illustration

Code example

The code example still uses the operation of addition, subtraction, multiplication and division. Like the simple factory mode, it only splits the factory class SimpleFactory into abstract factory classes and concrete factory classes. The simple factory mode can be viewed as follows: Simple factory mode.

Class dependency diagram:

Abstract factory role (IOperationFactory.java):

public interface IOperationFactory {
	public IOperation createOperation();
}

Abstract concrete factory object public interface.

Specific factory roles (AddOperationFactoryImpl.java, SubOperationFactoryImpl.java, MulOperationFactoryImpl.java, DivOperationFactoryImpl.java):

// Addition plant
public class AddOperationFactoryImpl implements IOperationFactory {

	@Override
	public IOperation createOperation() {
		return new AddOperationImpl();
	}

}
// Subtraction factory
public class SubOperationFactoryImpl implements IOperationFactory {

	@Override
	public IOperation createOperation() {
		return new SubOperationImpl();
	}

}
//  Multiplication factory
public class MulOperationFactoryImpl implements IOperationFactory {

	@Override
	public IOperation createOperation() {
		return new MulOperationImpl();
	}

}
//  Division factory
public class DivOperationFactoryImpl implements IOperationFactory {

	@Override
	public IOperation createOperation() {
		return new DivOperationImpl();
	}

}

Each specific factory object corresponds to a specific product object.

Abstract product role (IProduct.java):

public interface IOperation {
	public int getResult(int a, int b);
}

Specific product roles (AddOperationImpl.java, SubOperationImpl.java, MulOperationImpl.java, DivOperationImpl.java):

// Addition implementation class
public class AddOperationImpl implements IOperation{

	@Override
	public int getResult(int a, int b) {
		return a + b;
	}

}
// Subtraction implementation class
public class SubOperationImpl implements IOperation {
	
	@Override
	public int getResult(int a, int b) {
		return a - b;
	}
}
// Multiplication implementation class
public class MulOperationImpl implements IOperation {

	@Override
	public int getResult(int a, int b) {
		return a * b;
	}

}
// Division implementation class
public class DivOperationImpl implements IOperation {

	@Override
	public int getResult(int a, int b) {
		return a / b;
	}

}

Test class (FactoryMethodTest.java):

public class FactoryMethodTest {
	
	public static void main(String[] args) {
		int a = 999, b = 888;
		// plus
		// Create a specific factory
		IOperationFactory operationFactory = new AddOperationFactoryImpl();
		// Create specific products
		IOperation operation = operationFactory.createOperation();
		// Call the functions of specific products
		int result = operation.getResult(a, b); // 1887
		System.out.println(result);
		
		// reduce
		operationFactory = new SubOperationFactoryImpl();
		operation = operationFactory.createOperation();
		result = operation.getResult(a, b); // 111
		System.out.println(result);
		
		// ride
		operationFactory = new MulOperationFactoryImpl();
		operation = operationFactory.createOperation();
		result = operation.getResult(a, b); // 887112
		System.out.println(result);
		
		// except
		operationFactory = new DivOperationFactoryImpl();
		operation = operationFactory.createOperation();
		result = operation.getResult(a, b); // 1
		System.out.println(result);
		
	}
	
}

The client needs to determine what specific factory object to use, so as to instantiate the required specific product object.

Advantages (cheat sheet)

(link at the end of the text)

1. In the factory method mode, the factory method is used to create the product required by the customer. At the same time, it also hides the details of which specific product class will be instantiated from the customer. Users only need to care about the factory corresponding to the required product, do not need to care about the creation details, or even know the class name of the specific product class.

2. Polymorphic design based on factory role and product role is the key of factory method pattern. It enables factories to independently determine what product object to create, and the details of how to create this object are completely encapsulated in specific factories. Factory method pattern is also called polymorphic factory pattern because all specific factory classes have the same abstract parent class.

3. Another advantage of using the factory method pattern is that when adding new products to the system, there is no need to modify the interface provided by the abstract factory and the abstract product, the client, or other specific factories and specific products, but just add a specific factory and specific product. In this way, the scalability of the system becomes very good and fully conforms to the requirements "Opening and closing principle".

Disadvantages (cheat sheet)

1. When adding new products, you need to write new specific product classes and provide corresponding specific factory classes. The number of classes in the system will increase in pairs, which increases the complexity of the system to a certain extent. More classes need to be compiled and run, which will bring some additional overhead to the system.

2. Considering the scalability of the system, it is necessary to introduce an abstraction layer, which is defined in the client code, which increases the abstraction and understanding difficulty of the system, and DOM, reflection and other technologies may be used in the implementation, which increases the difficulty of the implementation of the system.

summary

Factory method pattern is a further abstraction and generalization of simple factory pattern.

Due to the use of object-oriented polymorphism, the factory method pattern not only maintains the advantages of simple factory pattern, but also overcomes its disadvantages.

All codes can be found in Github.Mingmingcome

finish

reference resources:

Factory method pattern

Posted by webgod on Mon, 29 Nov 2021 12:14:09 -0800