Design mode complete studies, I was a study lying on the order, there are three modes whose name contains the word "factory" in the eye,. They are the "factory Three Sisters", the following we pass the Calculator demonstration sample to have a good understanding of this sister three son.
Simple Factory mode:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq1lmx2hhchb5z2lyba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
Code in the Simple factory class:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq1lmx2hhchb5z2lyba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
From which we can see. When the user requests the addition operation, only needs operationfactory.createoperate ("+"), the factory will voluntarily give an instance of the addition class, the user does not have to deal with the detailed operation class, the object creation process is encapsulated.
But suppose we want to add a power operation, not only to add the sub-class of the power operation under the Operation class, but also to modify the factory class and add the new case. This is also open to modifications. The simple factory model is not a real design pattern because it violates the open and closed principle.
Using the simple factory model is a good choice when the factory class is responsible for creating fewer objects and no longer adding or subtracting, and the customer simply knows the parameters of the incoming factory class.
Factory method Mode:
Compared to the simple factory model, we can clearly see that the factory class has more detailed operations in the factory sub-class, The factory class interface task is not so onerous, there is only one way to create abstract products:
All production of detailed product class factory. To implement this interface. The client's code becomes this:
Ifactory operfactory=new addfactory (); Operation Oper=operfactory.createoperation ();
This lets the subclass decide to instantiate the detailed class. Division of Labor more fine, responsibility to people. At this point, we need to add the power operation only to the factory class and the operation of a separate extension, without changing the code, to compensate for the simple factory model deficiencies.
When it comes to producing a range of products, let's say there are two levels of calculators, one is our usual arithmetic calculator, and the other is a scientific calculator. They all compute subtraction, and the factory approach pattern is just a calculator. This time, it is necessary to use the abstract factory model, it is really a mountain outside the mountains, people outside.
Abstract Factory mode:
(Give yourself a sample.) There is something wrong with it. Please understand)
So. Customers can change the series of products, want the arithmetic type calculator, Ifactory=new arithmeticfactory (). Want scientific type,ifactory=new sciencefactory ().
the difference between the factory method pattern and the abstract factory model is actually two words: The former has only one abstract product class. And the latter has multiple. The former 's detailed factory class can only create an instance of a detailed product class, while the abstract factory pattern can create multiple.
Throughout these three models, superseding. But don't assume that the abstract factory model is perfect when we're adding multiplication? There are not only extensions but also modifications. Learning to be flexible, the three sisters can help each other, allowing the simple factory model to improve the abstract factory model. In fact, in places where all simple factories are used, reflection technology can be considered to remove a switch or an if, decoupling the branching inference.
There are more ways than difficulties.
This is where the three sisters are introduced. Let's have a familiar face today. There are many days to deal with later.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Design pattern------Plant Three Sisters