Recently for the design model of the factory three sisters-elder sister (abstract Factory), second sister (Factory mode), three elder sister (simple Factory), further study, the following are some of my understanding.
First, Simple factory
Using a separate class will create an instance of the process independent, this is the simple factory class, such as: simple operation of the factory class, as for the use of the process, if you increase the operation of operations, you need to modify the operation of the factory class, which violates the
Open-close principle (make the code as open as possible, change the closure, that is, the changes to the program by adding code, rather than changing the existing code).
Second, the factory model
The factory subclass is added to the simple factory relative to the above, so that an instance of a class is deferred to the subclass, so that when the operation is added, it is only necessary to increase the corresponding operation factory, which solves the disadvantage that the simple factory does not conform to the open-closed principle.
Simple Factory vs Factory method
The factory approach solves the hassle of simple factory plus case judgment, but it is also complicated to add the relevant factory to the code and then modify the client for each additional operation.
In fact, we can use simple Factory + reflection, which is more simple than the factory method alone.
Third, abstract factory
Abstract factory easy to exchange product family (an application only needs to occur once during the initialization process, so it is easy to change the application's specific factory), and the creation of the instance process is separated from the client, and the specific class name of the product is separated by the specific factory implementation.
But if you add functionality, the abstract factory needs to modify a lot of places, and here is a simple factory instead of all the specific factories.
The problem comes out again, using a simple factory will encounter case to determine the branch of trouble, but it doesn't matter, the previous mention of the use of reflection is easy to solve.
For the factory three sisters at present just understand these, believe will in the future programming on their understanding more profound, look forward to meet again.
Design Pattern Factory Three Sisters