Objective
An overview of the principles and classifications of design patterns has been introduced before. Today to open the design mode of learning, the first thing to introduce is the factory model, in the introduction of Factory mode before the introduction of a simple factory model, so easy to introduce.
Simple Factory mode
Practice: Create a factory (method or Class) that is used to make objects.
When a person wants to use a mobile phone, they need to create a mobile phone and then use it.
As follows:
Public class Iphonex { public Iphonex () { System.out.println ("##### manufacturing Iphonex #####");} }
public class Iphone8 { Iphone8 () {System.out.printf ( ##### manufacturing Iphone8 ##### "
Public class Customer { /** * Use phone * / @Test public void Usephone () { new Iphonex (); New Iphone8 (); }}
This way, customers in the use of the mobile phone will see the specific implementation of the mobile phone, the customer and the mobile phone coupling is very high, in fact, the customer does not care about what the specific mobile phone implementation, in order to reduce the coupling, there is a factory class, the mobile phone manufacturing process put into the factory to achieve. When the customer wants to use the phone, it is possible to pass the phone model to the factory class to create the phone. Instead of having to worry about the whole process of making a phone, it's also very flexible to add extra actions to the phone creation process. This implementation is the simple factory pattern described below
Public Abstract class iphone { public iphone () { }}
Public class extends iphone{ public Iphonex () { System.out.println ("##### manufacturing Iphonex #####" ); }}
Public class extends iphone{ public Iphone8 () { System.out.printf ("##### manufacturing Iphone8 #####");} }
Factory class:
Public classFoxconnfactory {/*** Create phone *@paramModel Type *@return */ Publicstatic Iphone Createiphone (String model) {Switch(model) { CaseX:{ return NewIphonex (); } Case"8":{ return NewIphone8 (); } default: Break; } return NULL; }}
Customer class:
Public class Customer { /** * Use phone * / @Test public void Usephone () { = Foxconnfactory.createiphone ("X"
= Foxconnfactory.createiphone ("8"
}
}
The class diagram for the simple factory pattern is as follows:
This class diagram contains three characters:
- Factory class Role (Factory): This is the core of this model, which contains certain business logic and judgment logic. In Java, it is often implemented by a specific class.
- Abstract product Role (abstract Iphone Class): Typically the parent or implementation interface of a specific product inheritance, implemented in Java by a specific class.
- Specific product roles (Iphonex): The object created by the factory class is an instance of this role, implemented in Java by a concrete class.
In fact, if we want to use the new model of the mobile phone, we need to transform the Foxconnfactory class (In the class to add case) this violates the open and close principle, because in the use of new products, the factory class is a passive change. In order to solve this situation, there is a factory method pattern, the biggest difference between the factory method pattern and the simple Factory mode is that the simple Factory mode has only one (for a project or a standalone module) the factory class, and the factory method pattern has a set of factory classes that implement the same interface.
The factory method pattern provides multiple factory classes that implement the same interface, so that each factory class produces different types of products, so that if a new phone is added, it only needs to add a new factory class to produce the phone, instead of changing the existing factory class. This is in line with the opening and shutting principle.
Examples are as follows:
Public Abstract class iphone { public iphone () { }}
Public class extends iphone{ public Iphonex () { System.out.println ("##### manufacturing Iphonex #####");} }
Public class extends iphone{ public Iphone8 () { System.out.printf ("##### manufacturing Iphone8 #####" ); }}
Factory class:
Public Interface Factory { Iphone createiphone ();}
public class foxconnfactory implements factory{ /** * Create Iphonex phone * @return */ Span style= "color: #000000;" > @Override public Iphone Createiphone () { return new Iphonex (); }}
Public class Implements Factory { /** * Production of Iphone8 mobile phone @return */ @Override Public Iphone Createiphone () { returnnew Iphone8 (); }}
Customer class:
Public class Customer { /** * Use phone * / @Test public void Usephone () { new foxconnfactory (); New heshuofactory (); = Foxconn.createiphone (); = Heshuo.createiphone (); }}
The factory method pattern structure is as follows:
This chart contains four roles:
- Abstract Factory Role (Foundry Plant): This is the core of the factory method pattern, which is independent of the application and is the interface that the specific factory role must implement or the parent class that must inherit. In Java, he is implemented by an abstract class or interface.
- Specific factory role (Foxconn plant): It contains code that is relevant to the specific business logic. Called by the application to create the corresponding specific product object. In Java it is implemented by a specific class.
- Abstract product role (Apple Phone abstract class): It is a specific product that inherits the parent class or is an implemented interface. In Java, there are generally abstract classes or interfaces to implement.
- Specific product role (Iphonex phone): The object created by the specific factory role is an instance of this role. Implemented in Java by a specific class.
Although the factory method model solves the problem of the simple factory model's dissatisfaction with the open and closed principle, it also causes the cost of the code to increase, when the number of products is too large, maintenance will be very troublesome, but simple factory is not so troublesome, when multiple products need to be modified, the simple Factory mode still only need to modify the unique factory class. Either way, the purpose is to achieve functionality.
Compare to draw a personal conclusion: Simple Factory mode, more concise and convenient. The factory method model is more loosely and more advanced.
Java Design Pattern Learning record-Simple Factory mode, factory method mode