Go语言装饰器模式功能扩展1. 装饰器实现type Component interface { Operation() string } type ConcreteComponent struct{} func (c *ConcreteComponent) Operation() string { return ConcreteComponent } type Decorator struct { component Component } func (d *Decorator) Operation() string { return d.component.Operation() } type LoggingDecorator struct { Decorator } func (d *LoggingDecorator) Operation() string { fmt.Println(Before operation) result : d.Decorator.Operation() fmt.Println(After operation) return result }2. 总结装饰器模式动态给对象添加额外职责比继承更灵活。
Go语言装饰器模式:功能扩展
Go语言装饰器模式功能扩展1. 装饰器实现type Component interface { Operation() string } type ConcreteComponent struct{} func (c *ConcreteComponent) Operation() string { return ConcreteComponent } type Decorator struct { component Component } func (d *Decorator) Operation() string { return d.component.Operation() } type LoggingDecorator struct { Decorator } func (d *LoggingDecorator) Operation() string { fmt.Println(Before operation) result : d.Decorator.Operation() fmt.Println(After operation) return result }2. 总结装饰器模式动态给对象添加额外职责比继承更灵活。