1. Simple factory is based on your conditions, by a factory class to determine what winter winter,
For example, we define a fruit class, then define two subclasses, apples and bananas. If we want to create an apple and banana, our way is
Apple a = new apple ();
Banna b = new banna ();
If there are 10 kinds of fruit, we have to write ten times, I am annoying.
Now, we want to use a function to decide when to generate an apple, and when to generate bananas,
The idea is to use a string as a parameter into a function, this function is similar below.
Public Fruit Makefruit (String Fruitname) // Makefruit is a factory, which is a fruit class.
If FruitName = 'a' Ten Return New Apple ();
IF fruitname = 'b' twurn new banna ();
This way we can return to the subclass we need according to strings.
If in the factory, this employee, I will definitely send him high salary because he will do anything.
What makes him produces, what he produces, but the drawbacks of this method are also obvious.
Makefruit itself is complicated, does not have good scalability, each plus a class must, write judgment statements, to generate the corresponding classes, so people think, other methods, what method, what method,
And listen to the decomposition.