zoukankan      html  css  js  c++  java
  • 结构型设计模式——装饰模式

    
    
    ../_images/Decorator.jpg

    这个玩意儿吧,其实用处就是为了在之后的项目中动态的给某一个对象更多的责任,或者说是执行方法,(当时程序是在运行时)
    它的设计方法的重点在那个Component接口,首先创建一个ConcreteComponent对象,将其作为参数传递到ConcreteDecoratorA这个装饰器内,这个
    装饰器给你返回一个全新的具有你原本功能的Component,实际上也就是把你的引用放进去,包一层返回给你。包的那一层就是它给你加的功能
    package main

    import "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()
    }

  • 相关阅读:
    弹飞绵羊
    POJ 3308
    狼抓兔子
    块状链表题*1
    块状链表
    双向链表
    Linux入职基础-1.2_U盘安装RedHat5具体步骤
    Linux入职基础-1.1_国内开源的主要镜像站
    VS.NET(C#)--2.9_HTML服务器控件案例
    VS2015按钮方法
  • 原文地址:https://www.cnblogs.com/mcmx/p/11328279.html
Copyright © 2011-2022 走看看