美文网首页
关于单例模式在实际开发中的问题

关于单例模式在实际开发中的问题

作者: alvan_wyal | 来源:发表于2020-01-17 15:58 被阅读0次

1:背景:适配Memcached

@Service(value ="cache")

public class CacheSystem {

private static StringHOST ="";

private static StringPORT ="";

private static StringUSERNAME ="";

private static StringPASSWORD ="";

private static MemcachedClientcache =null;

private static CacheSystem  cacheSystem =null;

public static MemcachedClient getInstance() {

if (cacheSystem ==null) {

synchronized (CacheSystem.class) {

if (cacheSystem ==null) {

try {

HOST = MemcacheConfiguration.ip;

PORT = MemcacheConfiguration.port +"";

if(MemcacheConfiguration.username!=null && MemcacheConfiguration.password!=null ){

USERNAME = MemcacheConfiguration.username;

PASSWORD = MemcacheConfiguration.password;

}

if (HOST.isEmpty() ||PORT.isEmpty()) {

logger.error("Wrong OCS Config!Can't start the OCS cache system!");

}else {

if (USERNAME.isEmpty() ||PASSWORD.isEmpty()) {

//无用户密码方式

                            logger.info("Create the OCS client with no account and pwd......");

cache =new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(HOST +":" +PORT));

}else {

//有用户密码方式

                            logger.info("Create the OCS client with account and pwd......");

AuthDescriptor ad =new AuthDescriptor(new String[]{"PLAIN"},new PlainCallbackHandler(USERNAME,PASSWORD));

cache =new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY)

.setAuthDescriptor(ad)

.build(),

AddrUtil.getAddresses(HOST +":" +PORT));

}

logger.info("OCS client is running.");

}

}catch (Exception e) {

logger.error("Start OCS client unsuccessful!Please make sure that the Config is all right,then restart it again.");

logger.error("[OCS]", e);

}

}

}

}

return cache;

}

2:在下面方法中调用时创建对象

例如:

/**

* 使用方式和get相同,需配合put方法使用

*

* @param

* @param key

* @param typeToken

* @return

*/

public static T getList(String key, TypeToken> typeToken) {

try {

return gson.fromJson((String)getInstance().get(key), typeToken.getType());

}catch (Exception e) {

logger.error("[OCS get error]", e);

return null;

}

}

}

3:使用单例模式中的懒汉模式解决在代码中静态代码块儿优先于配置文件加载;

注:在单例模式中两层判空;防止线程池中有活跃的线程,二次获取;

相关文章

  • 关于单例模式在实际开发中的问题

    1:背景:适配Memcached @Service(value ="cache") public class Ca...

  • 设计模式系列—单例(Singleton Pattern)模式

    《Head First设计模式》读书笔记 单例模式 一,背景介绍 1,为什么要使用单例模式? 在实际的开发中我们有...

  • iOS单例模式容错处理

    ios 单例模式容错处理 1、单例模式的使用和问题解决 在ios开发的过程中,使用单例模式的场景非常多。系统也有很...

  • Kotlin项目中常见用法

    关于单例模式的使用,通过object声明的对象实际为单例模式对象 1不带参数单例 class MyClass pr...

  • 羊皮书APP(Android版)开发系列(二十二)10分钟秒懂单

    单例模式在实际开发过程中经常会用到,我们有必要充分的理解单例模式。单例模式有多种写法,分为懒汉式、饿汉式、双重锁等...

  • 单例模式

    概要 关于单例的设计模式网上资料可以说是一大堆,但是对于单例在实际开发过程中运用,大多都是一知半解,此篇也只是自我...

  • Android基础巩固进阶

    如何创建完美的单例模式? 设计模式在软件开发者中十分受欢迎。设计模式是对于常见软件问题的良好解决方案。单例模式是 ...

  • Swift单例模式

    Swift单例模式 单例模式 单例模式的作用是解决“应用中只有一个实例”的一类问题。在Cocoa Touch框架中...

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS单例模式

    单例模式是在iOS开发中常用的开发模式之一,使用单例模式是为了保证在整个程序中,所定义的属性值是唯一的 之前对于单...

网友评论

      本文标题:关于单例模式在实际开发中的问题

      本文链接:https://www.haomeiwen.com/subject/xsoczctx.html