简单享元模式:
//抽象享元。这里将26个英文字母作为内部状态。
public interface Filyweigth {
//将打印功能作为共享的内部状态
public void print();
}
-----------------------------------
//简单享元类。一个对象只能存放一个字母
public class SimpleLetter implements Filyweigth {
private Character letter;
public SimpleLetter(Character letter) {
super();
this.letter = letter;
}
@Override
public void print() {
System.out.println("print() 当前字母 : " + letter);
}
}
----------------------------
//享元工厂
public class FlyweigthFactory {
private Map<Character,Filyweigth> pools;
public FlyweigthFactory() {
pools = new HashMap<>();
}
//获取简单享元对象的工厂方法
public Filyweigth getSimpleLetter(Character ct) {
if(!pools.containsKey(ct)) {
pools.put(ct, new SimpleLetter(ct));
}
return (Filyweigth)pools.get(ct);
}
public void listSize() {
System.out.println("享元池中享元个数 :"+pools.size());
}
}
----------------------------------
//客户端
public class MainTest {
public static void main(String[] args) {
FlyweigthFactory ff = new FlyweigthFactory();
Filyweigth a1 = ff.getSimpleLetter('w'); a1.print();
Filyweigth a2 = ff.getSimpleLetter('w'); a2.print();
//享元池的享元个数。
ff.listSize();
System.out.println(a1 == a2);
}
}
结果:
print() 当前字母 : w
print() 当前字母 : w
享元池中享元个数 :1
true
复杂享元模式:
//抽象享元。这里将26个英文字母作为内部状态。
public interface Filyweigth {
//将打印功能作为共享的内部状态
public void print();
}
-----------------------------------
//简单享元类。一个对象只能存放一个字母
public class SimpleLetter implements Filyweigth {
private Character letter;
public SimpleLetter(Character letter) {
super();
this.letter = letter;
}
@Override
public void print() {
System.out.println("print() 当前字母 : " + letter);
}
}
//复杂享元类
public class ComplexLetter implements Filyweigth {
//用于存放简单享元类组合后的结果
private List<Filyweigth> complex;
public ComplexLetter() {
this.complex = new LinkedList<Filyweigth>();
}
//向复杂享元类中添加简单享元类
public void add(Filyweigth fw) {
complex.add(fw);
}
//将复杂享元类中元素打印出来
public void print() {
//打印出复杂享元中不相同的元素
HashSet<Filyweigth> hs = new HashSet<Filyweigth>();
for(Filyweigth fw : complex) {
hs.add(fw);
}
Iterator<Filyweigth> iterator = hs.iterator();
while(iterator.hasNext()) {
iterator.next().print();
}
System.out.println(complex.size());
}
}
----------------------------------------------------
//享元工厂
public class FlyweigthFactory {
//存放简单享元类
private Map<Character,Filyweigth> pools;
public FlyweigthFactory() {
pools = new HashMap<>();
}
//获取简单享元对象的工厂方法
public Filyweigth getSimpleLetter(Character ct) {
if(!pools.containsKey(ct)) {
pools.put(ct, new SimpleLetter(ct));
}
return (Filyweigth)pools.get(ct);
}
//获取复杂享元,将简单享元进行组合,获取需要的结果
public Filyweigth getCpmplexLetter(String string) {
ComplexLetter fw = new ComplexLetter();
for(int i = 0 ,len = string.length(); i < len ;i++) {
char c = string.charAt(i);
Filyweigth simpleLetter = getSimpleLetter(c);
fw.add(simpleLetter);
}
return fw;
}
public void listSize() {
System.out.println("享元池中享元个数 :"+pools.size());
}
}
----------------------------------
//客户端
public class MainTest {
public static void main(String[] args) {
FlyweigthFactory ff = new FlyweigthFactory();
Filyweigth letter = ff.getCpmplexLetter("gggggggggggg");
letter.print();
ff.listSize();
}
}
结果:
print() 当前字母 : g
12
享元池中享元个数 :1
结果分析:由结果可以看出要创建由g组成的单词,只需要一个对象享元对象就可以组合。
总结:
复杂享元模式:简单享元模式+组合模式
复杂享元模式是在简单享元模式的基础上发展而来的。复杂享元中的结果是用简单享元对象组合而来的,没有创建新的对象就完成功能。