likes
comments
collection
share

通俗易懂设计模式(装饰器模式)

作者站长头像
站长
· 阅读数 6

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在不修改原始类的情况下,动态地为对象添加新的功能。装饰器模式通过创建一个包装类来实现,这个包装类包含了一个指向原始对象的引用,并实现了与原始对象相同的接口。

装饰器模式的主要组成部分包括:

  1. 组件(Component):定义了一个接口,用于访问和管理装饰器和被装饰对象。
  2. 具体组件(ConcreteComponent):表示被装饰的对象,它实现了组件接口。
  3. 装饰器(Decorator):实现了组件接口,并持有一个指向组件的引用。
  4. 具体装饰器(ConcreteDecorator):继承自装饰器,并实现了新的功能。

装饰器模式的优点:

  1. 提高了代码的可扩展性:装饰器模式可以通过添加新的装饰器来扩展系统的功能,而不需要修改已有的代码。
  2. 提高了代码的可维护性:装饰器模式将功能扩展和原始对象分离,使得代码更加清晰和易于维护。

Java 实现装饰器模式的示例代码:

// 组件接口
public interface Component {
    void operation();
}

// 具体组件
public class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("Concrete component operation");
    }
}

// 装饰器
public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

// 具体装饰器
public class ConcreteDecorator extends Decorator {
    public ConcreteDecorator(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        System.out.println("Concrete decorator operation");
        super.operation();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratedComponent = new ConcreteDecorator(component);

        decoratedComponent.operation();
    }
}

在这个示例中,我们定义了一个组件接口 Component,它包含了一个 operation() 方法。接着,我们定义了一个具体组件类 ConcreteComponent,它实现了 Component 接口。然后,我们定义了一个抽象装饰器类 Decorator,它实现了 Component 接口,并持有一个指向组件的引用。在抽象装饰器类中,我们提供了一个默认的 operation() 方法,它调用了组件的 operation() 方法。

接着,我们定义了一个具体装饰器类 ConcreteDecorator,它继承自抽象装饰器类,并实现了新的功能。在具体装饰器类中,我们重写了 operation() 方法,并在调用父类的 operation() 方法之前添加了新的功能。

在客户端代码中,我们创建了一个具体组件对象和一个具体装饰器对象,并将具体组件对象传递给具体装饰器对象。然后,我们调用具体装饰器对象的 operation() 方法,从而实现了具体组件对象的 operation() 方法的调用,并添加了新的功能。

场景举例

场景一:动态添加日志功能

在一个应用程序中,我们可能需要为某些对象添加日志功能,以便在运行时记录对象的行为。为了实现这个功能,我们可以使用装饰器模式。

使用装饰器模式来实现动态添加日志功能,可以让代码更加简洁、易于维护。具体实现如下:

// 被装饰的对象接口
interface Component {
    void operation();
}

// 具体被装饰的对象实现
class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}

// 装饰器基类
abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

// 具体装饰器实现,添加日志功能
class LoggingDecorator extends Decorator {
    public LoggingDecorator(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        System.out.println("Logging start");
        component.operation();
        System.out.println("Logging end");
    }
}

// 测试类
public class DecoratorPatternDemo {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        component = new LoggingDecorator(component);
        component.operation();
    }
}

场景二:动态添加权限控制功能

在一个应用程序中,我们可能需要为某些对象添加权限控制功能,以便在运行时限制对象的访问权限。为了实现这个功能,我们可以使用装饰器模式。

使用装饰器模式来实现动态添加权限控制功能,可以让代码更加简洁、易于维护。具体实现如下:

// 被装饰的对象接口
interface Component {
    void operation();
}

// 具体被装饰的对象实现
class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}

// 装饰器基类
abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

// 具体装饰器实现,添加权限控制功能
class AuthorizationDecorator extends Decorator {
    public AuthorizationDecorator(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        if (isAuthorized()) {
            component.operation();
        } else {
            System.out.println("Unauthorized access");
        }
    }

    private boolean isAuthorized() {
        // 这里可以添加实际的权限检查逻辑
        return true;
    }
}

// 测试类
public class DecoratorPatternDemo {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        component = new AuthorizationDecorator(component);
        component.operation();
    }
}

在这两个场景中,我们都使用了装饰器模式来动态地为对象添加新的功能。在第一个场景中,我们使用装饰器模式来实现动态添加日志功能;在第二个场景中,我们使用装饰器模式来实现动态添加权限控制功能。通过使用装饰器模式,我们可以让代码更加简洁、易于维护,同时提高了代码的可扩展性和可重用性。

转载自:https://juejin.cn/post/7341952374368026663
评论
请登录