享元模式:运用共享技术有效地支持大量细粒度的对象。UML如下图所示。
1、FlyWeight类所有具体享元类的超类或接口。
<span style="font-size:18px;">public abstract class FlyWeight
{
abstract void Operation(int extrinsictstate);
}</span>
2、具体FlyWeight类。<span style="font-size:18px;">public class ConcreteFlyWeight extends FlyWeight
{
@Override
void Operation(int extrinsictstate)
{
// TODO Auto-generated method stub
System.out.println("共享的具体的FlyWeight"+extrinsictstate);
}
}
class UnsharedConcreteFlyWeight extends FlyWeight
{
@Override
void Operation(int extrinsictstate)
{
// TODO Auto-generated method stub
System.out.println("非共享的具体"+extrinsictstate);
}
}
</span>
3、FlyWeightFactory类
<span style="font-size:18px;">public class FlyWeightFactory
{
private Map<String,FlyWeight> map=new HashMap<String,FlyWeight>();
public FlyWeightFactory()
{
map.put("x",new ConcreteFlyWeight());
map.put("y",new ConcreteFlyWeight());
map.put("z",new ConcreteFlyWeight());
}
public FlyWeight getFlyWeight(String key)
{
return map.get(key);
}
}</span>
4、Client类
<span style="font-size:18px;">public class Client
{
public static void main(String[] args)
{
int extrinsicstate=22;
FlyWeightFactory f=new FlyWeightFactory();
FlyWeight fx=f.getFlyWeight("x");
fx.Operation(--extrinsicstate);
FlyWeight fy=f.getFlyWeight("y");
fy.Operation(--extrinsicstate);
FlyWeight fz=f.getFlyWeight("z");
fz.Operation(--extrinsicstate);
FlyWeight uf=new UnsharedConcreteFlyWeight();
uf.Operation(--extrinsicstate);
}
}</span>
实例分析,网站的代码共享,如果在构建多个网站的时候,发现网站的功能只有小范围的差异,那么就可以通过享元的方法,将多个网站布置在一个服务器空间上。
1、Website抽象类
<span style="font-size:18px;">public abstract class Website
{
public abstract void use(User user);
}
</span>
2、Website具体类
<span style="font-size:18px;">public class ConcreteWebsite extends Website
{
private String name="";
public ConcreteWebsite(String name)
{
this.name=name;
}
@Override
public void use(User user)
{
// TODO Auto-generated method stub
System.out.println("网站分类:"+name+":"+"用户名"+user.getName());
}
}
</span>
3、WebsiteFactory类
<span style="font-size:18px;">public class WebsiteFactory
{
private Map<String, Website> map = new HashMap<String, Website>();
public Website getWebsiteCategory(String key)
{
if (!map.containsKey(key))
{
map.put(key, new ConcreteWebsite(key));
}
return map.get(key);
}
public int getWebsiteCount()
{
return map.size();
}
}</span>
4、User类
<span style="font-size:18px;">public class User
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
</span>
5、客户端类
<span style="font-size:18px;">public class Client
{
public static void main(String[] args)
{
User user = new User();
user.setName("A");
WebsiteFactory f = new WebsiteFactory();
Website fx = f.getWebsiteCategory("产品展示");
fx.use(user);
user.setName("B");
Website fy = f.getWebsiteCategory("产品展示");
fy.use(user);
user.setName("C");
Website fz = f.getWebsiteCategory("产品展示");
fz.use(user);
user.setName("E");
Website f1 = f.getWebsiteCategory("博客");
f1.use(user);
user.setName("F");
Website f2 = f.getWebsiteCategory("博客");
f2.use(user);
user.setName("G");
Website f3 = f.getWebsiteCategory("博客");
f3.use(user);
System.out.println("网站分类总数为:" + f.getWebsiteCount());
}
}
</span>
享元模式的应用场景:1、如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑适应。
2、对象的大多数状态可以是外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。
资料来源:程杰老师<大话设计模式>