当前位置:七道奇文章资讯编程技术Java编程
日期:2012-04-05 13:23:00  来源:本站整理

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编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .