当前位置: 首页 > news >正文

原型设计模式go实现尝试

文章目录

  • 前言
  • 代码
  • 结果
  • 总结


前言

本文章尝试使用go实现“原型”。


代码

package mainimport ("fmt"
)// 不同原型标志枚举
type Type intconst (PROTOTYPE_1 Type = iotaPROTOTYPE_2
)// 原型接口
type IPrototype interface {Clone() IPrototypeMethod(value int)Print()
}// 具体原型1
type ConcretePrototype1 struct {name  stringvalue int
}// 构造函数
func NewConcretePrototype1ByFields(name string, value int) *ConcretePrototype1 {return &ConcretePrototype1{name,value,}
}func NewConcretePrototype1ByObject(cp *ConcretePrototype1) *ConcretePrototype1 {return &ConcretePrototype1{name:  cp.name,value: cp.value,}
}// 接口方法
func (cp *ConcretePrototype1) Clone() IPrototype {return NewConcretePrototype1ByObject(cp)
}func (cp *ConcretePrototype1) Method(value int) {cp.value = value
}func (cp *ConcretePrototype1) Print() {fmt.Println("Call Method1 from ", cp.name, " with field : ", cp.value)
}// 具体原型2
type ConcretePrototype2 struct {name  stringvalue int
}// 构造函数
func NewConcretePrototype2ByFields(name string, value int) *ConcretePrototype2 {return &ConcretePrototype2{name,value,}
}func NewConcretePrototype2ByObject(cp *ConcretePrototype2) *ConcretePrototype2 {return &ConcretePrototype2{name:  cp.name,value: cp.value,}
}// 接口方法
func (cp *ConcretePrototype2) Clone() IPrototype {return NewConcretePrototype2ByObject(cp)
}func (cp *ConcretePrototype2) Method(value int) {cp.value = value
}func (cp *ConcretePrototype2) Print() {fmt.Println("Call Method2 from ", cp.name, " with field : ", cp.value)
}// 原型工厂
type PrototypeFactory struct {prototypes map[Type]IPrototype
}func NewPrototypeFactory() *PrototypeFactory {return &PrototypeFactory{prototypes: map[Type]IPrototype{PROTOTYPE_1: NewConcretePrototype1ByFields("PROTOTYPE_1 ", 1),PROTOTYPE_2: NewConcretePrototype2ByFields("PROTOTYPE_2 ", 2),},}
}func (p *PrototypeFactory) CreatePrototype(t Type) IPrototype {return p.prototypes[t].Clone()
}// 客户端代码
func clientCode(p *PrototypeFactory) {fmt.Println("Let's create a Prototype 1")prototype1 := p.CreatePrototype(PROTOTYPE_1)prototype2 := p.CreatePrototype(PROTOTYPE_1)prototype1.Method(3)prototype2.Method(4)prototype1.Print()prototype2.Print()fmt.Println()fmt.Println("Let's create a Prototype 2")prototype1 = p.CreatePrototype(PROTOTYPE_2)prototype2 = p.CreatePrototype(PROTOTYPE_2)prototype1.Method(5)prototype2.Method(6)prototype1.Print()prototype2.Print()
}func main() {clientCode(NewPrototypeFactory())
}

结果

Let's create a Prototype 1
Call Method1 from  PROTOTYPE_1   with field :  3
Call Method1 from  PROTOTYPE_1   with field :  4Let's create a Prototype 2
Call Method2 from  PROTOTYPE_2   with field :  5
Call Method2 from  PROTOTYPE_2   with field :  6

总结

新人设计模式理解,望大家多多指点。

http://www.lryc.cn/news/100418.html

相关文章:

  • 链表是否有环、环长度、环起点
  • 有效文档管理离不开这几个特点
  • 爬虫-requests-cookie登录古诗文网
  • Spring Boot实践三 --数据库
  • 分布式锁漫谈
  • mac 安装 php 与 hyperf 框架依赖的扩展并启动 gptlink 项目
  • ansible中run_once的详细介绍和使用说明
  • 短视频矩阵系统源码开发流程​
  • vite+vue3 css scss PC移动布局自适应
  • BLE配对和绑定
  • 无涯教程-jQuery - html( val )方法函数
  • 【单链表OJ题:删除链表中等于给定值 val 的所有节点】
  • vue element ui web端引入百度地图,并获取经纬度
  • 25.10 matlab里面的10中优化方法介绍—— 函数fmincon(matlab程序)
  • 赛效:如何将PDF文件免费转换成Word文档
  • java 8 的Stream API
  • TypeChat,用TypeScript快速接入AI大语言模型
  • Dcoker compose单机容器集群编排管理
  • P5635 【CSGRound1】天下第一(记忆化搜索)
  • 如何维护你的电脑:提升性能和延长使用寿命
  • Docker续集+Docker Compose
  • k8s deployment(k8s经典版)|PetaExpress
  • uni-app如何生成正式的APK
  • 低代码开发平台源码:可视化敏捷开发工具,拖拽式自定义表单界面
  • 利用读时建模等数据分析能力,实现网络安全态势感知的落地
  • 六、代理模式
  • Easy Glide
  • ppt怎么压缩到10m以内?分享好用的压缩方法
  • VBA技术资料MF35:VBA_在Excel中过滤数据
  • Debian12中为python3配置虚拟环境及在Pycharm中使用虚拟环境