享元模式在主流的标准里是放到结构大类下的,但是我感觉这个模式的最终作用也是为了获取一个类,所以我将其划分到创建大类下。
What is Flyweight Pattern
Flyweight 是指轻量级的。
享元模式旨在支持大量细粒度的对象共享,以减少内存消耗。该模式通过共享相似对象的部分状态,来减少对象的数量。通过共享,可以节省内存并提高系统的性能。
享元模式通常涉及两个关键概念:
- 内部状态(Intrinsic State)
内部状态是可以共享的部分,存储在享元对象内部; - 外部状态(Extrinsic State)
外部状态是不可共享的部分,需要在使用时提供。
通过将内部状态与外部状态分离,可以实现对象的共享。享元模式适用于需要创建大量相似对象且内存消耗较大的场景。
Comparison between prototype and Flyweight
Similarities:
- 从场景上来看都是为了节省资源区创建大量相似的对象;
- 一般都是配合工厂模式;
Differences:
- Flyweight 如果需要创建不同的对象,就得提供不同的外部状态;
- Prototype 无需依赖于显式的构造函数,它通过复制现有对象的原型来创建新对象。
Key elements
细颗粒类
接口细颗粒类
实现- 创建
细颗粒类
工厂 - Client
Simple Example
当你使用享元模式时,你需要定义一个享元工厂(Flyweight Factory),它负责创建和管理享元对象。下面是一个简单的Java代码示例,演示了如何实现享元模式:
首先,我们定义享元接口(Flyweight):
public interface Flyweight {
void operation();
}
然后,实现具体的享元类(ConcreteFlyweight):
public class ConcreteFlyweight implements Flyweight {
private String intrinsicState;
public ConcreteFlyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
@Override
public void operation() {
System.out.println("ConcreteFlyweight: " + intrinsicState);
}
}
接下来,创建享元工厂类(FlyweightFactory)来管理享元对象:
import java.util.HashMap;
import java.util.Map;
public class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
if (flyweights.containsKey(key)) {
return flyweights.get(key);
} else {
Flyweight flyweight = new ConcreteFlyweight(key);
flyweights.put(key, flyweight);
return flyweight;
}
}
}
最后,我们可以使用享元工厂来获取享元对象并调用其方法:
public class Main {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight("A");
flyweight1.operation();
Flyweight flyweight2 = factory.getFlyweight("B");
flyweight2.operation();
Flyweight flyweight3 = factory.getFlyweight("A");
flyweight3.operation();
// 输出结果:
// ConcreteFlyweight: A
// ConcreteFlyweight: B
// ConcreteFlyweight: A
}
}
在上述示例中,我们使用享元工厂来获取享元对象。首次获取某个享元对象时,工厂会创建一个新的对象并将其存储在内部的HashMap中。之后,如果再次请求相同的享元对象,则直接返回已经创建的对象。这样就实现了对象的共享,减少了内存消耗。
希望这个简单的示例能帮助你理解享元模式的基本概念和用法。
- Creational Flyweight Pattern Classcreational flyweight pattern class creational singleton pattern class creational factory pattern class creational prototype pattern class flyweight pattern 12 flyweight pattern cpp typescript flyweight pattern behavioral template pattern class composite structure pattern class structure builder pattern class