Factory Method Mode: Delay to subclass to select implementation

Framework and Design Patterns

    What is the framework: Framework is semi-finished software that can complete certain functions.
    What does a framework do: it can accomplish certain functions and speed up application development; Provides a sophisticated program architecture.
    The relationship between framework and application: who does and what does it do? The framework completes part of the functionality, and the application developer completes part of the functionality.
    For example, in a large application system, application development only needs to focus on business implementation, and the possible functions of the application framework are:
    (1)Unified response return message, standardize the content of message format;
    (2)Base class definition, common field conventions;
    (3)Unified exception handling mechanism;
    (4)Provide Public API Use, tool class development;
    (5)Provide third-party component packaging, such as: hadoop,hbase,kafka,mybitas,redis And so on;
    Application: Just focus on the implementation of business process capabilities. Interface development, user experience interaction, etc.

Framework versus design pattern:

    (1)Design patterns are more abstract than frameworks: frameworks are semi-finished software that has been implemented; The design pattern is based on the solution to the problem and the idea.
    (2)Design patterns are architectural elements smaller than frameworks: frameworks are implemented software, and a framework can contain applications of multiple design patterns.
    (3)Frames are more specific than design patterns: they are specific to a particular domain and solve problems within a domain, each domain has a different framework; The design mode focuses on solving problems ideologically and methodologically, not limited to a certain field, but more general.

Definition and nature
Definition: Defines an interface for creating objects, lets subclasses decide which class to instantiate, and factory methods delay the instantiation of a class to its subclasses.
Essential: Delay to subclasses to select implementations.
Structural Diagram:

Factory Method Details
(1) The function of the factory method is to allow the main class to complete the invocation of its own functions without knowing the specific implementation; The implementation is deferred to the subclass implementation.
(2) The implementation of factory methods, usually the parent class is an abstract class that contains abstract methods for creating objects, which are factory methods.
(3) The return of a factory method is typically an interface object, an abstract class or an instance of a class that is created.
(4) Factory methods are generally not provided for external (client) calls, and the objects created by them are not generally provided for external use. They are mainly created by methods within the creator class that use factory methods to create objects.
(5) The parent class can be implemented from an abstract class to a specific class.

Dependency Inversion Principle

   Dependency: To depend on abstract classes, not specific classes.
   Inverted: This is the ownership of the interface. The ownership of the interface implemented at the lower level is not in the hands of the lower level components, but is inverted to the higher level components. Functions in an interface are the functions required by high-level components, which make demands and implement the interface functions required by high-level components.

UML class diagram

Call Sequence Diagram

Advantages and disadvantages
(1) There is no need to know the implementation: the implementation is done by subclasses.
(2) It is easy to extend new objects: by hooking a subclass with the factory method (hook method), the subclass can be used by providing an implementation of the factory method.
(3) Connect parallel class hierarchies: make hierarchies easier to expand and reuse.
Specific product object coupled with factory method: When a factory method creates an object, it needs to select the object that is implemented specifically, which is coupled with the factory method.

Application scenarios and code implementation

// 1 Client Call Class
public class Client {
     public static void main(String[] args) {
           AbstractCreator creatorA = new AConcreteCreator();
           AbstractCreator creatorB = new BConcreteCreator();
// 2 Creator: Declare factory method
public abstract class AbstractCreator {
     // 2.1 Declare factory methods, implemented by subclasses, instantiate business interface objects
     protected abstract Api factoryApi();
     // 2.2 Specific Business Function Implementation Methods
     public void operation(){
           // 2.3 AbstractCreator requires an Api object, no longer actively creates a business implementation class, but hands it to factoryApi() of the subclass for implementation (IOC/DI)
           Api api = factoryApi();
// 3.1 Specific creator object class A, inheriting creator class
public class AConcreteCreator extends AbstractCreator{
     protected Api factoryApi() {
           // 3.1.1 Really select business implementation classes and create objects (IOC/DI)
           return new AApiImpl();
// 3.2 Specific creator object class B, inheriting creator class
public class BConcreteCreator extends AbstractCreator{
     protected Api factoryApi() {
           return new BApiImpl();
// 4 Business Interface Class
public interface Api {
     public void operation();
// 5.1 Business Interface Implementation Class A
public class AApiImpl implements Api {
     public void operation() {
           System.out.println("AApiImpl do...");
// 5.2 Business Interface Implementation Class B
public class BApiImpl implements Api {
     public void operation() {
           System.out.println("BApiImpl do...");

Keywords: Design Pattern UML

Added by thatsme on Wed, 01 Dec 2021 21:33:04 +0200