读书,有时候,我感觉总是有点绕和不具体。我阅读了代码,理解代码后,才有一种理解和把握的感觉。
优点三、
把某个对象的构建放给客户端来实现。
比如下面的实现,客户端Test,获取Service的实例具体类型,是由服务提供者Provider来确定的。
Services暴露服务提供者的注册方法,不同的服务提供者,会提供不同的实例。如下文,实现了DEFAULT_PROVIDER, COMP_PROVIDER, ARMED_PROVIDER三个服务提供者。
将服务提供者注册到Services中,用名称区别不同的服务提供者。
于是,客户端Test,在获取实例的时候,传递对应的服务提供者名称给Services,这样就可以获取指定类型的实例。
比如,Service s2 = Services.nexInstance("comp").获取的实例类型,是由COMP_PROVIDER来决定的。
// Simple test program for service provider framework package org.effectivejava.examples.chapter02.item01; public class Test { public static void main(String[] args) { // Providers would execute these lines Services. registerDefaultProvider(DEFAULT_PROVIDER); Services. registerProvider("comp", COMP_PROVIDER); Services. registerProvider("armed", ARMED_PROVIDER); // Clients would execute these lines Service s1 = Services.newInstance(); Service s2 = Services.newInstance("comp" ); Service s3 = Services.newInstance("armed" ); System. out.printf("%s, %s, %s%n" , s1, s2, s3); } private static Provider DEFAULT_PROVIDER = new Provider() { public Service newService() { return new Service () { @Override public String toString() { return "Default service" ; } }; } }; private static Provider COMP_PROVIDER = new Provider() { public Service newService() { return new Service () { @Override public String toString() { return "Complementary service" ; } }; } }; private static Provider ARMED_PROVIDER = new Provider() { public Service newService() { return new Service () { @Override public String toString() { return "Armed service" ; } }; } }; }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Services { private Services() { } // Prevents instantiation (Item 4) // Maps service names to services private static final Map<String, Provider> providers = new ConcurrentHashMap<String, Provider>(); public static final String DEFAULT_PROVIDER_NAME = "<def>"; // Provider registration API public static void registerDefaultProvider(Provider p) { registerProvider(DEFAULT_PROVIDER_NAME, p); } public static void registerProvider(String name, Provider p) { providers.put(name, p); } // Service access API public static Service newInstance() { return newInstance( DEFAULT_PROVIDER_NAME); } public static Service newInstance(String name) { Provider p = providers.get(name); if (p == null ) throw new IllegalArgumentException( "No provider registered with name: " + name); return p.newService(); } }
--------------------------------------------------------------------------------------------------------------------------------------------------------
//在接口中,可以定义要客户端实现的方法 public interface Service { // Service-specific methods go here }
-------------------------------------------------------------------------------------------------------------------------------------------------------
// Service provider framework sketch - Service provider interface - Page 12 package org.effectivejava.examples.chapter02.item01; public interface Provider { Service newService(); }