Go实现常用软件设计模式二:工厂模式

目录:

  1. 举个栗子
  2. 概念介绍
  3. 使用场景

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)
}

```

 

posted @ 2022-07-04 15:36  易先讯  阅读(52)  评论(0编辑  收藏  举报