From 2dd4c5aee1c71ca18168f8c0ac76aad164540c5c Mon Sep 17 00:00:00 2001 From: Edward Date: Sat, 2 May 2020 18:13:16 +0800 Subject: [PATCH] finish decorator pattern --- structure/06_decorator/README.md | 8 +++- structure/06_decorator/decorator.go | 58 ++++++++++++------------ structure/06_decorator/decorator_test.go | 26 +++++++---- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/structure/06_decorator/README.md b/structure/06_decorator/README.md index 6983a86..bb2f9f7 100644 --- a/structure/06_decorator/README.md +++ b/structure/06_decorator/README.md @@ -1,5 +1,11 @@ # 装饰模式 -装饰模式就是比较简单了,就是在不改变原有对象和类型的情况下,对其进行装饰,扩展,以达到公共强化和扩展的目的,装饰模式的目的是在原有功能或者能力的基础上提供额外的更多的能力和特性,这一点和代理模式、复合模式是有很大不同的,了解每一种模式,重点是要了解这个模式的最初的设计者应用场景和其解决目的,以便加以利用。 +装饰模式就是比较简单了,其目的是在不改变原有类型对象的情况下,对其进行装饰,扩展,以达到公共强化和扩展的目的。 + +装饰模式的目的是在原有功能或者能力的基础上提供额外的更多的能力和特性,这一点和代理模式、复合模式是有很大不同的,了解每一种模式,重点是要了解这个模式的最初的设计者应用场景和其解决目的,以便加以利用。 Go语言借助于匿名组合和非入侵式接口可以很方便实现装饰模式。使用匿名组合,在装饰器中不必显式定义转调原对象方法。 + +现实生活中例子就太多了,可以想一下带 "装饰"的东西,车子不漂亮,搞个车贴,脸不好看,化个妆,屋墙不好看,贴个墙纸。 + +都是在在不改变原有东西的情况下,做一些扩展和强化。 diff --git a/structure/06_decorator/decorator.go b/structure/06_decorator/decorator.go index 829eeaa..e268d47 100644 --- a/structure/06_decorator/decorator.go +++ b/structure/06_decorator/decorator.go @@ -1,43 +1,43 @@ package decorator -type Component interface { - Calc() int +//////////////////////////////// +//拿化妆做例子 + +//IFaceLooks 脸部的颜值 +type IFaceLooks interface { + FaceLooks() int } -type ConcreteComponent struct{} - -func (*ConcreteComponent) Calc() int { - return 0 +//NatureGirl 天然无化妆的小姐姐 +type NatureGirl struct { + faceValue int } -type MulDecorator struct { - Component - num int +//FaceLooks 获取小姐姐的颜值 +func (n *NatureGirl) FaceLooks() int { + return n.faceValue } -func WarpMulDecorator(c Component, num int) Component { - return &MulDecorator{ - Component: c, - num: num, +//GirlWithMakeups 化妆后的小姐姐 +type GirlWithMakeups struct { + origin IFaceLooks //这就是那个自然美的小姐姐 + facePlus int //脸部加成,说,你想化成什么样子吧。 +} + +//NewGirlWithMakeup 返回一个化妆后的小姐姐 +func NewGirlWithMakeup(origin IFaceLooks, facePlus int) IFaceLooks { + return &GirlWithMakeups{ + origin: origin, + facePlus: facePlus, } } -func (d *MulDecorator) Calc() int { - return d.Component.Calc() * d.num +//FaceLooks 我要开始化妆了.. +func (g *GirlWithMakeups) FaceLooks() int { + return g.origin.FaceLooks() + g.facePlus } -type AddDecorator struct { - Component - num int -} - -func WarpAddDecorator(c Component, num int) Component { - return &AddDecorator{ - Component: c, - num: num, - } -} - -func (d *AddDecorator) Calc() int { - return d.Component.Calc() + d.num +//FaceReal 实际的颜值.. +func (g *GirlWithMakeups) FaceReal() int { + return g.origin.FaceLooks() } diff --git a/structure/06_decorator/decorator_test.go b/structure/06_decorator/decorator_test.go index bf46b22..5d77ea2 100644 --- a/structure/06_decorator/decorator_test.go +++ b/structure/06_decorator/decorator_test.go @@ -1,14 +1,22 @@ package decorator -import "fmt" +import ( + "fmt" + "testing" +) -func ExampleDecorator() { - var c Component = &ConcreteComponent{} - c = WarpAddDecorator(c, 10) - c = WarpMulDecorator(c, 8) - res := c.Calc() +func TestDecorateNatureGirl(t *testing.T) { + + //准备天然的妹子一枚 + origin := &NatureGirl{faceValue: 6} + + fmt.Println("face looks ", origin.FaceLooks()) + + //只需要略施粉黛 + makeupGirl := NewGirlWithMakeup(origin, 2) + + fmt.Println("after makeup face looks ", makeupGirl.FaceLooks()) + + fmt.Println("girl's real face ", makeupGirl.(*GirlWithMakeups).FaceReal()) - fmt.Printf("res %d\n", res) - // Output: - // res 80 }