分类 设计模式 中的文章

设计模式-单例模式

概念 ​ 单例模式 是一种常见的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。 根据初始化的时机不同,单例模式又分为 懒汉式 和 饿汉式下面通过多个实例 懒汉式在第一次使用时创建实例对象, 饿汉式在程序启动时就创建实例对象。 饿汉式 type singleton struct {} var instance *singleton func init() { instance = new(singleton) } func GetInstance() *singleton { return instance } 饿汉式 是 线程……

阅读全文

建造者模式

概念 建造者模式一种创建型设计模式,它将一个复杂对象的构建过程与其表示分离开来,从而使得同样的构建过程可以创建不同的表示形式。 模式优点: 将对象的构建过程与具体表示分离:建造者模式通过将对象的构建过程与其表示分离开来,使得可以使用相同的构建过程来创建不同的表示形式。这种分离可以提高代……

阅读全文

工厂方法模式

实现代码 package factory // IRuleConfigParser IRuleConfigParser type IRuleConfigParser interface { Parse(data []byte) } // jsonRuleConfigParser jsonRuleConfigParser type jsonRuleConfigParser struct{} // Parse Parse func (J jsonRuleConfigParser) Parse(data []byte) { panic("implement me") } // yamlRuleConfigParser yamlRuleConfigParser type yamlRuleConfigParser struct{} // Parse Parse func (Y yamlRuleConfigParser) Parse(data []byte) { panic("implement me") } // IRuleConfigParserFactory 工厂方法接口 type IRuleConfigParserFactory interface { CreateParser() IRuleConfigParser } // yamlRuleConfigParserFactory yamlRuleConfigParser 的工厂类 type yamlRuleConfigParserFactory struct{} // CreateParser CreateParser func (y yamlRuleConfigParserFactory) CreateParser() IRuleConfigParser { return yamlRuleConfigParser{} } // jsonRuleConfigParserFactory jsonRuleConfigParser 的工厂类 type jsonRuleConfigParserFactory struct{} // CreateParser CreateParser func (j jsonRuleConfigParserFactory) CreateParser() IRuleConfigParser { return jsonRuleConfigParser{} } // NewIRuleConfigParserFactory 用一个简单工厂封装工厂方法 func NewIRuleConfigParserFactory(t string) IRuleConfigParserFactory { switch t { case "json": return jsonRuleConfigParserFactory{} case "yaml":……

阅读全文

原型模式

代码实现 package prototype import ( "encoding/json" "time" ) // Keyword 搜索关键字 type Keyword struct { word string visit int UpdatedAt *time.Time } // Clone 这里使用序列化与反序列化的方式深拷贝 func (k *Keyword) Clone() *Keyword { var newKeyword Keyword b, _ := json.Marshal(k) json.Unmarshal(b, &newKeyword) return &newKeyword } // Keywords 关键字 map type Keywords map[string]*Keyword // Clone 复制一个新的 keywords // updatedWords: 需要更新的关键词列表,由于从数据库中获取数据常常是数组的方式 func (words Keywords) Clone(updatedWords []*Keyword) Keywords { newKeywords := Keywords{} for k, v := range words { // 这里是浅拷……

阅读全文

适配器模式

代码实现 package adapter import "fmt" // ICreateServer 创建云主机 type ICreateServer interface { CreateServer(cpu, mem float64) error } // AWSClient aws sdk type AWSClient struct{} // RunInstance 启动实例 func (c *AWSClient) RunInstance(cpu, mem float64) error { fmt.Printf("aws client run success, cpu: %f, mem: %f", cpu, mem) return nil } // AwsClientAdapter 适配器 type AwsClientAdapter struct { Client AWSClient } // CreateServer 启动实例 func (a *AwsClientAdapter) CreateServer(cpu, mem float64) error { a.Client.RunInstance(cpu, mem) return nil } // AliyunClient aliyun sdk type AliyunClient struct{} // CreateServer 启动实例 func (c *AliyunClient) CreateServer(cpu, mem int) error { fmt.Printf("aws client run success, cpu: %d, mem: %d", cpu, mem) return nil } // AliyunClientAdapter 适配器 type AliyunClientAdapter struct { Client AliyunClient……

阅读全文

访问者模式

概念 优点 符合单一职责原则、优秀的扩展性、灵活性 缺点 具体元素对访问者公布细节,违反了迪米特原则 具体元素便跟比较困难 违反了依赖倒置原则,依赖了具体类,没有依赖抽象 应用场景 对象结构中对象对应的类很少改变,但经常需要在此对象结构上定义新的操作 需要对一个对象结构中的对象进行很多不同的并且不……

阅读全文

外观模式

概念 外观模式又叫门面模式,是一种常用的封装模式 给一系列具有关联性的子系统的集合提供对外访问的一组接口,调用者不用明确内部具体的业务逻辑,只需要调用这组接口达到目的即可。也就是要求子系统外部与内部不能直接进行通讯,必须通过一个统一的对象进行,而这个统一的对象就是门面。门面模式通过只……

阅读全文

简单工厂模式

概念 Golang 中没有构造函数,一般使用 NewXXX 或 New() 函数来初始化相关类, 在这个 simple 包中只有 API 接口和 NewGreeting 函数为包外可见,封装了实现细节。 使用场景 工厂类负责创建的对象比较少,客户只知道传入了工厂类的参数,对于始何创建对象(逻辑)不关心。 优点 工厂类含有必要的判断逻辑,可以决定在什么时候创建哪一个产品类的实……

阅读全文

策略模式

概念 策略模式是一种行为设计模式, 它将一组行为转换为对象, 并使其在原始上下文对象内部能够相互替换。 策略模式主要的作用还是解耦策略的定义、创建和使用,控制代码的复杂度,让每个部分都不至于过于复杂、代码量过多。 除此之外,对于复杂代码来说,策略模式还能让其满足开闭原则,添加新策略的时候,……

阅读全文