设计模式之享元模式(2)

来源:互联网 发布:如何禁止手机安装软件 编辑:程序博客网 时间:2024/05/21 12:25

本质

分离与共享。

代码示例

(1)定义描述授权数据的享元接口

package flyWeight;public interface FlyWeight {        public boolean match(String privelage,String securEntity);}

(2)实现享元对象

package flyWeight;//封装授权数据中重复出现的享元对象public class ConcretFlyWeight implements FlyWeight {    //内部状态,权限    private String privelage;    //内部状态,安全实体    private String securEntity;    //传入状态数据,安全实体+","+权限    public ConcretFlyWeight(String key) {        String[] ss = key.split(",");        this.privelage = ss[1];        this.securEntity = ss[0];    }    public String getPrivelage()    {        return privelage;    }    public String getSecurEntity()    {        return securEntity;    }    @Override    public boolean match(String privelage, String securEntity) {        // TODO 自动生成的方法存根        if (this.privelage.equals(privelage) && this.securEntity.equals(securEntity))        {            return true;        }        return false;    }}

(3)提供享元工厂来负责享元对象的共享管理和对外提供享元对象的接口。

package flyWeight;import java.util.HashMap;import java.util.Map;public class FlyWeightFactory {    private  static FlyWeightFactory fwf = new FlyWeightFactory();    private FlyWeightFactory()    {    }    public  static FlyWeightFactory getInstance()    {        return fwf;    }    //缓存多个FlyWeight对象    private Map<String,FlyWeight> hm = new HashMap<>();    public FlyWeight getFlyWeight(String key)    {        FlyWeight fw = hm.get(key);        if (hm.get(key) == null)        {                fw = new ConcretFlyWeight(key);                hm.put(key, fw););//这句话第一次忘写了,导致与上篇博客相同的结果        }        return fw;    }}

(4)使用享元对象

package flyWeight;import java.util.HashMap;import java.util.Map;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;public class SecurityMsg {//享元对象实现成单例。    private static SecurityMsg sm = new SecurityMsg();    private SecurityMsg()    {    }    public static SecurityMsg getIntance()    {        return sm;    }    private Map<String,Collection<FlyWeight>> hMap = new HashMap<>();    public void login(String user)    {        Collection<FlyWeight> cll = queryByUser(user);        hMap.put(user,cll   }    public boolean hasPermit(String user,String securEntity,String privilage)    {        Collection<FlyWeight> cll = hMap.get(user);        if (cll == null || cll.size() == 0)        {            System.out.println("没有设置"+user+"访问权限");            return false;        }        for (FlyWeight fl:cll)        {            System.out.println("fl="+fl);            if (fl.match(privilage, securEntity))            {                return true;            }        }        return false;    }    //从数据库中获取某人所拥有的权限    public Collection<FlyWeight> queryByUser(String user)    {        Collection<FlyWeight> cl = new ArrayList<>();        //FlyWeight fw = null;        for (String ss:DataBuffer.cll)        {            String[] str = ss.split(",");            if (str[0].equals(user))            {                String key = (str[1]+","+str[2]);                 FlyWeight fw = FlyWeightFactory.getInstance().getFlyWeight(key);//这里使用的是享元工厂创建享受元对象                 cl.add(fw);            }        }        return cl;    }}

(5)客户端

package flyWeight;public class Client {    public static void main(String[] args)    {        SecurityMsg scm = SecurityMsg.getIntance();        scm.login("张三");        scm.login("李四");        boolean f1 = scm.hasPermit("张三", "薪资数据", "查看");        //scm.login("李四");        boolean f2 = scm.hasPermit("李四", "薪资数据", "查看");        System.out.println("f1="+f1);        System.out.println("f2="+f2);        for (int i = 0;i < 3;i++)        {            scm.login("张三"+i);            scm.hasPermit("张三"+i, "薪资数据", "查看");        }    }}

运行结果:
fl=flyWeight.ConcretFlyWeight@1fc6e42
fl=flyWeight.ConcretFlyWeight@1fc6e42
fl=flyWeight.ConcretFlyWeight@1aaf0b3
f1=false
f2=true
fl=flyWeight.ConcretFlyWeight@1fc6e42
fl=flyWeight.ConcretFlyWeight@1fc6e42
fl=flyWeight.ConcretFlyWeight@1fc6e42
很明显,只由2个对象了。