实际使用 工厂模式+适配器模式
来源:互联网 发布:淘宝模版如何制作 编辑:程序博客网 时间:2024/06/06 01:48
目录
大讲设计模式的文章有很多,可以实际用起来却从来没有人讲过,我一直都坚信,必须是 自己见过猪跑,才会想起怎么去吃猪肉,很多文章都只是讲一个例子,但是实际生产中,很多人是无法根据例子直接可以在实际开发中使用的,所以我直接告诉你怎么来使用设计模式,保证你可以直接在项目中使用,
使用场景
在项目中我需要使用缓存, redis集群, redis, 本地缓存,于是我就有三种选择,为了能方便的创建者三种缓存机制,于是我需要一个工厂类,用于生产这三中 缓存的 容器,
于是 很自然我就 想到了 工厂模式,直接使用最简单的工厂模式
1.public class RedisCache{ 方法: get() set() remove() hash() exsits()} 两者方法都差不多,因为使用时 差不多2. public class LocalCache{ 方法: get() set() remove() hash() exsits()}3。工厂类public class Cache { static { Properties properties = new Properties(); try { InputStream fs = Cache .class.getClassLoader().getResourceAsStream("cache.properties"); properties.load(fs); if (properties.getString("handle").equalsIgnoreCase("redis")) { cache = new RedisCache(Redis.class); } else if (properties.getString("handle").equalsIgnoreCase("rediscluster")) { cache = new RedisCache(RedisCluster.class); } else { cache = new LocalCache(); } } catch (IOException e) { e.printStackTrace(); cache = new LocalCache(); } properties.clear(); }}
**工厂类中 我直接使用, static 块,当加载这个类时,就会执行这个初始化的操作,通过配置文件控制,到底选择哪一种的 缓存机制,
其次 这三种缓存机制,肯定有很多方法是一样的,于是我需要 把他们抽象出来,于是我需要一个 interface 被 RedisCache+LocalCache 实现,
public interface CacheAdapter { Boolean exists(String type, Object key); void set(String type, Object key, Object value); }于是 RedisCache+LocalCache需要 implements CacheAdapter
再者 由于 我使用了 Redis + RedisCluster
这两者不管有多少个链接 肯定是只能有一套链接配置,所以我这里需要使用到了单例模式
public class RedisConfig { private static RedisConfig redisConfig = null; // 懒汉式: public static RedisConfig getInstance() { if (redisConfig == null) { redisConfig = new RedisConfig(); } return redisConfig; } }
这两者 又是 相当于 同工厂生产出的不同 车,但是车的 各种属性和方法又是一样的,于是 我需要给他们 抽象出一个 父类 ,
1。public abstract class RedisBase { public abstract void set(byte[] key, byte[] value); public abstract void set(byte[] key, byte[] value, int seconds); }2。public class Redis extends RedisBase { private String auth = null; private List<JedisShardInfo> redisHosts = new ArrayList<>(); private static ShardedJedisPool pool = null;3。public class RedisCluster extends RedisBase { private String auth = null; private JedisCluster jedisCluster; private Set<HostAndPort> jedisClusterNodes = new HashSet<>(); private int timeout = 0; public RedisCluster() { if (jedisCluster == null) { JedisPoolConfig config = getConfig(); jedisCluster = new JedisCluster(jedisClusterNodes, timeout, config); } }
继续探究,我使用 Cache这个工厂
由于我将 RedisCache+LocalCache 做了抽象
于是 我这里使用了 对象的适配器模式+简单工厂模式结合
public class Cache { private static CacheAdapter cache; static { Properties properties = new Properties(); try { InputStream fs = RedisCache.class.getClassLoader().getResourceAsStream("cache.properties"); properties.load(fs); if (properties.getString("handle").equalsIgnoreCase("redis")) { cache = new RedisCache(Redis.class); } else if (properties.getString("handle").equalsIgnoreCase("rediscluster")) { cache = new RedisCache(RedisCluster.class); } else { cache = new LocalCache(); } } catch (IOException e) { e.printStackTrace(); cache = new LocalCache(); } properties.clear(); } public static void set(String type, Object key, Object value) { cache.set(type, key, value); }
详细代码参考:http://download.csdn.net/download/sinat_27639721/10139171
阅读全文
0 0
- 实际使用 工厂模式+适配器模式
- 工厂模式,策略模式,适配器模式
- 设计模式(工厂模式、适配器模式)
- 设计模式中工厂模式实际体验
- 适配器模式(工厂模式8)
- 解析简单工厂模式和适配器模式
- 接口的实际应用--适配器模式
- 设计模式(单例模式,工厂模式,适配器模式)
- 接口的实际应用--工厂设计模式
- 适配器模式具体使用
- 工厂模式与抽象工厂在实际项目中的应用
- 工厂模式的使用
- 适配器模式的简单使用
- 简单的工厂模式和工厂方法模式和适配器模式
- 黑马程序员——java-模式(工厂模式,单列模式,适配器模式)
- 适配器模式
- 适配器模式
- 适配器模式
- Error: "title_activity_hi" is translated here but not found in default locale [ExtraTranslation]
- vue的笔记
- LCS 求最长公共子序列 dp
- Android中AIDL的工作原理
- vim 使用
- 实际使用 工厂模式+适配器模式
- linux 中 xargs、cut 以及管道综合应用
- Android 开发之漫漫长途Ⅰ—Android 系统的创世之初以及 Activity 的生命周期
- js的null和undefined的区别-js基本数据类型
- HQL常用函数
- 骑士共存问题
- centos升级内核
- USB2.0速度识别(USB上电分析)
- Swing中JList的相关问题(2)