有关单例模式的使用方式

早前一直对单例模式和静态工具类的功能重叠有些疑惑,昨天做了一个功能,忽然发现了一些单例可以实现但是静态工具类无法满足的需求。

 

我有一个接口IAuthorization:

有关单例模式的使用方式有关单例模式的使用方式View Code
/**
 * 所有需要配置权限的功能块都必须实现本接口
 * 
 * @author caiyu
 * @date Nov 16, 2012 9:36:49 AM
 * 
 */
public interface IAuthorization {
    // 权限开关 false有验证,true无验证
    public static final boolean open = false;

    String XML_NEW = "new";
    String XML_DELETE = "delete";
    String XML_MODIFY = "modify";
    String XML_DEPLOY = "deploy";
    String XML_COMPILE = "compile";
    String XML_SYNC = "sync";
    String XML_EXPORT = "export";
    String XML_EXPORT_DETAIL = "export_detail";
    String XML_BROWSE = "browse";

    String EXT_TCD = "tcd";
    String EXT_BCD = "bcd";
    String EXT_TRD = "trd";
    String EXT_TWF = "twf";
    String EXT_TMPL = "tmpl";
    String EXT_DICT = "dict";
    String EXT_F_PROJECT = "folder_project";
    String EXT_F_APPLICATION = "folder_application";
    String EXT_FPCK = "fpck";
    String EXT_NATP = "pck";
    /**
     * 注册权限委托
     * 
     * @return
     */
    IAuthDelegate registerDelegate();

    /**
     * 处理权限委托
     * 
     * @param delegate
     *            委托
     * @param selection
     * @return 是否具备权限
     */
    boolean handleDelegate(IAuthDelegate delegate, Object selection);
}

要使用权限管理的类必须实现该接口,现在我有两个编辑器(Editor)需要被权限管理,当没有编辑权限的时候,将该编辑器置为不可编辑状态。由于以后编辑器会不停的增加,所以需要使用一个通用的IAuthorization的工具类来实现权限管理。

既然是工具类,那么就说明我们不需要多个实例,就应该使用单例或是或是静态工具类了。

既然是实现自IAuthorization,就限定了该类的实现方法不能为static,于是单例模式应用就呼之欲出了。

看看实现:

------------------------------------下面是SB的实现,不要看----------------------------

public class PacketAuthManager implements IAuthorization {
    public final static PacketAuthManager INSTANCE;
    static{
        INSTANCE=new PacketAuthManager();
    }
    
    protected IActionAuthDelegate authDele;
   
    private String actionPermCode;

    private PacketAuthManager() {
        authDele = (IActionAuthDelegate) registerDelegate();
        actionPermCode = AuthCodeUtil.calculateActionPermCode(XML_MODIFY);
    }

    @Override
    public IAuthDelegate registerDelegate() {
        return AuthDelegateFactory.newDelegate(AuthDelegateFactory.TYPE_ACTION);
    }

    public boolean handleDelegate(String resource) {
        return handleDelegate(authDele, resource);
    }

    @Override
    public boolean handleDelegate(IAuthDelegate delegate, Object selection) {
        String resourceCode = null;
        if (selection instanceof String) {
            resourceCode = AuthCodeUtil
                    .calculateResourcePermCode((String) selection);
        }
        delegate.setPermIDs(new String[] { AuthCodeUtil.calculatePermissionId(
                actionPermCode, resourceCode) });
        AuthController.getInstance().handleDelegate(delegate);
        return ((IActionAuthDelegate) delegate).isActionEnabled() || open;
    }

}

 

 -----------------------------------后面才是正确的实现------------------------------------------

 

/**
 * 通用SQL类型工具
 * 
 * @author caiyu
 * @date 2013 2013-7-15 上午10:54:17
 */
public final class GenericSqlTypeUtil {
    private static GenericSqlTypeUtil INSTANCE;
    private final static String TEMPLATE = "/genericSqlType.xml";
    private final Map<String, String> genericSqlTypeCache;

    private GenericSqlTypeUtil() {
        genericSqlTypeCache = new HashMap<String, String>();
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        SAXReader reader = null;
        Document doc = null;
        InputStream is = null;
        try {
            is = FileLocator.openStream(Activator.getDefault().getBundle(),
                    new Path(TEMPLATE), false);
            // is = new FileInputStream(TEMPLATE);
            reader = new SAXReader();
            doc = reader.read(is);
            // TODO
            // 对genericSqlTypeCache做初始化,genericSqlTypeCache的KEY为取自name,VALUE取自desc
            Element root = doc.getRootElement();
            List<?> list = root.elements("type");
            for (Object o : list) {
                Element element = (Element) o;
                String name = element.attributeValue("name");
                String desc = element.elementText("desc");
                genericSqlTypeCache.put(name, desc);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            if (reader != null)
                reader.resetHandlers();
            if (doc != null)
                doc.clearContent();
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    /**
     * 根据通用的SQL类型名称获取类型描述
     * 
     * @param genericSqlTypeName
     *            参考java.sql.Types
     * @return
     */
    public String getSqlTypeDescription(String genericSqlTypeName) {
        return genericSqlTypeCache.get(genericSqlTypeName);
    }

    public static GenericSqlTypeUtil getInstance() {
        if (INSTANCE == null)
            INSTANCE = new GenericSqlTypeUtil();
        return INSTANCE;
    }
}

 

 

 

 

上一篇:PS网页设计教程XXII——在PS中创建单页复古网页布局


下一篇:使用cisco设备要注意的一些问题?