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

来源:互联网 发布:m文件传入double数据 编辑:程序博客网 时间:2024/06/07 06:03

享元模式和组合模式

不需要共享的享元模式

代码示例

直接上代码了,有点累了
(1)测试数据

package flyWeight;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;public class DataBuffer {    public static Collection<String> cll = new ArrayList<>();    public static Map<String,String[]> clm = new HashMap<>();//这个是新增的    static {//注意这里的标记,2为叶子对象,1为组合对象        cll.add("张三,人员列表,查看,2");        cll.add("李四,人员列表,查看,2");        cll.add("李四,操作薪资数据,,1");        clm.put("操作薪资数据", new String[]{"薪资数据,查看","薪资数据,修改"});        for (int i = 0;i < 3;i++)        {            cll.add("张三"+i+",人员列表,查看,2");        }    }}

(2)

package flyWeight;public interface FlyWeight {        public boolean match(String privelage,String securEntity);        public void add(FlyWeight fw);//新增方法}

(3)

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;    }    @Override    public void add(FlyWeight fw) {        // TODO 自动生成的方法存根        throw new UnsupportedOperationException("对象不支持这个功能");    }}

(4)享元工厂

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;    }}

(5)组合对象

package flyWeight;import java.util.ArrayList;import java.util.Collection;public class UnsharedFlyWeight implements FlyWeight {    private Collection<FlyWeight> clf = new ArrayList<>();    @Override    public boolean match(String privelage, String securEntity) {        for (FlyWeight fw : clf)        {            if (fw.match(privelage, securEntity))            {                return true;            }        }        return false;    }    @Override    public void add(FlyWeight fw) {        clf.add(fw);    }}

(6)安全管理

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))            {                FlyWeight fw;            //  System.out.println(str[2]);                if (str[3].equals("1"))                {                    fw = new UnsharedFlyWeight();                    String s[] = DataBuffer.clm.get(str[1]);                    for (String tempS: s)                    {                        System.out.println(tempS);                        FlyWeight fw1 = FlyWeightFactory.getInstance().getFlyWeight(tempS);                        fw.add(fw1);                    }                }                else                {                    fw=FlyWeightFactory.getInstance().getFlyWeight(str[1]+","+str[2]);                }                cl.add(fw);            }        }        return cl;    }}

(7)客户端

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("李四", "薪资数据", "查看");        boolean f3 = scm.hasPermit("李四", "薪资数据", "修改");        System.out.println("f1="+f1);        System.out.println("f2="+f2);        System.out.println("f3="+f3);        for (int i = 0;i < 3;i++)        {            scm.login("张三"+i);            scm.hasPermit("张三"+i, "薪资数据", "查看");        }    }}

运行结果
薪资数据,查看
薪资数据,修改
fl=flyWeight.ConcretFlyWeight@210b5b
fl=flyWeight.ConcretFlyWeight@210b5b
fl=flyWeight.UnsharedFlyWeight@170888e
fl=flyWeight.ConcretFlyWeight@210b5b
fl=flyWeight.UnsharedFlyWeight@170888e
f1=false
f2=true
f3=true
fl=flyWeight.ConcretFlyWeight@210b5b
fl=flyWeight.ConcretFlyWeight@210b5b
fl=flyWeight.ConcretFlyWeight@210b5b