手撸golang 结构型设计模式 装璜器模式
缘起
最近温习设计模式
拜读谭勇德的<<设计模式就该这样学>>
本系列笔记拟采纳golang练习之
装璜器模式
装璜器模式(Decorator Pattern)也叫作包装器模式(Wrapper Pattern),指在不扭转原有对象的根底上,动静地给一个对象增加一些额定的职责。就减少性能来说,装璜器模式相比生成子类更为灵便,属于结构型设计模式。
_
装璜器模式vs代理模式
- 装璜器模式就是代理模式的一个非凡利用。
- 装璜器模式强调本身性能的扩大。
- 代理模式强调对代理过程的管制。
_
场景
- 某业务零碎, 输入日志信息到文件
- 现要求集成第三方日志剖析服务
- 日志服务须要json格局的日志流
- 因而须要革新原日志输入的实现, 实现json格局输入性能
设计
- ILogger: 定义日志器接口
- ILoggerFactory: 定义日志器工厂
- tConsoleLogger: 默认的日志器实现, 应用console输入日志音讯
- tConsoleLoggerFactory: 默认的日志器工厂
- tJsonLogger: 应用json格局输入日志音讯的日志器. 应用装璜器模式, 减少了将日志音讯格式化为json的性能.
- tJsonLoggerFactory: json日志器的工厂
单元测试 - decorator_pattern_test.go
通过切换日志工厂, 测试日志输入的不同成果
package structural_patternsimport ( "learning/gooop/structural_patterns/decorator" "testing")func Test_DecoratorPattern(t *testing.T) { fnTestLogger := func(factory decorator.ILoggerFactory) { logger := factory.GetLogger("testing.Test_DecoratorPattern") logger.Debug("This is a DEBUG msg") logger.Info("This is an INFO msg") } fnTestLogger(decorator.ConsoleLoggerFactory) fnTestLogger(decorator.JsonLoggerFactory)}
测试输入
$ go test -v decorator_pattern_test.go === RUN Test_DecoratorPattern2021-02-02T15:50:10 [testing.Test_DecoratorPattern] DEBUG This is a DEBUG msg2021-02-02T15:50:10 [testing.Test_DecoratorPattern] INFO This is an INFO msg2021-02-02T15:50:10 [testing.Test_DecoratorPattern] DEBUG { "Time": "2021-02-02T15:50:10", "Level": "DEBUG", "Msg": "This is a DEBUG msg"}2021-02-02T15:50:10 [testing.Test_DecoratorPattern] DEBUG { "Time": "2021-02-02T15:50:10", "Level": "INFO", "Msg": "This is an INFO msg"}--- PASS: Test_DecoratorPattern (0.00s)PASSok command-line-arguments 0.002s
ILogger.go
定义日志器接口
package decoratortype ILogger interface { Debug(msg string) Info(msg string) Warn(msg string) Error(msg string)}
ILoggerFactory.go
定义日志器工厂
package decoratortype ILoggerFactory interface { GetLogger(name string) ILogger}
tConsoleLogger.go
默认的日志器实现, 应用console输入日志音讯
package decoratorimport ( "fmt" "time")type tConsoleLogger struct { name string}func nowString() string { return time.Now().Format("2006-01-02T15:04:05")}func (me *tConsoleLogger) log(msg string, level string) { fmt.Printf("%s [%s] %s %s\n", nowString(), me.name, level, msg)}func (me *tConsoleLogger) Debug(msg string) { me.log(msg, "DEBUG")}func (me *tConsoleLogger) Info(msg string) { me.log(msg, "INFO")}func (me *tConsoleLogger) Warn(msg string) { me.log(msg, "WARN")}func (me *tConsoleLogger) Error(msg string) { me.log(msg, "ERROR")}
tConsoleLoggerFactory.go
默认的日志器工厂
package decoratortype tConsoleLoggerFactory struct {}func newConsoleLoggerFactory() ILoggerFactory { return &tConsoleLoggerFactory{}}func (me *tConsoleLoggerFactory) GetLogger(name string) ILogger { return &tConsoleLogger{ name, }}var ConsoleLoggerFactory = newConsoleLoggerFactory()
tJsonLogger.go
应用json格局输入日志音讯的日志器. 应用装璜器模式, 减少了将日志音讯格式化为json的性能.
package decoratorimport ( "encoding/json")type tJsonLogger struct { sName string mLogger ILogger}type tJsonMsg struct { Time string Level string Msg string}func (me *tJsonMsg) String() string { j, e := json.MarshalIndent(me, "", " ") if e != nil { return "" } return string(j)}func newJsonLogger(name string, logger ILogger) ILogger { return &tJsonLogger{ name, logger, }}func (me *tJsonLogger) toJson(msg string, level string) string { j := &tJsonMsg{ Time: nowString(), Level: level, Msg: msg, } return j.String()}func (me *tJsonLogger) Debug(msg string) { me.mLogger.Debug(me.toJson(msg, "DEBUG"))}func (me *tJsonLogger) Info(msg string) { me.mLogger.Debug(me.toJson(msg, "INFO"))}func (me *tJsonLogger) Warn(msg string) { me.mLogger.Debug(me.toJson(msg, "WARN"))}func (me *tJsonLogger) Error(msg string) { me.mLogger.Debug(me.toJson(msg, "ERROR"))}
tJsonLoggerFactory.go
json日志器的工厂
package decoratortype tJsonLoggerFactory struct {}func newJsonLoggerFactory() ILoggerFactory { return &tJsonLoggerFactory{}}func (me *tJsonLoggerFactory) GetLogger(name string) ILogger { logger := ConsoleLoggerFactory.GetLogger(name) return newJsonLogger(name, logger)}var JsonLoggerFactory = newJsonLoggerFactory()
装璜器模式小结
装璜器模式的长处
(1)装璜器是继承的无力补充,比继承灵便,在不扭转原有对象的状况下,动静地给一个对象扩大性能,即插即用。
(2)通过应用不同装璜类及这些装璜类的排列组合,能够实现不同成果。
(3)装璜器模式齐全恪守开闭准则。
装璜器模式的毛病
(1)会呈现更多的代码、更多的类,减少程序的复杂性。
(2)动静装璜在多层装璜时会更简单。
_
(end)