GoLang设计模式12 - 空对象模式
2021-11-06 19:39
941 查看
空对象设计模式是一种行为型设计模式,主要用于应对空对象的检查。使用这种设计模式可以避免对空对象进行检查。也就是说,在这种模式下,使用空对象不会造成异常。
空对象模式的组件包括:
- 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 56c += 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
代码已上传至GitHub: zhyea / go-patterns / null-object-pattern
End!!
相关文章推荐
- C++设计模式12--建造者模式----创建复杂对象的工程师
- [导入]C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式).zip(7.86 MB)
- 预习(DAY 12):面向对象之多态、设计模式、基本的配置文件、抽象
- Webcast:C#面向对象设计模式纵横谈系列课程
- JavaSE实战——面向对象(上) 封装,继承,对象初始化流程,单例设计模式
- 23种设计模式(12)-享元模式
- 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
- [导入]C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式)
- 【设计模式】之面向对象基础一
- 设计模式之对象创建型 — abstract_factory 抽象工厂模式
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
- 【面向对象设计模式】 接口型模式 (一)
- 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)
- Java进阶篇设计模式之十三 ---- 观察者模式和空对象模式
- C#设计模式(12)-Decorator Pattern
- 面向对象及设计模式 温习笔记
- [导入]C#面向对象设计模式纵横谈(15):(行为型模式) Command 命令模式.zip(7.56 MB)
- [导入]C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式.zip(10.27 MB)
- 23种设计模式(12)_结构型_代理模式(Proxy Pattern)
- 面面向对象(上) 封装,继承,对象初始化流程,单例设计模式向对象(上) 封装,继承,对象初始化流程,单例设计模式