Factory mode
Factory mode (Factory pattern) is one of the most common design patterns in Java. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.
In Factory mode, we create the object without exposing the creation logic to the client, and by using a common interface to point to the newly created object.
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
Type Shape Interface {Draw ()}
Step 2
Create an entity class that implements the interface.
Type Rectangle struct {}func (this Rectangle) Draw () {fmt. Println ("Inside Rectangle::d Raw () method.")} Type Square struct {}func (this Square) Draw () {fmt. Println ("Inside Square::d Raw () method.")} Type Circle struct {}func (this Circle) Draw () {fmt. Println ("Inside Circle ::d Raw () method.")}
Step 3
First give me 100 billion, no, first create a factory, Haha, the object that generates an entity class based on the given information.
Type shapefactory struct {}//uses the Getshape method to get the object of the shape type func (this shapefactory) Getshape (ShapeType string) shape {if Shapetyp E = = "" {return nil}if ShapeType = = "CIRCLE" {return circle{}} else if ShapeType = = "RECTANGLE" {return rectangle{}} else if ShapeType = = "SQUARE" {return Square{}}return nil}
Step 4
Use this factory to get the object of an entity class by passing type information.
Func Main () {factory: = Shapefactory{}factory.getshape ("CIRCLE"). Draw () factory.getshape ("RECTANGLE"). Draw () factory.getshape ("SQUARE"). Draw ()}
Step 5
Execute program, output result:
Inside Circle ::d Raw () method. Inside Rectangle::d Raw () method. Inside Square::d Raw () method.