一、Flyweight模式的意图:
缓存(共享)可以共同的对象、属性,避免创建大量的重复对象、内容。
二、Flyweight模式的特点:
为重复或可共享的对象、属性设置一个缓冲,称为内部属性。这些内部属性一般情况下都是不可修改的,也就是在第一个对象、属性被创建后,就不会去修改了(否则就没意义了)。
除了共享对象、属性之外,还有一些是基于应用环境,或是实时数据,这些无法共享的东西我们称为外部状态。
在Flyweight模式应用中,通常修改的是外部状态属性,而内部状态属性一般都是用于参考或计算时引用。
三、Flyweight模式的构成:
![](http://www.blogjava.net/images/blogjava_net/pengpenglin/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/Flyweight.JPG)
Flyweight模式一般由几个部分组成:
·Flyweight接口(抽象类) :定义了一个可共享的元类
·Flyweight实现类:实现了元类中的操作,而且可能会提供一个用于保存内部状态(共享属性)的空间
·Flyweight Factory:创建Flyweight的工厂类,创建后将其保存到Flyweight Pool中
·Flyweight Pool:缓冲Flyweight对象的池,通常包含在工厂类中
四、Flyweight模式的简单实现
1.Flyweight:
package com.pattern.flyweight2;
public
abstract
class Flyweight
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
{
public
abstract
void operation();
![](https://i-blog.csdnimg.cn/blog_migrate/da43bcbc871cd4f18defa9179da7af64.gif)
}
2.ConcreateFlyweight:
package com.pattern.flyweight2;
public
class ConcreateFlyweight
extends Flyweight
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
{
private Object key;
public ConcreateFlyweight(Object key)
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
this.key = key;
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
@Override
public
void operation()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
System.out.println("key:" + key);
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/da43bcbc871cd4f18defa9179da7af64.gif)
}
3.FlyweightFactory:
package com.pattern.flyweight2;
import java.util.Hashtable;
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
@SuppressWarnings("unchecked")
public
class FlyweightFactory
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
{
//
Flyweight pool
private Hashtable flyweights =
new Hashtable();
public Flyweight getFlyweight(Object key)
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
Flyweight flyweight =
null;
if(flyweights.containsKey(key))
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
flyweight = (Flyweight) flyweights.get(key);
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
if (!flyweights.containsKey(key))
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
flyweight =
new ConcreateFlyweight(key);
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
flyweights.put(key, flyweight);
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
return flyweight;
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
public
int getFlyweightsSize()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
return flyweights.size();
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/da43bcbc871cd4f18defa9179da7af64.gif)
}
4.测试类:
package com.pattern.flyweight2;
public
class Client
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
{
private Flyweight f1 =
null;
private Flyweight f2 =
null;
private Flyweight f3 =
null;
private Flyweight f4 =
null;
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
FlyweightFactory factory =
new FlyweightFactory();
public Client()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f1 = factory.getFlyweight("baidu");
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f2 = factory.getFlyweight("google");
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f3 = factory.getFlyweight("baidu");
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f4 = factory.getFlyweight("google");
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
public
void show()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f1.operation();
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f2.operation();
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f3.operation();
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
f4.operation();
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
System.out.println(factory.getFlyweightsSize());
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
public
static
void main(String[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
Client client =
new Client();
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
client.show();
![](https://i-blog.csdnimg.cn/blog_migrate/84b88d7ce3b476e13678154aa69b5b89.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/da43bcbc871cd4f18defa9179da7af64.gif)
}
也就是在缓冲这一点上,体现了Flyweight模式的精髓。