• 《golang设计模式》第二部分·结构型模式-04-装饰器模式(Decorator)


    1.概述

    装饰器(Decorator)通过包装(不是继承)的方式向目标对象中动态地添加或删除功能。

    1.1 说明

    • Component(抽象组件):定义了原始对象的接口,装饰器也会实现这个接口。
    • Concrete Component(具体组件):原始对象,以后装饰器会装饰它。
    • Decorator(抽象装饰器):关联/聚合了抽象组件,并实现了抽象组件接口。
    • Concrete Decorator(具体装饰器):继承或实现了抽象装饰器,负责向抽象组件添加新的功能。

    1.2 类图

    «interface»
    Component
    +Server()
    ConcreteComponent
    +Server()
    Decorator
    +Server()
    ConcreteDecoratorA
    +Server()
    ConcreteDecoratorB
    +Server()

    2.代码示例

    2.1代码

    package main
    
    import "fmt"
    
    // 定义一个抽象组件
    type Component interface {
    	Get()
    }
    
    // 定义一个具体组件
    type ConcreteComponent struct {
    	name string
    }
    
    // 具体组件有一个查看方法
    func (c *ConcreteComponent) Get() {
    	fmt.Printf("%+v\n", c)
    }
    
    // 定义一个装饰器,它实现了抽象组件,同时它也关联了抽象组件
    type Decorator struct {
    	Component   Component
    	DecorationA string
    	DecorationB string
    }
    
    func (d *Decorator) Get() {
    	d.Component.Get()
    	fmt.Printf("实现装饰:%q,%q", d.DecorationA, d.DecorationB)
    }
    
    //创建一个具体装饰器A
    type DecoratorA struct {
    	Decorator *Decorator
    }
    
    //Set模式扩展功能
    func (d *DecoratorA) Set(decoration string) {
    	d.Decorator.DecorationA = decoration
    }
    //创建一个装饰器B
    type DecoratorB struct {
    	Decorator *Decorator
    	//DecorationA string
    }
    //Set模式扩展功能
    func (d DecoratorB) Set(decoration string) {
    	d.Decorator.DecorationB = decoration
    }
    
    func main() {
    	//已经有一个具体组件如下
    	concreteComponent := &ConcreteComponent{
    		name: "马户",
    	}
    
    	//下边用装饰器扩展一些功能
    	//实例化一个装饰器
    	decorator := &Decorator{
    		Component: concreteComponent,
    	}
    
    	//实例化具体装饰器A
    	decoratorA := DecoratorA{
    		Decorator: decorator,
    	}
    	//用装饰器A装饰
    	decoratorA.Set("两耳垂肩")
    
    	//实例化一个装饰器B
    	decoratorB := DecoratorB{
    		Decorator: decorator,
    	}
    	//用装饰器B装饰
    	decoratorB.Set("三孔鼻")
    
    	//查看结果
    	decorator.Get()
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 输出
    &{name:马户}
    实现装饰:"两耳垂肩","三孔鼻"
    
    • 1
    • 2

    2.2 示例的类图

    设计中,我们并没有一个抽象的装饰器,而是每个实际的装饰器都继承了worker;

    «interface»
    Component
    +Get()
    ConcreteComponent
    +Name:string
    +Get()
    Decorator
    +Component:Component
    +DecorationA:string
    +DecorationB:string
    +Get()
    DecoratorA
    +Decorator *Decorator
    +Set()
    DecoratorB
    +Decorator *Decorator
    +Set()

    在这里插入图片描述

  • 相关阅读:
    基于PHP+MySQL的校园餐厅展示订餐系统
    STC设计和RTX51--核心板设计
    Windows上安装 RabbitMQ 教程
    代码随想录学习Day 29
    c++ 服务器之插件框架开发
    Java 泛型中的通配符
    Docker的基本操作
    第一章 | 计算机网络原理 谢希仁(第八版)_ 习题答案
    比较图片相似度算法介绍与应用(Java版)
    基于ES6的文章发布系统的设计与实现
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/131819218