The design pattern is simple like this (Golang version)-abstract factory pattern

Scenes

Boss: Design a universal factory. Currently, products A and B can be produced, and C, D may be produced in the future... These products belong to the same product family.
You: Good boss, then use the abstract factory model

Program

Products will increase over time. These products have similar attributes. We can call them a product family. We use an abstract factory (that is, a universal factory that can produce the same product family) to solve this problem, so that our code can be Unlimited expansion
Insert picture description here

achieve

see:github

package main

import "fmt"

type Product string

type AbstractFactory interface {
    
    
	createA() Product
	createB() Product
}

type Factory struct {
    
    
}

func (*Factory) createA() Product {
    
    
	fmt.Println("生产A")
	return Product("A")
}

func (*Factory) createB() Product {
    
    
	fmt.Println("生产B")
	return Product("B")
}

func main() {
    
    
	fmt.Println("从万能工厂那生产所有产品")
	fmt.Println("好的,老板")

	var f AbstractFactory
	f = &Factory{
    
    }
	f.createA()
	f.createB()
}

Guess you like

Origin blog.csdn.net/hayre/article/details/114905005