美文网首页
服务定位器模式

服务定位器模式

作者: Stroman | 来源:发表于2019-11-08 15:03 被阅读0次

    综述

    说到底,这就是个缓存的应用。这就好比是考试,你已经知道答案了,但是别人不知道,那别人就得通过去解题来获取答案,你知道答案你就省事了呗,只要试题不变,你这个答案始终有效,而别人始终都需要去解题。是不是很简单明了的比拟?实际上也就是这么简单的。

    服务定位器

    它是直接或者间接与用户对接的层次,它的输出就是用户想要的东西,因为用户关心的事是得没得到他想要的东西,至于怎么得到用户并不关心,于是直接用这个层次对用户进行供给。

    如何提供给用户他想要的东西呢?

    那这个就是服务定位器的内部实现了,原则也很简单,先找缓存,找到了就返回,没找到,再去按照最原本的方式寻找,找到了就更新缓存信息,然后返回,否则就真没有了。很简单。

    查找器

    这个就是在缓存中没有想要的信息的时候,就靠它来进行查找了。

    类图

    类图.png

    效果

    在缓存中没找到Message1信息
    在缓存之外重新寻找Message1信息
    在缓存中没找到Message2信息
    在缓存之外重新寻找Message2信息
    在缓存中找到了Message1信息
    在缓存中找到了Message2信息
    在缓存中没找到Message3信息
    在缓存之外重新寻找Message3信息
    看来Message3信息不存在
    此信息不存在
    
    Process finished with exit code 0
    

    调用

    package com.company;
    
    public class Main {
    
        public static void main(String[] args) {
        // write your code here
            InformationProvider provider = new InformationProvider();
            provider.getInformation("Message1");
            provider.getInformation("Message2");
            provider.getInformation("Message1");
            provider.getInformation("Message2");
            provider.getInformation("Message3");
        }
    }
    
    

    服务定位器

    package com.company;
    
    public class InformationProvider {
        private static InformationCache cache;
    
        static {
            //这个是Java的语法,用来初始化静态变量的静态块。
            cache = new InformationCache();
        }
    
        public InformationInterface getInformation(String identifier) {
            InformationInterface result = InformationProvider.cache.getInformation(identifier);
            if (result != null) {
                return result;
            }
            Finder finder = new Finder();
            result = finder.lookuper(identifier);
            if (result != null) {
                InformationProvider.cache.addInformation(result);
            } else {
                System.out.println("此信息不存在");
            }
            return result;
        }
    }
    
    

    缓存

    package com.company;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class InformationCache {
        private List<InformationInterface> informations;
    
        public InformationCache() {
            this.informations = new ArrayList<>();
        }
    
        /**
         * 从缓存中根据信息的标识符来获取信息。
         * @param identifier
         * @return
         */
        public InformationInterface getInformation(String identifier) {
            for (InformationInterface iterator:this.informations) {
                if (iterator.getIdentifier().equals(identifier)) {
                    System.out.println("在缓存中找到了" + identifier + "信息");
                    return iterator;
                }
            }
            System.out.println("在缓存中没找到" + identifier + "信息");
            return null;
        }
    
        /**
         * 当有新的信息的时候,把它添加到缓存里面。
         * @param aInformation
         */
        public void addInformation(InformationInterface aInformation) {
            this.informations.add(aInformation);
        }
    }
    
    

    信息查找器

    package com.company;
    
    /**
     * 它就是在缓存中没有找到想要的信息的时候被使用的
     * 用于查找信息的模块
     */
    public class Finder {
        /**
         * 在这里简化了逻辑,直接创建一个信息返回即可。
         * @param identifier
         * @return
         */
        public InformationInterface lookuper(String identifier) {
            System.out.println("在缓存之外重新寻找" + identifier + "信息");
            if (identifier.equals("Message1")) {
                return new Information1();
            } else if (identifier.equals("Message2")) {
                return new Information2();
            }
            System.out.println("看来" + identifier + "信息不存在");
            //到这就没办法了,或者压根就不存在此种信息。
            return null;
        }
    }
    
    

    信息接口

    package com.company;
    
    public interface InformationInterface {
        /**
         * 获取某个信息的标识符
         * @return
         */
        public String getIdentifier();
    }
    
    

    信息1

    package com.company;
    
    public class Information1 implements InformationInterface {
        @Override
        public String getIdentifier() {
            return "Message1";
        }
    }
    
    

    信息2

    package com.company;
    
    public class Information2 implements InformationInterface {
        @Override
        public String getIdentifier() {
            return "Message2";
        }
    }
    
    

    多谢捧场

    如果您觉得我的文章有价值,那么赏脸打赏一个,鄙人感激不尽。不过,不打赏看看也是好的,如果有不对的地方,还请您多多指正。



    相关文章

      网友评论

          本文标题:服务定位器模式

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