您现在的位置是:亿华云 > 知识

如何用享元模式优化系统内存?

亿华云2025-10-04 01:11:15【知识】9人已围观

简介哈喽,大家好,我是指北君。相信大家日常开发过程中,一个优秀的程序猿写出的代码一定要节省空间的,比如节省内存,节省磁盘等等。那么如何通过设计模式来节省内存呢?1.什么是享元模式?Use sharing

哈喽,何用大家好,享元系统我是模式指北君。

相信大家日常开发过程中,优化一个优秀的内存程序猿写出的代码一定要节省空间的,比如节省内存,何用节省磁盘等等。享元系统那么如何通过设计模式来节省内存呢?模式

1.什么是享元模式?

Use sharing to support large numbers of fine-grained objects efficiently.

享元模式(Flyweight Pattern):使用共享对象可有效地支持大量的细粒度的对象。

说人话:复用对象,优化节省内存。内存

2.享元模式定义

①Flyweight——抽象享元角色

是何用一个产品的抽象类, 同时定义出对象的享元系统外部状态和内部状态的接口或实现。

一个对象信息可以分为内部状态和外部状态。模式

内部状态:对象可共享出来的优化信息, 存储在享元对象内部并且不会随环境改变而改变,内存可以作为一个对象的动态附加信息, 不必直接储存在具体某个对象中, 属于可以共享的部分。

外部状态:对象得以依赖的一个标记,亿华云计算 是随环境改变而改变的、 不可以共享的状态。

②ConcreteFlyweight——具体享元角色

具体的一个产品类, 实现抽象角色定义的业务。该角色中需要注意的是内部状态处理应该与环境无关, 不应该出现一个操作改变了内部状态, 同时修改了外部状态, 这是绝对不允许的。

③unsharedConcreteFlyweight——不可共享的享元角色

不存在外部状态或者安全要求(如线程安全) 不能够使用共享技术的对象, 该对象一般不会出现在享元工厂中。

④FlyweightFactory——享元工厂

职责非常简单, 就是构造一个池容器, 同时提供从池中获得对象的方法。

3.享元模式通用代码

/

**

* 抽象享元角色

*/

public abstract class Flyweight {

// 内部状态

private String instrinsic;

// 外部状态 通过 final 修改,防止修改

protected final String extrinsic;

protected Flyweight(String extrinsic) {

this.extrinsic = extrinsic;

}

// 定义业务操作

public abstract void operate();

public String getInstrinsic() {

return instrinsic;

}

public void setInstrinsic(String instrinsic) {

this.instrinsic = instrinsic;

}

}/

**

* 具体享元角色1

*/

public class ConcreteFlyweight1 extends Flyweight{

protected ConcreteFlyweight1(String extrinsic) {

super(extrinsic);

}

@Override

public void operate() {

System.out.println("具体享元角色1");

}

}/

**

* 具体享元角色2

*/

public class ConcreteFlyweight2 extends Flyweight{

protected ConcreteFlyweight2(String extrinsic) {

super(extrinsic);

}

@Override

public void operate() {

System.out.println("具体享元角色2");

}

}public class FlyweightFactory {

// 定义一个池容器

private static HashMappool = new HashMap<>();

// 享元工厂

public static Flyweight getFlyweight(String extrinsic){

// 需要返回的对象

Flyweight flyweight = null;

// 池中没有该对象

if(pool.containsKey(extrinsic)){

flyweight = pool.get(extrinsic);

}else{

// 根据外部状态创建享元对象

flyweight = new ConcreteFlyweight1(extrinsic);

// 放置到池中

pool.put(extrinsic,flyweight);

}

return flyweight;

}

}

4.通过享元设计文本编辑器

假设文本编辑器只包含文字编辑功能,而且只记录文字和格式两部分信息,其中格式包括文字的字体型号、大小、亿华云颜色等信息。

4.1 普通实现

通常设计是把每个文字看成一个单独对象。

package com.itcoke.designpattern.flyweight.edittext;

/

**

* 单个文字对象

*/

public class Character {

// 字符

private char c;

// 字体型号

private String font;

// 字体大小

private int size;

// 字体颜色

private int colorRGB;

public Character(char c, String font, int size, int colorRGB){

this.c = c;

this.font = font;

this.size = size;

this.colorRGB = colorRGB;

}

@Override

public String toString() {

return String.valueOf(c);

}

}/

**

* 编辑器实现

*/

