Flyweight概述
玩具代码案例 - 字体享元管理器
享元对象
同一种类型的字体没有必要频繁创建,完全可以重复使用
package online.javabook.gof.structural.patterns6.flyweight.font;
import java.util.Objects;
public class Font {
final String name;
final int style;
final int size;
public Font(String name, int style, int size) {
this.name = name;
this.style = style;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Font font = (Font) o;
return style == font.style &&
size == font.size &&
Objects.equals(name, font.name);
}
@Override
public int hashCode() {
return Objects.hash(name, style, size);
}
}
package online.javabook.gof.structural.patterns6.flyweight.font;
public interface Style {
final static int PLAIN = 1;
final static int BOLD = 2;
final static int ITALIC = 3;
}
享元对象管理器
package online.javabook.design.gof.structural6.flyweight.font;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class FontFlyweight {
private static ConcurrentMap<String, Font> flyweights = new ConcurrentHashMap<>();
public static Font create(String name, int style, int size) {
Font font = null;
if(!flyweights.containsKey(name)) {
font = flyweights.putIfAbsent(name, new Font(name, style, size));
}
return font;
}
}
不基于享元模式的实现
Main
package online.javabook.gof.structural.patterns6.flyweight.font.app.bad;
import online.javabook.gof.structural.patterns6.flyweight.font.Font;
import online.javabook.gof.structural.patterns6.flyweight.font.Style;
public class Main {
public static void main(String[] args) {
Font font1 = new Font("宋体", Style.BOLD, 12);
Font font2 = new Font("宋体", Style.BOLD, 12);
System.out.println("font1 == font2 ? " + (font1 == font2));
}
}
Console
font1 == font2 ? false
基于享元模式的实现
Main
package online.javabook.gof.structural.patterns6.flyweight.font.app.good;
import online.javabook.gof.structural.patterns6.flyweight.font.Font;
import online.javabook.gof.structural.patterns6.flyweight.font.FontFlyweight;
import online.javabook.gof.structural.patterns6.flyweight.font.Style;
public class Main {
public static void main(String[] args) {
Font font1 = FontFlyweight.create("宋体", Style.BOLD, 12);
Font font2 = FontFlyweight.create("宋体", Style.BOLD, 12);
System.out.println("font1 == font2 ? " + (font1 == font2));
}
}
Console
font1 == font2 ? true
现实世界中的享元模式
字符集对象
没有必要每次都生成一个字符集对象
package online.javabook.io.bio.charset;
import java.nio.charset.Charset;
public class CharsetMain {
public static void main(String[] args) {
//返回指定的字符集CharSet
Charset utf81 = Charset.forName("utf8");
//返回指定的字符集CharSet
Charset utf82 = Charset.forName("utf8");
System.out.println(utf81 == utf82);
}
}
Console
true
字符串对象
Java字符串对象的常量池我认为也是一种享元模式。字符串在开发者被频繁创建,相同的字符串如果能够重用,自然是能够降低内存的使用。
只是大家关注的都是string创建了几个对象,引用比较,放在永生区还是堆中等等这些问题上。而不是真正学习设计的本身,本末倒置。
/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
* <p>
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* <cite>The Java™ Language Specification</cite>.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();