目录
抽象⼯⼚模式与⼯⼚⽅法模式虽然主要意图都是为了解决,接⼝选择问题。但在实现上,抽象⼯⼚是⼀个中⼼⼯⼚,创建其他⼯⼚的模式。
随着这次的升级,可以预⻅的问题会有;
1. 很多服务⽤到了Redis需要⼀起升级到集群。
2. 需要兼容集群A和集群B,便于后续的灾备。
3. 两套集群提供的接⼝和⽅法各有差异,需要做适配。
4. 不能影响到⽬前正常运⾏的系统。



- public interface CacheService {
- String get(final String key);
- void set(String key, String value);
- void set(String key, String value, long timeout, TimeUnit timeUnit);
- void del(String key);
- }
- public class CacheServiceImpl implements CacheService {
- private RedisUtils redisUtils = new RedisUtils();
-
- public String get(String key) {
- return redisUtils.get(key);
- }
-
- public void set(String key, String value) {
- redisUtils.set(key, value);
- }
-
- public void set(String key, String value, long timeout, TimeUnit timeUnit) {
- redisUtils.set(key, value, timeout, timeUnit);
- }
-
- public void del(String key) {
- redisUtils.del(key);
- }
- }
- public interface ICacheAdapter {
- String get(String key);
- void set(String key, String value);
- void set(String key, String value, long timeout, TimeUnit timeUnit);
- void del(String key);
- }
- public class EGMCacheAdapter implements ICacheAdapter {
- private EGM egm = new EGM();
-
- public String get(String key) {
- return egm.gain(key);
- }
-
- public void set(String key, String value) {
- egm.set(key, value);
- }
-
- public void set(String key, String value, long timeout, TimeUnit timeUnit) {
- egm.setEx(key, value, timeout, timeUnit);
- }
-
- public void del(String key) {
- egm.delete(key);
- }
- }
- public class IIRCacheAdapter implements ICacheAdapter {
- private IIR iir = new IIR();
-
- public String get(String key) {
- return iir.get(key);
- }
-
- public void set(String key, String value) {
- iir.set(key, value);
- }
-
- public void set(String key, String value, long timeout, TimeUnit timeUnit) {
- iir.setExpire(key, value, timeout, timeUnit);
- }
-
- public void del(String key) {
- iir.del(key);
- }
- }
- public static <T> T getProxy(Class<T> interfaceClass, ICacheAdapter cacheAdapter) throws Exception {
- InvocationHandler handler = new JDKInvocationHandler(cacheAdapter);
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- Class<?>[] classes = interfaceClass.getInterfaces();
- return (T) Proxy.newProxyInstance(classLoader, new Class[]{classes[0]}, handler);
- }
- public class JDKInvocationHandler implements InvocationHandler {
- private ICacheAdapter cacheAdapter;
-
- public JDKInvocationHandler(ICacheAdapter cacheAdapter) {
- this.cacheAdapter = cacheAdapter;
- }
-
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- return ICacheAdapter.class.getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args)).invoke(cacheAdapter, args);
- }
-
- }
- @Test
- public void test_CacheService() throws Exception {
- CacheService proxy_EGM = JDKProxy.getProxy(CacheServiceImpl.class, new EGMCacheAdapter());
- proxy_EGM.set("user_name_01","⼩亮哥");
- String val01 = proxy_EGM.get("user_name_01");
- System.out.println(val01);
-
- CacheService proxy_IIR = JDKProxy.getProxy(CacheServiceImpl.class, new IIRCacheAdapter());
- proxy_IIR.set("user_name_01","⼩亮哥");
- String val02 = proxy_IIR.get("user_name_01");
- System.out.println(val02);
- }