Golang设计模式——22装饰者模式

装饰者模式允许在不修改对象的基础上增加额外职责,提供比继承更灵活的解决方案。文章通过代码示例展示了如何在Go中实现装饰者模式,包括装饰器的基本结构和应用场景,如动态添加对象功能、撤销功能等。同时,还介绍了如何利用装饰器模式优化计算π的性能,并结合日志记录进行功能装饰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

装饰者模式

优点

  1. 是指在不改变原有对象的基础之上,将功能附加到对象上.提供了比继承更灵活的替代方法,属于结构型模式
  2. 动态的给一个对象增加一些额外的职责,就增加功能来说,装饰模式相比生产子类更为灵活

缺点

  1. 使用装饰模式进行系统设计时将产生很多小对象,这些对象的区别在于它们之间相互连接的方式有所不同,而不是它们的类或者属性值有所不同,大量小对象的产生势必会占用更多的系统资源,在一定程序上影响程序的性能。
  2. 装饰模式提供了一种比继承更加灵活机动的解决方案,但同时也意味着比继承更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为繁琐。

场景

  1. 用于拓展一个类的功能或者给一个类添加附加职责
  2. 动态的给一个对象添加功能,这些功能可以再动态的撤销。
  3. 需要为一批的兄弟类进行改装或加装功能。

代码

在这里插入图片描述

package Decorator

import "fmt"

type Component interface {
	Operation()
}
type ConcreteComponent struct {
}

func (c *ConcreteComponent) Operation() {
	fmt.Println("具体的对象开始操作...")
}

type Decorator struct {
	cc Component
}

func (d *Decorator) SetComponent(c Component) {
	d.cc = c
}

func (d *Decorator) Operation() {
	if d.cc != nil {
		d.cc.Operation()
	}
}

type DecoratorA struct {
	Decorator
}

func (d *DecoratorA) Operation() {
	d.cc.Operation()
	d.IndependentMethod()
}
func (d *DecoratorA) IndependentMethod() {
	fmt.Println("装饰A扩展的方法")
}

type DecoratorB struct {
	Decorator
}

func (d *DecoratorB) Operation() {
	d.cc.Operation()
	fmt.Println(d.String())
}
func (d *DecoratorB) String() string {
	return "装饰B扩展的方法"
}

package Decorator

import "testing"

func TestDecorator_Operation(t *testing.T) {
	objectA := &ConcreteComponent{}
	cdA := &DecoratorA{}
	cdB := &DecoratorB{}
	cdA.SetComponent(objectA)
	cdB.SetComponent(cdA)
	cdB.Operation()
}

package Decorator

import (
	"log"
	"math"
	"time"
)

func pi(n int) float64 {
	ch := make(chan float64)
	for k := 0; k < n; k++ {
		go func(ch chan float64, k float64) {
			ch <- 4 * math.Pow(-1, k) / (2*k + 1)
		}(ch, float64(k))
	}
	result := 0.0
	for i := 0; i < n; i++ {
		result += <-ch
	}
	return result
}

type piFunc func(int) float64

func WrapLogger(fun piFunc, logger *log.Logger) piFunc {
	return func(n int) float64 {
		fn := func(n int) (result float64) {
			defer func(t time.Time) {
				logger.Printf("took=%v,n=%v,result=%v", time.Since(t), n, result)
			}(time.Now())
			return fun(n)
		}
		return fn(n)
	}
}

package Decorator

import (
	"log"
	"os"
	"testing"
)

func TestWrapLogger(t *testing.T) {
	f := WrapLogger(pi, log.New(os.Stdout, "test ", 1))
	f(100000)
}

其他设计模式

设计模式Git源代码
00简单工厂模式
01工厂方法模式
02抽象工厂模式
03外观模式
04建造者模式
05桥接模式
06命令模式
07迭代器模式
08模板模式
09访问者模式
10备忘录模式
11责任链模式
12中介模式
13原型模式
14状态模式
15策略模式
16享元模式
17组合模式
18解释器模式
19单例模式
20适配器模式
21代理模式
22装饰器模式
23观察者模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cheems~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值