当前位置:网站首页>Explain the factory method

Explain the factory method

2022-06-09 14:48:00 Floral Sea

Explain the factory method in detail

For better explanation Factory method , Let's not give rigid language , Let's look at it from a practical example

If we want to build a Calculator , Define addition 、 Multiplication 、 Various operations such as division , in consideration of encapsulation Thought , We might construct something like an addition class , Multiplication class , Divide the class , A series of classes such as subtraction class are as follows :

  • Class OperationAdd
  • Class OperationMinus
  • Class OperationMul
  • Class OperationDiv

When we want to add new functions, we can add corresponding classes , But this brings about a problem , When we want to use methods in a particular class , If not Static class We all need to instantiate an object first , For example, we need to do continuous operations such as multiplication and division , Then our next behavior may be like this :
Class mul = new OperationMul()
Class div = new OperationDiv()
...
You may need the most repeated operations , Our code may become bloated . In fact, what we need to consider at this time is how to instantiate the object , That is, who should be instantiated , Will instantiated objects be added in the future , For example, what should we do if we want to add new functions ? The simple factory approach just provides us with such a solution :

Click here to see the code
public class OperationFactory
{
	public static Operation createOperate(String operate){
		Operation oper = null;
		switch(opertate){
			case "+":
				oper = new OperationAdd();
				break;
			case "-":
				oper = new OperationSub();
				break;
			case "*":
				oper = new OperationMul();
				break;
			case "/":
				oper = new OperationDiv();
				break;
		}
	return oper;
	}
}

ad locum Operation Is a calculation class , But we go through Switch case Different subclasses are implemented when instantiating , It can be said that operation It is actually a statement here , our Operation[a-z] All are operation Subclasses of , It uses polymorphic The nature of .
eg: Polymorphic interpretation

Animal obj = new Horse();

So let's assume that Horse Inherited Animal This class , So here's Animal It's just a statement , The actual situation of this object at run time is determined by the following new The object of horse decision , Parameterized polymorphism means that the name of a type can represent multiple types .

Did you see? ? We design in this way , Just enter the symbol of the operation , Factories can instantiate different objects , The method that returns the parent class through polymorphism also achieves the desired result .

Next, consider the following for such an approach Two questions

1. If we want to change the behavior of addition , What to do ?

 answer : Just change OperationAdd The class can 

2. What if we want to add an operation , For example, add the operation of square root

 answer : Add the corresponding operator class , And in switch case Add the corresponding branch to the table entry of 

summary :

When the user does not know which concrete class instance to create , Or we usually use factory methods when we don't want to specify the instance to be created in the user code .
** The essence of the factory method is :** Let subclasses decide which class to instantiate , Thus, the instantiation of a class is delayed to its subclasses .

I believe you have a certain understanding of this simple factory method , If you are interested, you can take a look at **《 Big talk design patterns 》** Abstract factory methods in , I believe you will understand the design subtlety and corresponding disadvantages of the factory method .

This blog is over here , Thanks for reading !

原网站

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