结构型设计模式——装饰模式

筅森魡賤 提交于 2019-11-26 17:07:46
  

这个玩意儿吧,其实用处就是为了在之后的项目中动态的给某一个对象更多的责任,或者说是执行方法,(当时程序是在运行时)它的设计方法的重点在那个Component接口,首先创建一个ConcreteComponent对象,将其作为参数传递到ConcreteDecoratorA这个装饰器内,这个装饰器给你返回一个全新的具有你原本功能的Component,实际上也就是把你的引用放进去,包一层返回给你。包的那一层就是它给你加的功能package mainimport "fmt"type Component interface {   operation()}type ConcreteComponent struct {}func(it *ConcreteComponent)operation(){   fmt.Println("asf")}type ConcreteDecoratorA struct {   concretecomponent Component}func(it *ConcreteDecoratorA)set(component Component ) Component{   it.concretecomponent = component   return it}func(it *ConcreteDecoratorA)operation(){   it.concretecomponent.operation()   fmt.Println("A")}func main(){   var com Component = new(ConcreteComponent)   com = (new(ConcreteDecoratorA).set(com))   com.operation()}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!