设计模式之装饰器模式,golang,php实现

程序员卷不动了 2023-03-17 AM 198℃ 0条

装饰器模式是一种设计模式,它允许通过在不改变原始对象的结构的情况下,动态地增加对象的功能。它通过向一个对象添加一个或多个装饰器对象,以提供不同的行为,来扩展它的功能。

装饰器模式由两个核心角色组成:组件和装饰器。组件表示一个基本对象,它可以被装饰器对象处理。装饰器对象实现了特定功能,但与组件类似,也实现了相同的接口,以便它们可以被链式调用。

装饰器模式有多种变体,但它们通常包含以下步骤:

  1. 创建一个接口或抽象类,以定义基本对象和装饰器对象之间的通用接口。
  2. 创建一个类来实现该接口或抽象类,并提供基本功能。
  3. 创建一个装饰器类,它实现相同的接口或抽象类,以便可以与基本对象和其他装饰器类相互交换。在实现时,将基本对象作为参数传递,并覆盖一些方法来提供额外的行为。
  4. 通过递归调用其他装饰器类,创建一系列装饰器对象来增强基本对象的功能。
  5. 使用修饰后的对象来执行特定任务。

装饰器模式的优点是可以动态地增加功能,而无需对原始代码进行修改。它还提供了一种处理对象的不同方式的方式,使代码更加可读和更易于维护。缺点是可能会创建大量的中间对象,并且可能会导致性能问题。

golang代码实现:

package main

import "fmt"

// 组件接口
type Component interface {
    Operation() string
}

// 基础组件
type ConcreteComponent struct{}

func (c *ConcreteComponent) Operation() string {
    return "ConcreteComponent"
}

// 装饰器接口
type Decorator interface {
    Component
}

// 装饰器1
type ConcreteDecoratorA struct {
    component Component
}

func (d *ConcreteDecoratorA) Operation() string {
    return "ConcreteDecoratorA(" + d.component.Operation() + ")"
}

// 装饰器2
type ConcreteDecoratorB struct {
    component Component
}

func (d *ConcreteDecoratorB) Operation() string {
    return "ConcreteDecoratorB(" + d.component.Operation() + ")"
}

func main() {
    // 创建基础组件
    component := &ConcreteComponent{}

    // 链式调用装饰器
    decoratorA := &ConcreteDecoratorA{component}
    decoratorB := &ConcreteDecoratorB{decoratorA}

    // 执行装饰后的操作
    result := decoratorB.Operation()
    fmt.Println(result) // 输出: ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))
}

Component表示基础组件的接口,ConcreteComponent表示一个具体的基础组件。Decorator表示装饰器的接口,ConcreteDecoratorAConcreteDecoratorB分别表示装饰器A和装饰器B。

main函数中,我们首先创建了一个基础组件component,然后通过链式调用装饰器,依次将component传递给decoratorAdecoratorB。最后,我们调用decoratorB.Operation()来执行装饰后的操作,输出结果为ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))

php代码实现:

<?php

// 组件接口
interface Component {
    public function operation(): string;
}

// 基础组件
class ConcreteComponent implements Component {
    public function operation(): string {
        return "ConcreteComponent";
    }
}

// 装饰器接口
interface Decorator extends Component {}

// 装饰器1
class ConcreteDecoratorA implements Decorator {
    protected $component;

    public function __construct(Component $component) {
        $this->component = $component;
    }

    public function operation(): string {
        return "ConcreteDecoratorA(" . $this->component->operation() . ")";
    }
}

// 装饰器2
class ConcreteDecoratorB implements Decorator {
    protected $component;

    public function __construct(Component $component) {
        $this->component = $component;
    }

    public function operation(): string {
        return "ConcreteDecoratorB(" . $this->component->operation() . ")";
    }
}

// 使用示例
$component = new ConcreteComponent();
$decoratorA = new ConcreteDecoratorA($component);
$decoratorB = new ConcreteDecoratorB($decoratorA);
$result = $decoratorB->operation();
echo $result; // 输出: ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))

Component表示基础组件的接口,ConcreteComponent表示一个具体的基础组件。Decorator表示装饰器的接口,ConcreteDecoratorAConcreteDecoratorB分别表示装饰器A和装饰器B。

在示例中,我们创建了一个ConcreteComponent实例component,然后通过链式调用装饰器来包装这个组件。最终,我们调用operation()方法来执行装饰后的操作,输出结果为ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))

标签: 设计模式

非特殊说明,本博所有文章均为博主原创。

评论啦~