享元模式
/**
* 享元模式
* 运用共享技术有效的支持大量细粒度的对象.
*
* 享元模式的内部&外部状态
* 享元模式内部是不会随环境变化而变化的部分,可以称为是享元对象的内部状态
* 而随着环境改变而改变,不可以共享的部分就是享元对象的外部状态了.
*
* 作用:
* 享元模式可以避免大量的非常相似的类的开销.
*
* 应用场景:
* 在程序设计中有时候需要生成大量细粒度的类的实例来表示数据.如果能发现这些实例除了几个
* 参数外基本都是相同的,有时就能够大幅度的减少需要实例化的类的数量.
* 如果能够把那些参数移到类实例的外部,在方法调用时将他们传递进来,就可以通过共享大幅度的减少单个实例的数量.
*/
结构图
范例
abstract public class Flyweight {
abstract public void operate(int extrinsicstate);
}
public class FlyweightFactory {
public static String TYPE_1="com.junx.designpattem.FlyweightPattern.Demo.FlyweightFactory.1";
public static String TYPE_2="com.junx.designpattem.FlyweightPattern.Demo.FlyweightFactory.2";
public static String TYPE_3="com.junx.designpattem.FlyweightPattern.Demo.FlyweightFactory.3";
static private HashMap<String,Flyweight> map=new HashMap<>();
static {
map.put(TYPE_1,new ConcreteFlyweight());
map.put(TYPE_2,new ConcreteFlyweight());
map.put(TYPE_3,new ConcreteFlyweight());
}
static public Flyweight getFlyweight(String key){
return map.get(key);
}
}
public class ConcreteFlyweight extends Flyweight {
@Override
public void operate(int extrinsicstate) {
Log.d("meee","具体:"+extrinsicstate+" "+this);
}
}
public class UnsharedFlyweight extends Flyweight{
@Override
public void operate(int extrinsicstate) {
Log.d("meee","不分享的"+extrinsicstate);
}
}
//客户端代码
//共享的数据库
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_1).operate(998)
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_2).operate(998)
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_3).operate(998)
//私有的数据库
UnsharedFlyweight unsharedFlyweight = new UnsharedFlyweight()
unsharedFlyweight.operate(998)
网站范例
public interface Website {
void run(User user);
}
public class WebsiteImp implements Website {
protected String name;
public WebsiteImp(String name) {
this.name = name;
}
@Override
public void run(User user) {
Log.d("meee","正在运行"+user.name+"的"+name);
}
}
public class User {
String name;
public User(String name) {
this.name = name;
}
}
public class WebsiteFactory {
public static HashMap<String,Website> map;
static {
map=new HashMap<>();
}
public static Website get(String name){
Website website = map.get(name);
if (website==null){
website=new WebsiteImp(name);
map.put(name,website);
}
return website;
}
}
//客户端代码
//共享的数据库
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_1).operate(998)
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_2).operate(998)
FlyweightFactory.getFlyweight(FlyweightFactory.TYPE_3).operate(998)
//私有的数据库
UnsharedFlyweight unsharedFlyweight = new UnsharedFlyweight()
unsharedFlyweight.operate(998)
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小黄的博客
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小张的博客
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小丽的博客
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小蚕的博客
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小黄的淘宝
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 正在运行小蚕的淘宝
04-12 15:33:27.332 26986-26986/com.junx.designpattem D/meee: 2
实例只有两个