标记接口模式
public class Marker {
/**
* 标记模式:
* Using empty interfaces as markers to distinguish special treated objects。
* 使用空的标记接口来区分需要特殊处理的对象。
*/
@SuppressWarnings("boxing")
@Test
public void all() {
final PermissionServiceImpl serviceImpl = new PermissionServiceImpl();
final Map<Permission, Boolean> map = serviceImpl.getPermission("zxd");
Assert.assertTrue(map.get(ReadPermission.ALL));
}
}
interface Permission {
}
enum ReadPermission implements Permission {
SELF, ALL;
}
enum WritePermission implements Permission {
SELF;
}
interface PermissionService {
/**
* 高内聚,将所有的权限操作都封装在一处
*/
Map<Permission, Boolean> getPermission(String userId);
}
class PermissionServiceImpl implements PermissionService {
@Override
public Map<Permission, Boolean> getPermission(String userId) {
final Map<Permission, Boolean> map = Maps.newHashMap();
if ("zxd".equals(userId)) {
map.put(ReadPermission.ALL, Boolean.TRUE);
}
map.put(ReadPermission.SELF, Boolean.TRUE);
map.put(WritePermission.SELF, Boolean.TRUE);
return map;
}
}