享元模式的实例
好了,废话不多说,直接上代码,首先抽象一个场景:
公司的管理层和普通员工对员工的信息有不同的权限。管理层可以修改员工的个人信息,普员工只能查看不能修改,擅自修改系统会给出提示。员工的个人信息是可以共享的,但是起职能范围和操作权限可以被管理层修改。
抽象享元角色:
[java]
- public interface IFlyWeight {
- /**
- * 判断传入的安全实体和权限,是否和享元对象内部状态匹配
- * 参数 securityEntity 和 permit 是外蕴对象
- * @param securityEntity
- * @param permit
- * @return
- */
- public boolean match(String securityEntity, String permit);
- }
具体享元角色:
[java]
- public class AuthorizationFlyweight implements IFlyWeight {
- /**
- * 内蕴对象,安全实体
- */
- private String mSecurityEntity;
- /**
- * 内蕴对象,权限
- */
- private String mPermit;
- public AuthorizationFlyweight(String state) {
- String str[] = state.split(",");
- this.mSecurityEntity = str[0];
- this.mPermit = str[1];
- }
- @Override
- public boolean match(String securityEntity, String permit) {
- if (mSecurityEntity.equals(securityEntity) && mPermit.equals(permit)) {
- return true;
- }
- return false;
- }
- }
享元工厂:
[java]
- public class FlyWeightFactory {
- private static FlyWeightFactory mFactory = new FlyWeightFactory();
- private FlyWeightFactory() {
- }
- public static FlyWeightFactory getInstance() {
- return mFactory;
- }
- /**
- * 缓存多个人IFlyWeight 对象
- */
- private Map<String, IFlyWeight> flyMap = new HashMap<String, IFlyWeight>();
- /**
- * 获取享元单元
- * @param key
- * @return
- */
- public IFlyWeight getFlyWeight(String key) {
- IFlyWeight fly = flyMap.get(key);
- if(fly==null){
- fly = new AuthorizationFlyweight(key);
- flyMap.put(key, fly);
- }
- return fly;
- }
- }
员工信息的管理工具:
[java]
- public class SecurityManager {
- private static SecurityManager manager = new SecurityManager();
- private SecurityManager() {
- }
- public static SecurityManager getInstance() {
- return manager;
- }
- /**
- * 存放登录人员的权限
- */
- private Map<String, Collection<IFlyWeight>> map = new HashMap<String, Collection<IFlyWeight>>();
- public void logon(String user) {
- Collection<IFlyWeight> coll = queryByUser(user);
- map.put(user, coll);
- }
- /**
- * 从数据库中获取某人所有的权限
- *
- * @param user
- * @return
- */
- private Collection<IFlyWeight> queryByUser(String user) {
- Collection<IFlyWeight> coll = new ArrayList<IFlyWeight>();
- for (String s : TestDB.coll) {
- String str[] = s.split(",");
- if (str[0].equals(user)) {
- IFlyWeight fly = FlyWeightFactory.getInstance().getFlyWeight(
- str[1] + "," + str[2]);
- coll.add(fly);
- }
- }
- return coll;
- }
- /**
- * 判断某个用户对某个安全实体是否有某种权限
- *
- * @param user
- * 用户
- * @param securityEntity
- * 安全实体
- * @param permit
- * 权限
- * @return
- */
- public boolean hasPermit(String user, String securityEntity, String permit) {
- Collection<IFlyWeight> coll = map.get(user);
- if (coll == null || coll.size() == 0) {
- System.out.println(user + "没有登录或者没有该权限...");
- return false;
- }
- for (IFlyWeight fly : coll) {
- if (fly.match(securityEntity, permit)) {
- return true;
- }
- }
- return false;
- }
- }
模拟一个数据库保存员工信息:
[java]
- public class TestDB {
- public static Collection<String> coll = new ArrayList<String>();
- static {
- coll.add("张三,人员列表,查看");
- coll.add("李四,人员列表,查看");
- coll.add("李四,薪资列表,查看");
- coll.add("李四,薪资列表,修改");
- for (short i = 0; i < 3; i++) {
- coll.add("张三" + i + ",人员列表,查看");
- }
- }
- }
具体的实现,本例子是基于android实现的,java只单独写一个Test类在main里面实现就行了:
- SecurityManager manager = SecurityManager.getInstance();
- manager.logon("张三");
- manager.logon("李四");
- boolean b1 = manager.hasPermit("张三", "薪资列表", "查看");
- boolean b2 = manager.hasPermit("李四", "薪资列表", "查看");
- boolean b3 = manager.hasPermit("张三", "人员列表", "查看");
- System.out.println("b1 = "+b1);
- System.out.println("b2 = "+b2);
- System.out.println("b3 = "+b3);
运行效果:
从数据库中可以看出来,张三是人事的普通员工,只能查看公司在职人员,对员工的薪资无权限查看,b1返回false,b3返回true
李四是人事普通员工兼财务主管,可以修改查询员工薪资并且可以查看在职员工信息,b2返回true
如果
[java]
- boolean b4 = manager.hasPermit("王武", "人员列表", "查看");
因为“王武”这个人没有登录,所以提示信息是 “没有登录或者没有该权限...”