Factory mode (Factory method)
The factory model is suitable : When a large number of products need to be created, and have a common interface, can be created through the factory method mode. In the following three modes, the first if the passed string is incorrect, the object cannot be created correctly, the third is not required for the second type, so, in most cases, we will choose the third-static factory method mode.
1: normal Factory mode
is to create a factory class that creates instances of some classes that implement the same interface
example, press write a common factory method mode
First, create a common interface sender
Public Interface Sender { publicvoid Send (); }
Then each creates the implementation class
Implementing Class 1:mailsender
Public class Implements Sender { @Override publicvoid Send () { System.out.println ("This is mailsender! " ); } }
Implementing Class 2:smssender
Public class Implements Sender { @Override publicvoid Send () { System.out.println ( "This is SMS sender!" ); } }
Create a common factory release sendfactory
Public classSendfactory { PublicSender Produce (String type) {if("Mail". Equals (Type) { return NewMailSender (); } Else if("SMS". Equals (Type) { return NewSmssender (); } Else{System.out.println ("Please enter the correct type!"); return NULL; } } }
Test: Call the factory method, pass the condition to think which subclass to use
Public class factorytest { publicstaticvoid main (string[] args) { new sendfactory (); = Factory.produce ("SMS"); Sender. Send (); } }
Output: This is SMS sender!
2: Multiple Factory method modes
Multiple factory method patterns are an improvement to the common factory method pattern, in which the object is not created correctly if a string error is passed in the normal factory method pattern, and multiple factory method patterns provide multiple factory methods to create the object individually.
Example: (The difference from the normal Factory mode calls the respective method return for the respective subclass)
Public class sendfactory { public Sender producemail () { returnnew MailSender (); } Public Sender producesms () { returnnew smssender (); } }
3: Static Factory method mode
The method in the above multiple factory method pattern is set to static, do not need to create the instance factory class, call directly.
Public class sendfactory { publicstatic Sender Producemail () { return New MailSender (); } Public Static Sender producesms () { returnnew smssender (); } }
The difference on the call
Public class factorytest { publicstaticvoid main (string[] args) { = Sendfactory.producemail (); Sender. Send (); } }
The problem with factory method mode is that class creation relies on the factory class, that is, if you want to expand the program, the factory class must be modified, which violates the closure principle, so from the design point of view, there are certain problems, how to solve? Use the abstract factory pattern to create multiple factory classes so that once you need to add new functionality, you can add new factory classes directly, without having to modify the previous code. Because the abstract factory is not very well understood, we first look at the diagram, and then the code, it is more easily understood.
Java design mode-factory method Mode (Factory methods)