设计模式之适配器模式

将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份

Java 适配器模式示例

在Java里面,日志打印框架有很多,那如果设计才能更有效服务于系统呢

public interface MyLog {
	public void info(String str);
}

// 适配器
public interface MyLogAdapter {
	MyLog getLog(Class<?> key);
}

//
public class LogFactory {
	private LogFactory() {
	}
	private static volatile MyLogAdapter mLogAdapter;
	private static final ConcurrentMap<String, MyLog> mLogs = new ConcurrentHashMap<String, MyLog>();

	// 查找常用的日志框架
	static {
		try {
	    	init();
			String logger = System.getProperty("pangugle.application.log", "slf4j");
			if ("slf4j".equals(logger)) {
				setLogAdapter(new Slf4jAdapter());
			}
		} catch (Exception e) {
			 try {
				 setLogAdapter(new JdkAdapter());
       } catch (Throwable e2) {
       }
		}
	}

	public static void setLogAdapter(MyLogAdapter adapter) {
		mLogAdapter = adapter;
	}

	public static MyLog getLog(Class<?> key) {
		MyLog log = mLogs.get(key.getName());
		mLogs.putIfAbsent(key.getName(), log);
		return log;
	}

	public static void init()
	{
		try {
			String filename = "logback.xml";
			ResourceLoader resourceLoader = new DefaultResourceLoader();
			InputStream is = resourceLoader.getResource("classpath:config/" + filename).getInputStream();
			loadLogback(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void loadLogback(InputStream inputStream)
	{
		try {
			if(inputStream == null)
			{
				throw new RuntimeException("logback config is not exist for ");
			} else
			{
				LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
				JoranConfigurator configurator = new JoranConfigurator();
				configurator.setContext(context);
				context.reset();
				configurator.doConfigure(inputStream);
				StatusPrinter.printInCaseOfErrorsOrWarnings(context);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

// slf adapter
public class Slf4jAdapter implements MyLogAdapter{
	public MyLog getLog(Class<?> key) {
		return new Slf4jLog(org.slf4j.LoggerFactory.getLogger(key));
	}
	public MyLog getLog(String key) {
		return new Slf4jLog(org.slf4j.LoggerFactory.getLogger(key));
	}
}

public class Slf4jLog implements MyLog{
	private final org.slf4j.Logger logger;
	public Slf4jLog(org.slf4j.Logger logger) {
		this.logger = logger;
	}
	public void info(String msg) {
		logger.info(msg);
	}
	public void info(String msg, Throwable e) {
		logger.info(msg, e);
	}
}

// jdk adapter
public class JdkLog implements MyLog {
	private final java.util.logging.Logger logger;
	public JdkLog(java.util.logging.Logger logger) {
		this.logger = logger;
	}
	public void info(String msg) {
		logger.log(Level.INFO, msg);
	}
	public void info(String msg, Throwable e) {
		logger.log(Level.INFO, msg, e);
	}
}

public class JdkAdapter implements MyLogAdapter{
	public MyLog getLog(Class<?> key) {
		return new JdkLog(java.util.logging.Logger.getLogger(key == null ? "" : key.getName()));
	}
}

// 使用
public class App
{
  public static final MyLog LOG = LogFactory.getLog(App.class);
  public void test()
  {
    LOG.info("==============");
  }
}

这个是 盘古歌技术 项目的日志框架简写版本,应对各种日志框架,非常实用,是真正的应用场景! 盘古歌技术所有项目都是用这个日志框架的!