Summary of simple factory mode and factory method mode, and summary of factory mode methods
I. Concepts and Class Diagrams
1. Simple factory mode (static factory Mode)
In the simple factory mode, the factory class can return instances of different classes according to different parameters.
2. Factory method mode (polymorphism Mode)
The core of the Factory method mode is the abstract Factory class Factory. Various Factory classes inherit the abstract Factory class and implement the Factory methods defined in the abstract Factory class, therefore, the customer only cares about the abstract product and the abstract factory, regardless of which specific product is returned or how it is created by a specific factory.
Ii. Example 1. Simple factory mode-simple calculator
// Abstract Operation class --- public abstract class Operation {private double numberA = 0; private double numberB = 0; public double getNumberA () {return numberA;} public void setNumberA (double numberA) {this. numberA = numberA;} public double getNumberB () {return numberB;} public void setNumberB (double numberB) {this. numberB = numberB;} public abstract double GetResult ();}
// Addition class public class OperationAdd extends Operation {@ Override public double GetResult () {// TODO Auto-generated method stub double result = 0; result = getNumberA () + getNumberB (); return result ;}}
// Division class public class OperationDiv extends Operation {@ Override public double GetResult () {double result = 0; result = getNumberA ()/getNumberB (); return result ;}}
// Multiplication class public class OperationMul extends Operation {@ Override public double GetResult () {double result = 0; result = getNumberA () * getNumberB (); return result ;}}
// Subtraction class public class OperationSub extends Operation {@ Override public double GetResult () {// TODO Auto-generated method stub double result = 0; result = getNumberA () -getNumberB (); return result ;}}
/*** Operation factory class --- create different instance Objects Based on different operators **/public class OperationFactory {public static Operation createOperation (String operate) {operation Operation = null; switch (operate) {case "+": operation = new OperationAdd (); break; case "-": operation = new OperationSub (); break; case "*": operation = new OperationMul (); break; case "/": operation = new OperationDiv (); break;} return operation ;}}
// Client public class Show {public static void main (String [] args) {System. out. println ("Enter the number A:"); then SC = new then (System. in); String strNumberA = SC. nextLine (); System. out. println ("Enter the operator (+,-, *,/):"); String strOperate = SC. nextLine (); System. out. println ("Enter the number B:"); String strNumberB = SC. nextLine (); Operation operation = OperationFactory. createOperation (strOperate); operation. setNumberA (Double. parseDouble (strNumberA); operation. setNumberB (Double. parseDouble (strNumberB); double result = operation. getResult (); System. out. println ("result:" + result); SC. close ();}}
2. Factory method mode-simple calculator
/** Factory interface */public interface ifacloud {Operation CreateOperation ();}
/** Addition factory */public class AddFactory implements ifacloud {@ Override public Operation CreateOperation () {return new OperationAdd ();}}
/** Division factory */public class DivFactory implements ifacloud {@ Override public Operation CreateOperation () {return new OperationDiv ();}}
/** Multiplication factory */public class MulFactory implements ifacloud {@ Override public Operation CreateOperation () {return new OperationMul ();}}
/** Subtraction factory */public class SubFactory implements ifacloud {@ Override public Operation CreateOperation () {return new OperationSub ();}}
// Operation class public abstract class Operation {private double numberA = 0; private double numberB = 0; public double getNumberA () {return numberA;} public void setNumberA (double numberA) {this. numberA = numberA;} public double getNumberB () {return numberB;} public void setNumberB (double numberB) {this. numberB = numberB;} public abstract double GetResult ();}
// Addition class public class OperationAdd extends Operation {@ Override public double GetResult () {// TODO Auto-generated method stub double result = 0; result = getNumberA () + getNumberB (); return result ;}}
// Division class public class OperationDiv extends Operation {@ Override public double GetResult () {double result = 0; result = getNumberA ()/getNumberB (); return result ;}}
// Multiplication class public class OperationMul extends Operation {@ Override public double GetResult () {double result = 0; result = getNumberA () * getNumberB (); return result ;}}
// Subtraction class public class OperationSub extends Operation {@ Override public double GetResult () {// TODO Auto-generated method stub double result = 0; result = getNumberA () -getNumberB (); return result ;}}
/** Client display ---- simple calculator in factory method mode */public class Show {public static void main (String [] args) {ifacloud = new AddFactory (); operation operation = ifacloud. createOperation (); operation. setNumberA (1); operation. setNumberB (2); double result = operation. getResult (); System. out. println ("result:" + result );}}
Iii. Summary
1. In the simple Factory mode, the Factory class integrates the logic of creating all products. Once new products are to be expanded, the Factory class has to be modified, this violates the open and closed principle (open to expand, closed to modify), and will cause the factory logic to be too complex.
2. In the factory method model, when a new product is added, a specific factory and a specific product category need to be added, so that the scalability of the program can be improved, in line with the open and closed principles, this avoids the disadvantages of the simple factory model. However, when adding a product, you need to add two classes to increase the amount of code. This can be said to be a good solution. How can we use it based on the actual situation.