Simple Factory model
Wiki: The Simple Factory pattern is not one of GoF’s 23 classic design patterns, but it is often used as a basis for learning other factory patterns. Its design idea is simple, and its basic flow is as follows:
Firstly, the relevant codes of various objects to be created (such as various Chart objects) are encapsulated into different classes, which are called concrete product classes. After abstraction and extraction, their common codes are encapsulated into an abstract product class. Each concrete product class is a subclass of the abstract product class. We then provide a factory class for creating various products. In the factory class, we provide a factory method for creating products, which can create different concrete product objects depending on the parameters passed in. The client simply calls the factory method of the factory class and passes in the appropriate parameters to get a product object.
The simple factory pattern is defined as follows:
Simple Factory Pattern: Defines a Factory class that can return instances of different classes depending on the parameters. The created instances usually have a common parent class. Because the methods used to create instances in the simple Factory pattern are static methods, the simple Factory pattern is also known as the static Factory Method pattern, which belongs to the class creation pattern.
The point of the simple factory pattern is that when you need something, you can just pass in the right argument and get the object you need without knowing the details of how it was created. The structure of the simple factory pattern is relatively simple, and its core is the design of the factory class, as shown in the figure
I copied all the above…
Basically what we do is, when we want to create an object, we call the same method, pass in different arguments and we get different objects back
That is, of course, if the corresponding classes of these objects all implement the same interface
Such as:
We create a factory structure, and we create a product interface, and a factory can create a product, and just by passing different parameters in one of the factory methods, we can return different objects that implement the product interface,
- Create factory structure:
type Factory struct{}Copy the code
- To create a product interface, only one method has been written here for convenience. Please extend it to suit your needs
type Product interface {
create()
}
Copy the code
- Create two products: Product 1 and product 2, which implement the product interface:
// product 1 implements the product interface
type Product1 struct{}func (p1 Product1) create(a) {
fmt.Println("this is product 1")}// Product 2, implement product interface
type Product2 struct{}func (p1 Product2) create(a) {
fmt.Println("this is product 2")}Copy the code
- Add a method to the factory structure to produce the product (instantiated object) :
func (f Factory) Generate(name string) Product {
switch name {
case "product1":
return Product1{}
case "product2":
return Product2{}
default:
return nil}}Copy the code
- This allows you to get different product instances by passing in different methods:
// Create a factory class that can be used as a global variable in your application
factory := new(Factory)
// Pass different arguments in the factory class to get different instances
p1 := factory.Generate("product1")
p1.create() // output: this is product 1
p2 := factory.Generate("product2")
p2.create() // output: this is product 2
Copy the code
The above example is only the simplest example set up to explain the idea of the factory pattern. Here is an example of how it can be used in practice:
Binguo log is a log package of go language, which can customize the log output format. In this case, the simple factory mode is used. All structures that implement Connector interface can be passed into the log structure as parameters to achieve the purpose of customized output format
Project address: Bingu-log
Develop log processing package based on GO
Please go directly to the project readme. md to see the usage method, and go to the idea analysis to see the overall design idea
Here are the pros and cons of the factory model:
-
Advantages: Factory class is the core of the entire factory pattern, we only need to pass in the given information, we can create the required instance, in the collaboration of multiple people, without knowing the internal dependencies between objects, can be directly created, conducive to the optimization of the entire software architecture
-
Disadvantages: Factory class contains all create a logical instance, once the factory class appeared problem, all instances are affected, and, in the factory to produce the products based on a common interface, once you want to add a variety of products, this will increase the complexity of the factory class, will be different kinds of product mix in together, has violated the single responsibility, The system becomes less flexible and maintainable, and factory classes must be modified when new products are added, violating the “system is open for extension, closed for modification” principle
So we have more complex design patterns to accommodate more complex systems
Listen to the breakdown next time
The source code for this article is in this repository:Golang design pattern
Go web framework bingo, for star, for PR ~