GoLang设计模式12 - 空对象模式
空对象设计模式是一种行为型设计模式,主要用于应对空对象的检查。使用这种设计模式可以避免对空对象进行检查。也就是说,在这种模式下,使用空对象不会造成异常。
空对象模式的组件包括:
- Entity:接口,定义了子struct需要实现的方法
- ConcreteEntity:实现了Entity 的具体struct
- NullEntity:这个就表示了空对象,虽然也实现了Entity接口,但它的值都是空的
- Client:这个类会获取Entity接口实现类的实例并使用它。这里并不关注实现类是
ConcreteEntity还是NullEntity,对二者会进行相同的处理。
用个例子来说一下:假设有一所大学,大学有多个系,每个系都有一定数量的教授。
系(department)可以用一个接口来表示:
type department interface {
getNumberOfProfessors() int
getName() string
}
大学(college)也是一个接口:
type college struct {
departments []department
}
现在假设有一个机构想统计下大学每个系的教授数量。
在这个例子里,假设大学里没有某个系,我们就会用到空对象模式。这里定义了一个nullDepartment来表示不存在的系。
nullDepartment.go:
type nullDepartment struct {
numberOfProfessors int
}
func (c *nullDepartment) getNumberOfProfessors() int {
return 0
}
func (c *nullDepartment) getName() string {
return "nullDepartment"
}
统计的代码在agency.go里:
func main() {
college1 := createCollege1()
college2 := createCollege2()
totalProfessors := 0
departmentArray := []string{"computerScience", "mechanical", "civil", "electronics"}
for _, departmentName := range departmentArray {
d := college1.getDepartment(departmentName)
totalProfessors += d.getNumberOfProfessors()
}
fmt.Printf("Total number of professors in college1 is %d\n", totalProfessors)
//Reset the professor count
totalProfessors = 0
for _, departmentName := range departmentArray {
d := college2.getDepartment(departmentName)
totalProfessors += d.getNumberOfProfessors()
}
fmt.Printf("Total number of professors in college2 is %d\n", totalProfessors)
}
func createCollege1() *college {
college := &college{}
college.addDepartment("computerScience", 4)
college.addDepartment("mechanical", 5)
return college
}
func createCollege2() *college {
college := &college{}
college.addDepartment("computerScience", 2)
return college
}
注意这段代码:
- agency.go 并不关心某个系在大学里是否存在。当这个系不存在时,大学只需要返回一个
nullDepartment对象即可 - agency.go 对
nullDepartment对象和其他department实现类的对象做了相同处理,这之中不需要对空值进行检查,直接调用getNumberOfProfessors()就可以了
以上就是使用空对象模式的好处了。
下面是其他的代码。
college.go:
type college struct {
departments []department
}
func (c *college) addDepartment(departmentName string, numOfProfessors int) {
if departmentName == "computerScience" {
computerScienceDepartment := &computerScience{numberOfProfessors: numOfProfessors}
c.departments = append(c.departments, computerScienceDepartment)
}
if departmentName == "mechanical" {
mechanicalDepartment := &mechanical{numberOfProfessors: numOfProfessors}
c.departments = append(c.departments, mechanicalDepartment)
}
return
}
func (c *college) getDepartment(departmentName string) department {
for _, department := range c.departments {
if department.getName() == departmentName {
return department
}
}
//Return a null department if the department doesn't exits
return &nullDepartment{}
}
计算机系,computerscience.go:
type computerScience struct {
numberOfProfessors int
}
func (c *computerScience) getNumberOfProfessors() int {
return c.numberOfProfessors
}
func (c *computerScience) getName() string {
return "computerScience"
}
数学系,mechanical.go:
type mechanical struct {
numberOfProfessors int
}
func (c *mechanical) getNumberOfProfessors() int {
return c.numberOfProfessors
}
func (c *mechanical) getName() string {
return "mechanical"
}
执行agency.go,输出内容如下:
Total number of professors in college1 is 9
Total number of professors in college2 is 2
End!!
GoLang设计模式12 - 空对象模式的更多相关文章
- 设计模式:空对象模式(Null Object Pattern)
设计模式:空对象模式(Null Object Pattern) 背景 群里聊到<ASP.NET设计模式>,这本书里有一个“Null Object Pattern”,大家就闲聊了一下这个模式 ...
- C# 设计模式之空对象模式
最近看了不少的书籍和视频等相关资料,决定自己边学习边写一下个人对设计模式的理解,如果有不对的请大家多多指正. 今天先说说我个人觉得最简单的设计模式 -- [空对象模式] 空对象模式可以减少客户端对对象 ...
- 设计模式之空对象模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象 ...
- Java进阶篇设计模式之十三 ---- 观察者模式和空对象模式
前言 在上一篇中我们学习了行为型模式的备忘录模式(Memento Pattern)和状态模式(Memento Pattern).本篇则来学习下行为型模式的最后两个模式,观察者模式(Observer P ...
- Java设计模式之十三 ---- 观察者模式和空对象模式
前言 在上一篇中我们学习了行为型模式的备忘录模式(Memento Pattern)和状态模式(Memento Pattern).本篇则来学习下行为型模式的最后两个模式,观察者模式(Observer P ...
- 被遗忘的设计模式——空对象模式(Null Object Pattern)
GoF(四人帮)那本<设计模式 可复用面向对象软件的基础>可谓是设计模式方面的经典之作,其中介绍的23种设计模式, 也可谓是经典中的经典.但是,设计模式的种类绝不仅仅是这23种,除此之外还 ...
- ASP.NET设计模式(一)、适配器模式、依赖注入依赖倒置、空对象模式
鸟随凤鸾,人伴贤良,得以共之,我之幸也.说的是鸟随着鸾凤可以飞的更高远,人和比自己境界高的相处,自己也会得到熏染进步. 一.概述 分享出来简单的心得,望探讨 依赖倒置 依赖注入 Adapter模式 N ...
- 【设计模式 - 21】之空对象模式(Null Object)
1 模式简介 在空对象模式中,一个空对象取代NULL对象的实例的检查.NULL对象不是检查空值,而是反映一个不做任何动作的关系.这样的NULL对象也可以在数据不可用的时候提供默认的行为. 在 ...
- 设计模式のNullObjectPattern(空对象模式)----行为模式
一.产生背景 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查.Null 对象不是检查空值,而是反应一个不做任何动作的关系.这样的 Null 对象也可 ...
随机推荐
- 直接取PHP二维数组里面的值
具体是这样的,如下一个二维数组,是从库中读取出来的. $user = array( 0 => array( 'id' => 1, 'name' => '张三', 'email ...
- python之jsonpath
json 官方文档:http://docs.python.org/library/json.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使 ...
- whistle手机抓包(以安卓手机为例)
环境:whistle:1.14.6 whistle手机抓包 以安卓手机为例 手机跟电脑要连同一个wifi. 1.启动whistle 使用w2 start启动whistle. 退出cmd后,whistl ...
- NOI.AC#2266-Bacteria【根号分治,倍增】
正题 题目链接:http://noi.ac/problem/2266 题目大意 给出\(n\)个点的一棵树,有一些边上有中转站(边长度为\(2\),中间有一个中转站),否则就是边长为\(1\). \( ...
- xLua自定义加载器
xLua入门基础 环境配置 github下载xLua文件: xLua是腾讯开发,据说比较先进: 下载下来后将Plugins和XLua文件夹考进项目: Plugins多平台权限:XLua和C#交互: t ...
- 程序员微机课系列—我的nodejs多版本管理方法
nodejs的多版本配置对于我来说一直都是一个较为头疼的事情.本人的开发工作会涉及electron以及前端,对于工作中使用的npm包(点名node-sqlite3和node-sass)在某些情况下,会 ...
- 工作日常-SQL不能乱写
前言:刚接手别人的项目没多久,在昨天的一次上线中无故躺坑,且该大兄弟已经离职,不得不帮他填坑,整完后,今天想搞一个总结,结论就是:SQL不能乱写. 搜索关键词:Cause: java.sql.SQLE ...
- NOI 2021 部分题目题解
最近几天复盘了一下NOI 2021,愈发发觉自己的愚蠢,可惜D2T3仍是不会,于是只写前面的题解 Day1 T1 可以发现,每次相当于将 \(x\to y\) 染上一种全新颜色,然后一条边是重边当且仅 ...
- vue介绍啊
声明式渲染:vue的核心是一个允许你才用一个简洁的模板语法来声明式的将数据渲染进行DOM的系统 html部分:<div id="app"> {{message}}< ...
- 【Java虚拟机5】Java内存模型(硬件层面的并发优化基础知识--指令乱序问题)
前言 其实之前大家都了解过volatile,它的第一个作用是保证内存可见,第二个作用是禁止指令重排序.今天系统学习下为什么CPU会指令重排. 存储器的层次结构图 1.CPU乱序执行指令的根源 CPU读 ...