public class Editor {

private ArrayListchars = new ArrayList<>();

public void appendCharacter(char c, String font, int size, int colorRGB){

Character character = new Character(c,font,size,colorRGB);

chars.add(character);

}

public void display(){

System.out.println(chars);

}

}

客户端:

public class EditorClient {

public static void main(String[] args) {

Editor editor = new Editor();

editor.appendCharacter(A,"宋体",11,0XFFB6C1);

editor.appendCharacter(B,"宋体",11,0XFFB6C1);

editor.appendCharacter(C,"宋体",11,0XFFB6C1);

editor.display();

}

}4.2 享元模式改写

上面的问题很容易发现,每一个字符就会创建一个 Character 对象,如果是几百万个字符,那内存中就会存在几百万的对象,那怎么去节省这些内存呢?

其实,分析一下,对于字体的格式,通常不会有很多,于是我们可以把字体格式设置为享元,也就是上面说的可以共享的内部状态。

内部状态(共享):字体类型、大小、颜色

外部状态(不共享):字符

于是代码改写如下:

public class CharacterStyle {

// 字体型号

private String font;

// 字体大小

private int size;

// 字体颜色

private int colorRGB;

public CharacterStyle(String font, int size, int colorRGB) {

this.font = font;

this.size = size;

this.colorRGB = colorRGB;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

CharacterStyle that = (CharacterStyle) o;

return size == that.size &&

colorRGB == that.colorRGB &&

Objects.equals(font, that.font);

}

@Override

public int hashCode() {

return Objects.hash(font, size, colorRGB);

}

}public class CharacterStyleFactory {

private static final MapmapStyles = new HashMap<>();

public static CharacterStyle getStyle(String font, int size, int colorRGB){

CharacterStyle newStyle = new CharacterStyle(font,size,colorRGB);

if(mapStyles.containsKey(newStyle)){

return mapStyles.get(newStyle);

}

mapStyles.put(newStyle,newStyle);

return newStyle;

}

}public class Character {

private char c;

private CharacterStyle style;

public Character(char c, CharacterStyle style) {

this.c = c;

this.style = style;

}

@Override

public String toString() {

return String.valueOf(c);

}

}public class Editor {

private Listchars = new ArrayList<>();

public void appendCharacter(char c, String font, int size, int colorRGB){

Character character = new Character(c,CharacterStyleFactory.getStyle(font,size,colorRGB));

chars.add(character);

}

public void display(){

System.out.println(chars);

}

}

5.享元模式在 java.lang.Integer 中应用

看下面这段代码,打印结果是啥?

public class IntegerTest {

public static void main(String[] args) {

Integer i1 = 56;

Integer i2 = 56;

Integer i3 = 129;

Integer i4 = 129;

System.out.println(i1 == i2);

System.out.println(i3 == i4);

}

}

为什么是这种结果呢?

首先说一下 Integer i = 59;底层执行了:Integer i = Integer.valueOf(59); 这是自动装箱。

int j = i; 底层执行了:int j = i.intValue(); 这是自动拆箱。

然后我们Integer.valueOf() 方法:

再看 IntegerCache 源码:

private static class IntegerCache {

static final int low = -128;

static final int high;

static final Integer cache[];

static {

// high value may be configured by property

int h = 127;

String integerCacheHighPropValue =

sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

if (integerCacheHighPropValue != null) {

try {

int i = parseInt(integerCacheHighPropValue);

i = Math.max(i, 127);

// Maximum array size is Integer.MAX_VALUE

h = Math.min(i, Integer.MAX_VALUE - (-low) -1);

} catch( NumberFormatException nfe) {

// If the property cannot be parsed into an int, ignore it.

}

}

high = h;

cache = new Integer[(high - low) + 1];

int j = low;

for(int k = 0; k < cache.length; k++)

cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7)

assert IntegerCache.high >= 127;

}

private IntegerCache() { }

}

其实这就是我们前面说的香港云服务器享元对象的工厂类,缓存 -128 到 127 之间的整型值,这是最常用的一部分整型值,当然JDK 也提供了方法来让我们可以自定义缓存的最大值。

6.享元模式优点

减少应用程序创建的对象, 降低程序内存的占用, 增强程序的性能。

但它同时也提高了系统复杂性, 需要分离出外部状态和内部状态, 而且外部状态具有固化特性, 不应该随内部状态改变而改变, 否则导致系统的逻辑混乱。

7.享元模式应用场景

①系统中存在大量的相似对象。

②细粒度的对象都具备较接近的外部状态, 而且内部状态与环境无关, 也就是说对象没有特定身份。

③需要缓冲池的场景。

很赞哦!(6)