Java学习笔记-01 考虑用静态工厂办法替换构造器[Java编程]
本文“Java学习笔记-01 考虑用静态工厂办法替换构造器[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
关于类而言,为了让客户端获得它自身的一个实例,最常用的办法就是供应一个公有的构造器.类也可以供应一个公有的静态工厂办法,它只是一个返回类的实例的静态办法.下面是一个来自Boolean的简单示例.
[java] public static Boolean valueOf(boolean b) {
return b ? Boolean.TRUE : Boolean.False;
}
public static Boolean valueOf(boolean b) {
return b ? Boolean.TRUE : Boolean.False;
}
静态工厂办法与构造器差别的第一大上风在于,它们闻名称.
静态工厂办法与构造器差别的第二大上风在于,没必要在每次调用它们的时刻都成立一个新对象.
静态工厂办法与构造器差别的第三大上风在于,它们可以返回原返回范例的任何子范例的对象.
静态工厂办法的第四大上风在于,在成立参数化范例实例的时刻,它们使代码变得越发简便.
静态工厂办法的主要缺陷在于,类假如不含有公有的或受保护的构造器,就不能被子类化.
静态工厂办法的第二个缺陷在于,它们与去他的静态办法实际上没有任何辨别.
服务供应者框架:
[java]
// Service provider framework sketch - Service interface
public interface Service {
// Service-specific methods go here
}
// Service provider framework sketch - Service interface
public interface Service {
// Service-specific methods go here
}
[java]
// Service provider framework sketch - Service provider interface
public interface Provider {
Service newService();
}
// Service provider framework sketch - Service provider interface
public interface Provider {
Service newService();
}[java]
// Service provider framework sketch
// Noninstantiable class for service registration and access
import java.util.*;
import java.util.concurrent.*;
public class Services {
private Services() { } // Prevents instantiation (Item 4)
// Maps service names to services
private static final Map providers =
new ConcurrentHashMap();
public static final String DEFAULT_PROVIDER_NAME = "";
// 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();
}
}
// Service provider framework sketch
// Noninstantiable class for service registration and access
import java.util.*;
import java.util.concurrent.*;
public class Services {
private Services() { } // Prevents instantiation (Item 4)
// Maps service names to services
private static final Map providers =
new ConcurrentHashMap();
public static final String DEFAULT_PROVIDER_NAME = "";
// 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();
}
}
[java] // Simple test program for service provider framework
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";
}
};
}
};
}
摘自 horsttnann的专栏
以上是“Java学习笔记-01 考虑用静态工厂办法替换构造器[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |