Java 享元模式(Flyweight Pattern)详解

说明:

享元模式(Flyweight Pattern)是一种结构型设计模式,它通过共享对象来减少内存使用和提高性能。享元模式的核心思想是将对象的状态分为内部状态(Intrinsic State)和外部状态(Extrinsic State),内部状态是可以共享的,而外部状态是每个对象独有的。

在享元模式中,通过使用工厂类来创建和管理共享的对象实例。当需要获取对象时,工厂类首先检查是否已经存在具有相同内部状态的对象,如果存在,则返回已有的对象;如果不存在,则创建新的对象并将其添加到对象池中供后续使用。

享元模式的主要目的是尽可能地减少对象的数量,通过共享对象来节省内存空间和提高性能。它适用于以下场景:

  1. 当一个系统中存在大量相似的对象,并且这些对象的内部状态可以共享时,可以使用享元模式。

  2. 当需要频繁创建和销毁对象,并且创建和销毁对象的成本很高时,可以使用享元模式来复用对象。

  3. 当对象的内部状态可以抽离出来,并且与对象的外部状态相对独立时,可以使用享元模式。

享元模式通常包含以下角色:

  1. 抽象享元(Flyweight):定义了享元对象的接口,其中包含了设置外部状态和获取内部状态的方法。

  2. 具体享元(Concrete Flyweight):实现了抽象享元接口,包含了内部状态,并提供了设置外部状态和获取内部状态的方法。

  3. 享元工厂(Flyweight Factory):负责创建和管理享元对象,包含了对象池用于存储和复用享元对象。

使用享元模式可以有效地减少对象的数量,降低系统的内存占用和提高性能。然而,享元模式也会增加系统的复杂性,因为需要维护对象的内部状态和外部状态之间的关系。因此,在使用享元模式时需要权衡对象复用带来的好处和增加的复杂性。

翻译:

Flyweight Pattern is a structural design pattern that aims to reduce memory usage and improve performance by sharing objects. The core idea of the Flyweight Pattern is to divide an object's state into intrinsic state and extrinsic state, where intrinsic state can be shared among multiple objects, while extrinsic state is unique for each object.

In the Flyweight Pattern, a factory class is used to create and manage shared object instances. When an object is requested, the factory class first checks if an object with the same intrinsic state already exists. If it does, the existing object is returned. If not, a new object is created and added to the object pool for future use.

The main purpose of the Flyweight Pattern is to minimize the number of objects by sharing them, thereby saving memory space and improving performance. It is suitable for the following scenarios:

  • When a system has a large number of similar objects and their intrinsic states can be shared, the Flyweight Pattern can be used.

  • When there is a high cost of creating and destroying objects frequently, the Flyweight Pattern can be used to reuse objects.

  • When the intrinsic state of an object can be separated from its extrinsic state and is relatively independent, the Flyweight Pattern can be used.

The Flyweight Pattern typically consists of the following roles:

  • Flyweight: Defines the interface for flyweight objects, including methods to set the extrinsic state and get the intrinsic state.

  • Concrete Flyweight: Implements the Flyweight interface, including the intrinsic state, and provides methods to set the extrinsic state and get the intrinsic state.

  • Flyweight Factory: Responsible for creating and managing flyweight objects, including an object pool for storing and reusing flyweight objects.

Using the Flyweight Pattern can effectively reduce the number of objects, decrease memory usage, and improve performance. However, it also adds complexity to the system as the relationship between intrinsic and extrinsic states needs to be maintained. Therefore, when using the Flyweight Pattern, the benefits of object reuse need to be balanced with the increased complexity.

下面我将使用一个具体的例子来说明享元模式的应用。

假设我们有一个游戏场景,需要绘制大量的树木对象。每个树木对象都有相同的外观(如形状、颜色等),但位置是不同的。在这种情况下,我们可以使用享元模式来节省内存并提高性能。

首先,我们定义一个抽象享元类 Tree,其中包含设置外部状态(位置)和获取内部状态(外观)的方法:

public abstract class Tree {
    public abstract void draw(int x, int y);
}

然后,我们创建具体享元类 ConiferousTreeDeciduousTree,它们实现了抽象享元类,并定义了内部状态(外观):

public class ConiferousTree extends Tree {
    private String appearance;

    public ConiferousTree(String appearance) {
        this.appearance = appearance;
    }

    @Override
    public void draw(int x, int y) {
        System.out.println("Drawing a coniferous tree at (" + x + ", " + y + ") with appearance: " + appearance);
    }
}

public class DeciduousTree extends Tree {
    private String appearance;

    public DeciduousTree(String appearance) {
        this.appearance = appearance;
    }

    @Override
    public void draw(int x, int y) {
        System.out.println("Drawing a deciduous tree at (" + x + ", " + y + ") with appearance: " + appearance);
    }
}

接下来,我们创建享元工厂类 TreeFactory,用于创建和管理树木对象:

import java.util.HashMap;
import java.util.Map;

public class TreeFactory {
    private static Map<String, Tree> treePool = new HashMap<>();

    public static Tree getTree(String type, String appearance) {
        Tree tree = treePool.get(type);

        if (tree == null) {
            if (type.equals("coniferous")) {
                tree = new ConiferousTree(appearance);
            } else if (type.equals("deciduous")) {
                tree = new DeciduousTree(appearance);
            }

            treePool.put(type, tree);
        }

        return tree;
    }
}


最后,我们可以在客户端代码中使用享元模式来绘制树木对象:

public class Client {
    public static void main(String[] args) {
        Tree tree1 = TreeFactory.getTree("coniferous", "green");
        tree1.draw(10, 20);

        Tree tree2 = TreeFactory.getTree("deciduous", "yellow");
        tree2.draw(30, 40);

        Tree tree3 = TreeFactory.getTree("coniferous", "green");
        tree3.draw(50, 60);
    }
}

在上述代码中,我们通过 TreeFactory 工厂类获取树木对象,如果对象池中已经存在相同类型的树木对象,则直接返回已有的对象,否则创建新的对象并添加到对象池中。

通过使用享元模式,我们可以避免创建大量相同外观的树木对象,而是共享已有的对象实例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,根据提供的引用内容中没有包含java享元模式的UML图。但是可以为您简要介绍java享元模式的UML图。 享元模式的UML图如下所示: - Flyweight:享元接口,定义了享元对象的公共方法。 - ConcreteFlyweight:具体享元对象,实现了享元接口,并存储了内部状态。 - UnsharedConcreteFlyweight:非共享具体享元对象,不共享的对象,一般不会出现在享元工厂中。 - FlyweightFactory:享元工厂类,负责创建和管理享元对象。 - Client:客户端类,通过享元工厂获取享元对象,并操作具体享元对象。 请注意,由于无法直接提供UML图,以上是一份简化的描述。在实际应用中,可能还会有其他的类和关系。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Java设计模式之享元模式(UML类图分析+代码详解)](https://blog.csdn.net/m0_51750419/article/details/127332662)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [设计模式-享元模式](https://blog.csdn.net/qq_44065088/article/details/108725714)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值