Introduced
Intent: Define an interface that creates an object, letting its subclasses decide which factory class to instantiate, and the factory pattern to defer its creation to subclasses.
Main Solution: The main solution to the problem of interface selection.
when to use: we explicitly plan to create different instances under different conditions.
How to solve: let its sub-class implement the Factory interface, return is also an abstract product.
Key code: The creation process executes in its subclasses.
Application Example: 1, you need a car, you can pick up directly from the factory, without having to control how the car is done, and the specific implementation of the car. 2, Hibernate Exchange database only need to change dialect and drive.
Pros: 1, a caller wants to create an object, just know its name. 2, high scalability, if you want to add a product, as long as the expansion of a factory class can be. 3, the specific implementation of shielding products, the caller only care about the interface of the product.
disadvantage: Each time you add a product, you need to add a specific class and object to implement the factory, so that the number of classes in the system multiplied, to a certain extent, increase the complexity of the system, but also increase the system specific class dependency. That's not a good thing.
usage Scenario: 1, Logger: Records may be logged to local hard disk, system events, remote server, etc., the user can choose where to log logs. 2, database access, when the user is not aware of the last system to use which type of database, and the database may change. 3, design a connection server framework, requires three protocols, "POP3", "IMAP", "HTTP", you can use these three as product classes, together to implement an interface.
Note: as a way to create a class pattern, you can use the factory method pattern in any place where complex objects need to be generated. One thing to keep in mind is that complex objects are suitable for Factory mode, and simple objects, especially those that can be created only through new, do not need to use Factory mode. If you use Factory mode, you need to introduce a factory class that increases the complexity of the system.
Realize
We will create a shape interface and an entity class that implements the shape interface. The next step is to define the factory class Shapefactory.
Factorypatterndemo, our demo class uses shapefactory to get the Shape object. It will pass information (Circle/rectangle/square) to the shapefactory to get the type of object it needs.
Step 1
Create an interface.
Public Interface Shape { void Draw ();}
Step 2
Create an entity class that implements the interface.
Public class Implements Shape { @Override publicvoid Draw () { System.out.println ("Inside Rectangle::d Raw () method. " ); }}
Public class Implements Shape { @Override publicvoid Draw () { System.out.println ( "Inside Square::d Raw () method." ); }}
Public class Implements Shape { @Override publicvoid Draw () { System.out.println ("Inside Circle::d Raw () method. ); }}
Step 3
Creates a factory that generates an object based on the entity class of the given information.
Public classShapefactory {//to get the object of a shape type using the Getshape method PublicShape Getshape (String shapetype) {if(ShapeType = =NULL){ return NULL; } if(Shapetype.equalsignorecase ("CIRCLE")){ return NewCircle (); } Else if(Shapetype.equalsignorecase ("RECTANGLE")){ return NewRectangle (); } Else if(Shapetype.equalsignorecase ("SQUARE")){ return NewSquare (); } return NULL; }}
Step 4
Use this factory to get the object of an entity class by passing type information.
Public classFactorypatterndemo { Public Static voidMain (string[] args) {shapefactory shapefactory=Newshapefactory (); //gets the Circle object and calls its Draw methodShape shape1 = Shapefactory.getshape ("CIRCLE"); //call the Draw method of CircleShape1.draw (); //gets the Rectangle object and calls its Draw methodShape shape2 = Shapefactory.getshape ("RECTANGLE"); //call the Draw method of RectangleShape2.draw (); //gets the Square object and calls its Draw methodShape shape3 = Shapefactory.getshape ("SQUARE"); //call Square's Draw methodShape3.draw (); }}
Step 5
Verify the output.
Inside Circle::d Raw () method. Inside Rectangle::d Raw () method. Inside Square::d Raw () method.
"Design mode" Factory mode