openfire源码研究笔记:对设计模式及原则的学习

原文:http://blog.csdn.net/jinzhencs/article/details/50522105

一.拦截器的实现

地点:   package org.jivesoftware.openfire.interceptor-public class InterceptorManager

作用:   在每个packet处理之前先使用拦截器Manager里面的interceptors处理

解释:   这个拦截器Manager是一个List,里面包含多个拦截器的实例,如果需要多加拦截器则只需往里面add就行了.符合            开闭原则,非常轻松的就能扩展功能.

结果:   所以openfire支持自定义插件,它的每个插件就是一个拦截器.

启示:   一个良好的架构必定是能够轻松扩展的,使用这种拦截器的方式能够让我们的系统轻松扩展功能.

           我觉得这个就是aop的实现,并且这种以Manager管理多个拦截器的形式无疑是更好的

           系统可以在两个地方拦截:

                           1.进入的时候(编码,身份验证,加密解密等)

                           2.真正逻辑代码处理的时候(打印日志,事务增强,读取缓存等)

 

图示:InterceptorManager

 

图示:逻辑代码处理之前调用拦截器处理:

 

———————————————————罪恶的分割线————————————————————

 

二.CacheFactoryStrategy,缓存工厂策略模式

策略模式:

作用: 没有设置集群的情况下就用DefaultLocalCacheStategy来创建cache,当设置了集群则自动使用ClusteredCacheFactory来创建

实现:

-声明:

    private static CacheFactoryStrategy cacheFactoryStrategy = new DefaultLocalCacheStrategy();
    private static CacheFactoryStrategy localCacheFactoryStrategy;
    private static CacheFactoryStrategy clusteredCacheFactoryStrategy;

 

@SuppressWarnings("unchecked")
    public static synchronized <T extends Cache> T createCache(String name) {
        T cache = (T) caches.get(name);
        if (cache != null) {
            return cache;
        }
        cache = (T) cacheFactoryStrategy.createCache(name);
        
        log.info("Created cache [" + cacheFactoryStrategy.getClass().getName() + "] for " + name);


        return wrapCache(cache, name);
    }

 public static void stopClustering() {
        // Stop the cluster
        clusteredCacheFactoryStrategy.stopCluster();
        clusteredCacheFactoryStrategy = null;
        // Set the strategy to local
        cacheFactoryStrategy = localCacheFactoryStrategy;
    }

    /**
     * Notification message indicating that this JVM has joined a cluster.
     */
    @SuppressWarnings("unchecked")
    public static synchronized void joinedCluster() {
        cacheFactoryStrategy = clusteredCacheFactoryStrategy;
        // Loop through local caches and switch them to clustered cache (purge content)
        for (Cache cache : getAllCaches()) {
            // skip local-only caches
            if (localOnly.contains(cache.getName())) continue;
            CacheWrapper cacheWrapper = ((CacheWrapper) cache);
            Cache clusteredCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
            cacheWrapper.setWrappedCache(clusteredCache);
        }
        clusteringStarting = false;
        clusteringStarted = true;
        log.info("Clustering started; cache migration complete");
    }


 

解释:

声明的时候就赋值了Default,然后初始化的时候把localCacheFactoryStrategy也实例化成了Default,

然后initialize()的时候使用了localCache.即默认使用的这个

如果发现集群加入,则cacheFactoryStrtegy = clusterCacheFactoryStrategy.则之后创建cache都是使用的集群的创建方法.

 

启示: 策略模式不是多实例实现,而是根据不同的条件选择不同的实现方法实现.

          有点类似于状态模式(状态模式是不是添加个listener监听到状态变化然后就赋值新的实例?)

          但是它是在初始化的时候就决定的.只一次.从这方面来讲,这里算是半个策略半个状态,因为集群启动是在运行过程中而不是初始化,

补充:策略模式还有个重要的角色 环境Context ,这个Context用来实例化接口,然后能够对实例进行增强

链接:http://zhanche2011.iteye.com/blog/1169948

posted @ 2016-11-25 13:56  这个名字想了很久~  阅读(343)  评论(0编辑  收藏  举报