当前位置:网站首页>[creation mode] factory method mode

[creation mode] factory method mode

2022-06-11 15:23:00 Evader1997

Review the simple factory model

The last column introduced the simple factory mode , its UML The class diagram looks like this
 Insert picture description here

   In the simple factory pattern, the factory class is used to create four corresponding to addition, subtraction, multiplication and division ‘ product ’, At this point, we create the object in the factory class through switch Of case To decide . If the computer needs to add a residual method , How do we do it ?

  1. Create a new remainder operation class to inherit the parent operation class Operation
  2. In factory class OperationFactory Of switch One more in case sentence ( You need to modify the code of this class )

   Creating a new residual class will not affect , however modify OperationFactory The code in the class violates the open closed principle , And every time a new algorithm class is added, the code in the factory class needs to be modified ! So there is a factory approach !

I understand the factory method

Let's first look at the factory method UML Class diagram
 Insert picture description here
   Compare the two pictures , It is clear that the factory method will One factory has become four factories , Every factory is very specific ,FactoryAdd The factory only produces addition objects ,FactoryDiv The factory only produces division objects ,FactoryMul Only multiplicative objects are produced ,FactorySub Only subtraction objects are produced .

   The factory method is an extension of the simple factory , From the review, we can see that the disadvantage of a simple factory is that it is not conducive to expansion . The factory approach perfectly addresses this shortcoming , It further abstracts the factory in the simple factory pattern , It has become a universal factory , This universal factory only provides methods for producing objects , The permission to create concrete objects is given to the implementation class .

The factory method pattern implements the calculator

Factory method interface

public interface FactoryMethod {
    
    Operation createOperation();
}

Factory classes that implement factory methods

public class FactoryAdd implements FactoryMethod {
    

    @Override
    public Operation createOperation() {
    
        return new OperationAdd();
    }
}
public class FactoryDiv implements FactoryMethod {
    
    @Override
    public Operation createOperation() {
    
        return new OperationDiv();
    }
}
public class FactoryMul implements FactoryMethod {
    

    @Override
    public Operation createOperation() {
    
        return new OperationMul();
    }
}
public class FactorySub implements FactoryMethod {
    
    @Override
    public Operation createOperation() {
    
        return new OperationSub();
    }
}

test

public class MainTest {
    
    public static void main(String[] args) {
    
        FactoryMethod factoryMethod = new FactorySub();
        Operation operation = factoryMethod.createOperation();
        operation.setNum1(2);
        operation.setNum2(3);
        System.out.println(" The result is :" + operation.getResult());
    }
}

Running results :

The result is :-1

summary

   The factory method pattern uses polymorphism , So it retains the advantages of simple factory , Customer service has overcome its shortcomings , A residual function is added in the factory method mode , There is no need to modify the existing code , You only need to add a factory class of the production surplus object , But this time, some development will be added ( A few more lines of code for a simple factory , Factory methods need to add a class ).

If you don't know why you use the simple factory, please see the simple factory article in the column , Hard core analysis !
Simple factory model hard core analysis

原网站

版权声明
本文为[Evader1997]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111518260604.html