工厂方法模式
2. 简单工厂
2.1 概述
指在不指定具体产品类的情况下,为相互关联的产品簇或产品集(Families of Products)提供创建接口,并向客户端隐藏具体产品创建的细节或表示的对象。
2.2 代码示例
- 对
deployment
,daemonset
,statefulset
三个类,抽象出一个WorkLoad
类 - 该类有一个
创建工作负载
方法,该方法根据条件返回WorkLoad
的一个子类 - 创建一个Option接口,对三个子类进行操作
package main
import "fmt"
type Option interface {
Create() string
}
type Deployment struct {
WorkLoad WorkLoad
}
func (deployment *Deployment) Create() string {
return fmt.Sprintf("假装创建了%q:%q", deployment.WorkLoad.Kind, deployment.WorkLoad.Name)
}
type Daemonset struct {
WorkLoad WorkLoad
}
func (daemonset *Daemonset) Create() string {
return fmt.Sprintf("假装创建了%q:%q", daemonset.WorkLoad.Kind, daemonset.WorkLoad.Name)
}
type Statefulset struct {
WorkLoad WorkLoad
}
func (statefulset *Statefulset) Create() string {
return fmt.Sprintf("假装创建了%q:%q", statefulset.WorkLoad.Kind, statefulset.WorkLoad.Name)
}
type WorkLoad struct {
Name string
Kind string
}
func (workLoad *WorkLoad) setWorkLoad() Option {
switch workLoad.Kind {
case "Deployment":
return &Deployment{
WorkLoad: *workLoad,
}
case "Daemonset":
return &Daemonset{
WorkLoad: *workLoad,
}
case "Statefulset":
return &Statefulset{
WorkLoad: *workLoad,
}
}
return nil
}
func main() {
//简单工厂使用代码
workLoad := &WorkLoad{
Name: "nginx",
Kind: "Deployment",
}
parse := workLoad.setWorkLoad()
if parse != nil {
data := parse.Create()
fmt.Println(data)
}
}
- 输出
假装创建了"Deployment":"nginx"
2.3 类图
classDiagram
Deployment --|> WorkLoad
Daemenset --|> WorkLoad
Stagefulset --|> WorkLoad
Option <|.. Deployment
Option <|.. Daemenset
Option <|.. Stagefulset
class WorkLoad{
+string Name
+string Kind
+Option setWorkLoad()
}
class Deployment{
-WorkLoad WorkLoad
+Create()
}
class Daemenset{
-WorkLoad WorkLoad
+Create()
}
class Stagefulset{
-WorkLoad WorkLoad
+Create()
}
class Option{
<<interface>>
+string Create()
}
3. 工厂模式
3.1 概述
工厂方法类定义产品对象创建接口,但由子类实现具体产品对象的创建。
3.2 代码示例
package main
import (
"fmt"
)
type Option interface {
Create() string
}
type Deployment struct {
Name string
Kind string
}
func (deployment *Deployment) Create() string {
return fmt.Sprintf("假装创建了%q:%q", deployment.Kind, deployment.Name)
}
type Daemonset struct {
Name string
Kind string
}
func (daemonset *Daemonset) Create() string {
return fmt.Sprintf("假装创建了%q:%q", daemonset.Kind, daemonset.Name)
}
type Statefulset struct {
Name string
Kind string
}
func (statefulset *Statefulset) Create() string {
return fmt.Sprintf("假装创建了%q:%q", statefulset.Kind, statefulset.Name)
}
type WorkLoad struct {
Name string
Kind string
}
type Set interface {
SetWorkLoad(workLoad *WorkLoad) Option
}
type SetDeployment struct {
}
func (jsonFactory *SetDeployment) SetWorkLoad(workLoad *WorkLoad) Option {
deployemnt := &Deployment{
Name: workLoad.Name,
Kind: workLoad.Kind,
}
return deployemnt
}
type SetDaemonset struct {
}
func (setDaemonset *SetDaemonset) SetWorkLoad(workLoad *WorkLoad) Option {
daemonset := &Deployment{
Name: workLoad.Name,
Kind: workLoad.Kind,
}
return daemonset
}
type SetStatefulset struct {
}
func (setStatefulset *SetStatefulset) SetWorkLoad(workLoad *WorkLoad) Option {
statefulset := &Statefulset{
Name: workLoad.Name,
Kind: workLoad.Kind,
}
return statefulset
}
func SetWorkLoad(workLoad *WorkLoad) Set {
switch workLoad.Kind {
case "Deployment":
return &SetDeployment{}
case "Daemonset":
return &SetDaemonset{}
case "Statefulset":
return &SetStatefulset{}
}
return nil
}
func main() {
workLoad := &WorkLoad{
Name: "nginx",
Kind: "Deployment",
}
workLoadKind := SetWorkLoad(workLoad)
workLoadConf := workLoadKind.SetWorkLoad(workLoad)
if workLoadConf != nil {
data := workLoadConf.Create()
fmt.Println(data)
}
}
3.3 类图
classDiagram
Option <|.. Deployment
Option <|.. Daemenset
Option <|.. Stagefulset
SetDeployment ..|> Set
SetDaemenset ..|> Set
SetStagefulset ..|> Set
Deployment <.. SetDeployment
Daemenset <.. SetDaemenset
Stagefulset <.. SetStagefulset
class Deployment{
+string Name
+string Kind
+Create()
}
class Daemenset{
+string Name
+string Kind
+Create()
}
class Stagefulset{
+string Name
+string Kind
+Create()
}
class Option{
<<interface>>
+string Create()
}
class Set{
<<interface>>
+string Create()
}
class SetDeployment{
+Option SetWorkLoad()
}
class SetDaemenset{
+Option SetWorkLoad()
}
class SetStagefulset{
+Option SetWorkLoad()
}