Go实现常用软件设计模式二:工厂模式
目录:
- 举个栗子
- 概念介绍
- 使用场景
1.举个栗子
类图
```
@startuml
'https://plantuml.com/class-diagram
class Elephant {
String name
String getName()
}
interface Container {
List<Elephant> elephants
void add(Elephant elephants)
}
class Refrigerator {
List<Elephant> elephants;
void add(Elephant elephants);
}
class Bowl {
List<Elephant> elephants
void add(Elephant elephants)
}
class Box {
List<Elephant> elephants
void add(Elephant elephants)
}
Container <|- Refrigerator
Container <|- Bowl
Container <|- Box
class ContainerFactory {
Container getInstance(String containerType)
}
class ContainerCounter {
Container container
void countTotalNum()
}
class Main {
void main();
}
@enduml
```
2.概念介绍
创建一个中间件来分离创建对象和使用对象的逻辑
优点:
将创建对象的逻辑放在一起
扩展新的相同行为的对象无需修改使用者的逻辑
封装复杂的对象创建逻辑
缺点:
多了一个工厂类
3.使用场景
main.go
```
package main
import (
"dbTest/Builder/factory/container/containerFactory"
"dbTest/Builder/factory/containerCounter"
"dbTest/Builder/factory/elephant"
"fmt"
"time"
)
func main() {
fmt.Println("start add elephant")
elephants := elephant.Elephant{
"rampage",
}
var containerType string
containerType = "refrigerator"
Contains := containerFactory.GetInstance(containerType)
Contains.AddElephant(elephants)
containerCount := containerCounter.ContainerCounter{}
for i := 0; i < 10; i++ {
go containerCount.CounterNum(Contains)
}
time.Sleep(time.Second)
fmt.Println("we are finished, nums:",Contains.GetTotalNum())
}
```
elephat.go
```
package elephant
type Elephant struct {
Name string
}
func (e Elephant) getName() string {
return e.Name
}
```
containerCounter.go
```
package containerCounter
import (
"dbTest/Builder/factory/container/containerInterface"
"fmt"
)
type ContainerCounter struct {
}
func (rc ContainerCounter) CounterNum(container containerInterface.Container) {
fmt.Println("ContainerCounter:",container.GetTotalNum())
return
}
```
containerFactory.go
```
package containerFactory
import (
"dbTest/Builder/factory/container/bowl"
"dbTest/Builder/factory/container/box"
"dbTest/Builder/factory/container/containerInterface"
"dbTest/Builder/factory/container/refrigerator"
)
func GetInstance(containerType string) containerInterface.Container{
switch containerType {
case "bowl":
return &bowl.Bowl{}
case "box":
return &box.Box{}
case "refrigerator":
return &refrigerator.Refrigerator{}
}
return nil
}
```
container.go
```
package containerInterface
import "dbTest/Builder/factory/elephant"
type Container interface {
AddElephant(elephant elephant.Elephant)
GetTotalNum() int
}
```
refrigerator.go
```
package refrigerator
import (
"dbTest/Builder/factory/elephant"
)
// 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
type Refrigerator struct {
elephants []elephant.Elephant
}
func (r *Refrigerator) AddElephant(elephants elephant.Elephant) {
r.elephants = append(r.elephants, elephants)
}
func (r *Refrigerator) GetTotalNum() int {
return len(r.elephants)
}
```
box.go
```
package box
import "dbTest/Builder/factory/elephant"
// 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
type Box struct {
elephants []elephant.Elephant
}
func (r *Box) AddElephant(elephants elephant.Elephant) {
r.elephants = append(r.elephants, elephants)
}
func (r *Box) GetTotalNum() int {
return len(r.elephants)
}
```
bowl.go
```
package bowl
import "dbTest/Builder/factory/elephant"
// 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
type Bowl struct {
elephants []elephant.Elephant
}
func (r *Bowl) AddElephant(elephants elephant.Elephant) {
r.elephants = append(r.elephants, elephants)
}
func (r *Bowl) GetTotalNum() int {
return len(r.elephants)
}
```
本文来自博客园,作者:易先讯,转载请注明原文链接:https://www.cnblogs.com/gongxianjin/p/16443149.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)