java设计模式进阶_service-locator

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Service.java
//  @ Date : 2016/9/2
//  @ Author : 
//
//



/**
 * This is going to be the parent service interface which we will
 * use to create out services.All services will have a
 * <li>service name</li>
 * <li>unique id</li>
 * <li>execution work flow</li>
 *
 */
public interface Service {
    /**
     * the human readable name of the service
     * @return
     */
    public String getName();

    /**
     * Unique ID of the particular service
     * @return
     */
    public int getId();

    /**
     * the workflow method that defines what this service does
     */
    public void execute();
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ServiceImpl.java
//  @ Date : 2016/9/2
//  @ Author : 
//
//



/**
 * This is a single service implementation of a sample service.This is the actual
 * service that will process the request.The reference for this service is to
 * be looked upon in the JNDI server that can be set in the web.xml deployment descriptor
 *
 */
public class ServiceImpl implements Service {
    private final String serviceName;
    private final int id;

    public ServiceImpl(String serviceName) {
        //set the service name
        this.serviceName = serviceName;

        //Generate a random id the this ervice object
        this.id = (int)Math.floor(Math.random()*1000) + 1;
    }

    public String getName() {
        return serviceName;
    }

    public int getId() {
        return id;
    }

    public void execute() {
        System.out.println("Service " + getName() + " is now executing with id " + getId());
    }
}


import java.util.HashMap;
import java.util.Map;

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ServiceCache.java
//  @ Date : 2016/9/2
//  @ Author : 
//
//



/**
 * The service cache implementation which will cache services that are being created.
 * On first hit,the cache will be empty and thus any service that is being requested,will be
 * created fresh and the placed into the cache map.On next hit,if same service name will
 * be requested,it will be returned from the cache
 *
 */
public class ServiceCache {

    private final Map<String,Service> serviceCache;

    public ServiceCache() {
        serviceCache = new HashMap<String,Service>();
    }

    /**
     * Get the service from the cache.null if no service is found matching the name
     * 
     */
    public Service getService(String serviceName) {
        Service cachedService = null;
        for(String serviceJndiName : serviceCache.keySet()) 
        {
            if(serviceJndiName.equals(serviceName))
            {
                cachedService = serviceCache.get(serviceJndiName);
                System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !");
            }
        }

        return cachedService;
    }

    /**
     * Adds the service into the cache map
     * @param service
     */
    public void addService(Service service) {
        serviceCache.put(service.getName(),service);
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ServiceLocator.java
//  @ Date : 2016/9/2
//  @ Author : 
//
//



/**
 * The service locator module.
 * Will fetch service from cache,otherwise creates a fresh service and update cache
 *
 */
public class ServiceLocator {

    private static ServiceCache serviceCache = new ServiceCache();

    /**
     * Fetch the service with the name param from the cache first,
     * if no service is found,lookup the service from the InitContext and
     * then add the newly created service into the cache map for future requests.
     * 
     * @param serviceName
     * @return
     */
    public static Service getService(String serviceName) {
        Service serviceObj = serviceCache.getService(serviceName);
        if(serviceObj != null)
            return serviceObj;
        else{
            /**
             * If we are unable to retrive anything from cache,then
             * lookup the service and add it in the cache map
             */
            InitContext ctx = new InitContext();
            serviceObj = (Service)ctx.lookup(serviceName);
            serviceCache.addService(serviceObj);;
            return serviceObj;
        }
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : InitContext.java
//  @ Date : 2016/9/2
//  @ Author : 
//
//



/**
 * For JNDI lookup of services from the web.xml Will match name of the service name that
 * is being requested and return a newly created service object with the name
 *
 */
public class InitContext {

    /**
     * Perform the lookup based on the service name.The returned object will need to be
     * casted into a Service
     * @param serviceName
     * @return
     */
    public Object lookup(String serviceName) {
        if (serviceName.equals("jndi/serviceA")) {
            return new ServiceImpl("jndi/serviceA");
        } else if (serviceName.equals("jndi/serviceB")) {
            return new ServiceImpl("jndi/serviceB");
        } else {
            return null;
        }
    }
}

/**
 * Service locator pattern,used to lookup jndi services
 * and cache them for subsequent requests.
 * @author Administrator
 *
 */
public class App {

    public static void main(String[] args) {
        Service service = ServiceLocator.getService("jndi/serviceA");
        service.execute();

        service = ServiceLocator.getService("jndi/serviceB");
        service.execute();

        service = ServiceLocator.getService("jndi/serviceA");
        service.execute();

        service = ServiceLocator.getService("jndi/serviceA");
        service.execute();
    }
}

/*
Service jndi/serviceA is now executing with id 908
Service jndi/serviceB is now executing with id 893
(cache call) Fetched service jndi/serviceA(908) from cache... !
Service jndi/serviceA is now executing with id 908
(cache call) Fetched service jndi/serviceA(908) from cache... !
Service jndi/serviceA is now executing with id 908


*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值