当前位置: 首页 > news >正文

宝鸡政府网站建设公司/站长素材音效下载

宝鸡政府网站建设公司,站长素材音效下载,企业门户网址,网站设计需要考虑的基本原则7.3Java设计模式快速入门之服务定位器模式 7.3.1概念 服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务…

7.3Java设计模式快速入门之服务定位器模式

7.3.1概念

服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。

  • 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
  • Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
  • 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
  • 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。

7.3.2实现

我们将创建 ServiceLocator、InitialContext、Cache、Service 作为表示实体的各种对象。Service1 和 Service2 表示实体服务。
ServiceLocatorPatternDemo,我们的演示类在这里是作为一个客户端,将使用 ServiceLocator 来演示服务定位器设计模式。

步骤1创建服务接口Service。
public interface Service {public String getName();public void execute();
}
步骤2创建实体服务。
public class Service1 implements Service {public void execute(){System.out.println("Executing Service1");}@Overridepublic String getName() {return "Service1";}
}public class Service2 implements Service {public void execute(){System.out.println("Executing Service2");}@Overridepublic String getName() {return "Service2";}
}
步骤3为JNDI查询创建 InitialContext。
public class InitialContext {public Object lookup(String jndiName){if(jndiName.equalsIgnoreCase("SERVICE1")){System.out.println("Looking up and creating a new Service1 object");return new Service1();}else if (jndiName.equalsIgnoreCase("SERVICE2")){System.out.println("Looking up and creating a new Service2 object");return new Service2();}return null;      }
}
步骤4创建缓存 Cache。
import java.util.ArrayList;
import java.util.List;public class Cache {private List<Service> services;public Cache(){services = new ArrayList<Service>();}public Service getService(String serviceName){for (Service service : services) {if(service.getName().equalsIgnoreCase(serviceName)){System.out.println("Returning cached  "+serviceName+" object");return service;}}return null;}public void addService(Service newService){boolean exists = false;for (Service service : services) {if(service.getName().equalsIgnoreCase(newService.getName())){exists = true;}}if(!exists){services.add(newService);}}
}
步骤5创建服务定位器。
public class ServiceLocator {private static Cache cache;static {cache = new Cache();    }public static Service getService(String jndiName){Service service = cache.getService(jndiName);if(service != null){return service;}InitialContext context = new InitialContext();Service service1 = (Service)context.lookup(jndiName);cache.addService(service1);return service1;}
}
步骤6使用 ServiceLocator 来演示服务定位器设计模式。
public class ServiceLocatorPatternDemo {public static void main(String[] args) {Service service = ServiceLocator.getService("Service1");service.execute();service = ServiceLocator.getService("Service2");service.execute();service = ServiceLocator.getService("Service1");service.execute();service = ServiceLocator.getService("Service2");service.execute();      }
}
步骤7执行程序,输出结果:
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached  Service1 object
Executing Service1
Returning cached  Service2 object
Executing Service2
步骤8查看类关系图。

在这里插入图片描述

http://www.lbrq.cn/news/1531135.html

相关文章:

  • 西安广告公司网站建设/百度推广助手客户端
  • 邯郸媒体网络营销诚信合作/seo站长平台
  • 苏州 ebcart 网站开发/友情链接平台赚钱吗
  • 怎么做虚拟的网站/36优化大师下载安装
  • 网站做投票系统/二十条优化措施
  • 通辽建设公司网站/百度官网电话客服24小时
  • 做直播网站赚钱/央视新闻的新闻
  • 网站怎么做二维码/市场调研报告
  • 创建一个网站/建站工具
  • 网站建设公司做销售好不好/sem投放是什么意思
  • 湖北工程建设总承包有限公司网站/全网霸屏推广系统
  • 中国人事建设部网站/新型网络搜索引擎
  • 如何查询网站是哪家公司做的/女生读网络营销与电商直播
  • 网站设计前期沟通单/如何优化关键词的方法
  • seo是什么服/关键词快速优化排名软件
  • 如何做转发文章赚钱的网站/营销网站建设价格
  • 昆明网站制作方案/宁波seo博客
  • 本溪建网站/广州排名推广
  • yii2框架做的网站有哪些/南京百度竞价推广公司排名
  • 网站多语言建设方案/百度西安
  • 给网站做排名优化学什么好处/百度手机助手网页
  • 梦织做网站/蚂蚁链接bt链接
  • 一般建设网站的布局/模板网站建设开发
  • 网站开发的价格/创意营销案例
  • 做网站收录/广州seo优化外包服务
  • 如何做国外销售网站/营销案例分享
  • 自学网站建设好学吗/疫情二十条优化措施
  • 做网站 如何注册公司/google商店
  • 政府门户网站集群建设工程招标/软文街
  • 无基础想学室内设计/快照关键词优化
  • 大视协作码垛机:颠覆传统制造,开启智能工厂新纪元
  • 活性数据处理与标准化
  • 从零开始理解 K 均值聚类:原理、实现与应用
  • openEuler系统中r如何将docker安装在指定目录
  • Qt 中最经典、最常用的多线程通信场景
  • jvm三色标记