Advantage

  • Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Random)
  • They are not required to create a new object each time they're invoked(Flyweight pattern). This ensures that a.equals(b) if and only if a = = b. Then the client can use = = instead of equals method which gets better performance to Equals method.
  • It can return an object of any subtype of their return type.(This gives you great flexibility in choosing the class of the returned object. )

    /********************sample code ******************/

    // Service provider framework sketch

    // Service interface

    public interface Service {

    ... // Service-specific methods go here

    }

    // Service provider interface

    public interface Provider {

    Service newService();

    }

    // Noninstantiable class for service registration and access

    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();

    }

    }

  • Reduce the verbosity of creating parameterized type instances.
Disadvantages
  • The class without public or protected constructors cannot be subclassed.
  • They are not readily distinguishable from other static methods.

Here are some common names for static factory methods:

• valueOf—Returns an instance that has, loosely speaking, the same value as its

parameters. Such static factories are effectively type-conversion methods.

• of—A concise alternative to valueOf, popularized by EnumSet(Item 32).

• getInstance—Returns an instance that is described by the parameters but

cannot be said to have the same value. In the case of a singleton, getInstance

takes no parameters and returns the sole instance.

• newInstance—LikegetInstance, except that newInstanceguarantees that

each instance returned is distinct from all others.

• getType—Like getInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

• newType—Like newInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

Effective Java 01 Consider static factory methods instead of constructors的更多相关文章

  1. Effective Java - Item 1: Consider static factory methods instead of constructors

    考虑使用静态工厂方法来替代构造方法, 这样的做的好处有四点. 1. 更好的表意 有的构造方法实际上有特殊的含义, 使用静态工厂方法能更好的表达出他的意思. 例如 BigInteger(int, int ...

  2. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...

  3. Effective Java P2 Item1 Consider static factory methods instead of constructors

    获得一个类的实例的传统方法是公共的构造方法,还可以提供一个公共的静态工厂方法(一个返回值为该类实例的简单静态方法), 例如Boolean(boolean 的封装类) public static Boo ...

  4. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  5. Effective Java 第三版笔记(目录)

    <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时 ...

  6. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  7. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  8. Effective Java —— 用静态工厂方法代替构造器

    本文参考 本篇文章参考自<Effective Java>第三版第一条"Consider static factory methods instead of constructor ...

  9. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

随机推荐

  1. [Matlab] Galois Field

    1. gf(M)用来把M转换到伽罗华域 2. gf(M.x)用来把M转换到实数域

  2. 移动端前端常见的触摸相关事件touch、tap、swipe等整理

    前端的很多事件在PC端和浏览器端可公用,但有些事件却只在移动端产生,如触摸相关的事件 本文整理了移动端常见的一些事件,包括原生支持的click.touch.tap.swipe事件,也有定义型的gest ...

  3. 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!

    Solve the puzzle, Save the world! Problem Description In the popular TV series Heroes, there is a ta ...

  4. FreeBSD 9.1安装KMS 这是一个伪命题###### ,9....

    FreeBSD 9.1安装KMS 这是一个伪命题###### ,9.1的内核已经加入了KMS内核支持 需要更新ports中的xorg到打了补丁的版本,无意中发现了一个pkg源,这个事也搞定了 free ...

  5. scons学习

    http://blog.csdn.net/andyelvis/article/category/948141 http://www.scons.org/doc/production/HTML/scon ...

  6. 【转载】delete table 和 truncate table 的区别

    使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where] 将XS表中的所有行数据删除 delete XS 执行完后,发现X ...

  7. jquery 回到 顶部

    1. 页面内容较多, 从底部超链接 点击回到页面顶部 // 回到顶部 var $top = $('<a class="doc-gotop" href="javasc ...

  8. 最小化安装centos7下配置网络

    虚拟机操作系统:centos7.0 命令行模式 1.首先明确centos7在最小化安装完是不支持上网的,相应的查看网络以及修改网络参数是不能使用的,最常见的就是我们常用的ifconfig. 2.找到网 ...

  9. quartz使用(一)

    在项目中经常会碰到定时任务,quartz是一款非常优秀的开源框架, 提供了定时任务的支持,还支持任务的持久化,并且提供了对数据库的支持.下面首先对quartz做一个简单介绍,并附上一个小例子. 1.下 ...

  10. 股票价格涨跌预测—基于KNN分类器

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...