前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java一分钟之-设计模式:装饰器模式与代理模式

Java一分钟之-设计模式:装饰器模式与代理模式

作者头像
Jimaks
发布2024-05-28 08:42:44
920
发布2024-05-28 08:42:44
举报
文章被收录于专栏:大数据大数据

装饰器模式和代理模式都是在不改变原有对象的基础上,为对象添加新功能的设计模式。在这篇博客中,我们将讨论这两种模式的基本概念、常见问题及如何避免它们,并提供代码示例。

1. 装饰器模式 (Decorator Pattern)

定义

装饰器模式动态地将责任附加到对象上。若要扩展功能,装饰器提供了比继承更有弹性的替代方案。

常见问题与易错点

  • 过度使用:过度使用装饰器可能导致类的数量过多,增加系统复杂性。
  • 职责模糊:装饰器和被装饰对象之间的职责边界不清晰,可能导致设计混乱。

代码示例

代码语言:javascript
复制
interface Coffee {
    double getCost();
    String getDescription();
}

class SimpleCoffee implements Coffee {
    @Override
    public double getCost() {
        return 5.0;
    }

    @Override
    public String getDescription() {
        return "Simple Coffee";
    }
}

abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;

    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }

    @Override
    public double getCost() {
        return coffee.getCost();
    }

    @Override
    public String getDescription() {
        return coffee.getDescription();
    }
}

class MilkCoffee extends CoffeeDecorator {
    public MilkCoffee(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double getCost() {
        return super.getCost() + 0.5;
    }

    @Override
    public String getDescription() {
        return super.getDescription() + ", with milk";
    }
}

public class Main {
    public static void main(String[] args) {
        Coffee simpleCoffee = new SimpleCoffee();
        System.out.println(simpleCoffee.getCost());
        System.out.println(simpleCoffee.getDescription());

        Coffee milkCoffee = new MilkCoffee(simpleCoffee);
        System.out.println(milkCoffee.getCost());
        System.out.println(milkCoffee.getDescription());
    }
}

2. 代理模式 (Proxy Pattern)

定义

代理模式为一个对象提供一个代理以控制对该对象的访问。代理对象在客户端和目标对象之间起到中介作用。

常见问题与易错点

  • 额外开销:代理可能会引入额外的性能开销。
  • 过度代理:如果只是为了简单控制访问,可能不需要使用代理。

代码示例

代码语言:javascript
复制
interface Image {
    void display();
}

class RealImage implements Image {
    @Override
    public void display() {
        System.out.println("Displaying real image from disk.");
    }
}

class ProxyImage implements Image {
    private RealImage realImage;
    private boolean isLoaded = false;

    public ProxyImage() {
        // Lazy loading
    }

    @Override
    public void display() {
        if (!isLoaded) {
            realImage = new RealImage();
            isLoaded = true;
        }
        realImage.display();
    }
}

public class Main {
    public static void main(String[] args) {
        Image proxyImage = new ProxyImage();
        proxyImage.display();
    }
}

在实际应用中,装饰器模式用于扩展功能,而代理模式用于控制访问或提供额外功能。理解这两种模式的适用场景,避免上述问题,可以提高代码的可扩展性和可维护性。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 装饰器模式 (Decorator Pattern)
    • 定义
      • 常见问题与易错点
        • 代码示例
        • 2. 代理模式 (Proxy Pattern)
          • 定义
            • 常见问题与易错点
              • 代码示例
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档