First look at the structure :
Simple factory model (SimpleFactory Mode):
The idea of the simple factory model is , First of all, let's put some things in common ( algorithm ) take out , Abstract , Such as addition, subtraction, multiplication and division . Then we define a class as a factory class , The function of the factory class is based on the string passed in or something else
Key Value to the entity that returns a corresponding algorithm .
advantage :
Convenient extension algorithm , For example, add a root opening function , We just need to continue to inherit the operation class , At the same time, the client is the user does not know the specific implementation details , As long as the relevant identifier is given , The factory function will immediately create an entity he wants . It reduces the coupling between users and function developers .
shortcoming :
Obviously , When expanding , We want to change the branch statement in the factory function Switch, And that destroyed it OCP
, And when there are multi-level structure inheritance , A simple factory will remember inheritance because it can only correspond to a parallel layer , You have to make many classes inherit the same interface , And then you get it A*B So many factory entities may , Factory functions are difficult to maintain .
Factory approach model (Factory Method):
Define an interface for creating objects , Let subclasses decide which class to instantiate . Factory methods delay the instantiation of a class to its subclasses .
advantage :
The creation of algorithm entities has been deferred to the factory subclass , We don't create objects directly in the factory , Instead, they directly package small factories one by one , Each factory is responsible for creating its own subclass , So it doesn't exist switch
The situation , There is no extension dissatisfaction OCP The question of .
shortcoming :
If there are many kinds of algorithms , Then there will be many subclasses that inherit the abstract factory , Maintenance is not very good , Product switching is not supported at the same time , For example, we need to develop PC
, Divided into multiple systems , So we can abstract all the systems ( Like the addition, subtraction, multiplication and division above ), And then we're abstracting the factory , But what if we have two hardware at this time ,PC and Phone
, Although we can guarantee only these two hardware , But it's still awkward to use a basic abstraction factory to implement it .
Abstract factory pattern (Abstract Factory):
Provides an interface to create a series of related or interdependent objects , You don't need to specify their specific classes .
advantage :
The first is satisfaction OCP Of , And it can satisfy the product switching , The premise that can be realized is, for example A and B Two products , They have 1 and 2 Two method interfaces ( class ), Now we are adding new products C( The assumption is that 1 and
2 Two method interfaces ), All we have to do is add a product class and a factory class , If it is a simple factory or factory method, it usually adds two algorithm classes C.1,C.2, Simple factories need to be modified
switch Add two statements , The factory method is to add two factory classes . We can see the advantages of abstract factory .
shortcoming :
obvious , It's too heavy .
contrast :
Simple factory implementation , It's also easy to expand , But not satisfied OCP, dissatisfaction OCP The price is hard to maintain , It is easy to cause new problems during maintenance BUG
, by comparison , The factory method delays the instantiation of the object to the inherited subclass , In this way, the factory can be expanded in quantity or . It's time to expand OCP
, However, product switching is not supported , That is to say, only one layer of products can be satisfied ( algorithm ) abstract , The abstract factory is to continue to abstract the product again , Finally, we get a structure that can support product switching , But the problem is too heavy , Too complicated , But it's OK , Many languages support reflection , We can optimize this directly through reflection technology “ overweight ” The disadvantages of . of course , Reflection can also be used to optimize the structure of the first two plants ( But the abstract factory is compared to the factory approach , Both of them only support the scalability of one place , Don't misunderstand that abstract factories can be extended in two places ).
evolution :
Optimization and satisfaction OCP ; Optimization meets product switching
Simple factory ---------------------------> Factory approach --------------------------------> Abstract factory
degeneration :
Increase the coupling between client and client ( Clients need to know all kinds of factories ); It's getting too heavy
Simple factory ---------------------------> Factory approach --------------------------------> Abstract factory
Technology
Daily Recommendation