博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective Java - Creating and Destroying Objects (1)
阅读量:4691 次
发布时间:2019-06-09

本文共 959 字,大约阅读时间需要 3 分钟。

Static Factory Methods v.s. Constructor

Advantages to use Static Factory Methods

1. More descriptive - the name of the factory methods can explain more than a constructor

  e.g.  v.s. 

2. Can have multiple static factory methods for separate purpose

3. Static factory methods can return existing instance instead of generate a new one everytime when it's invoked

  - Good for immutable, singleton, uninstantiable

  - Improve performance

  e.g. 

4. Static factory method can return any subtype of their return type

  - an API can return objects without making their classes public (can only make the interface public)

5. Static factory method can help guess type parameter

      e.g. Map<String, List<String>> m = HashMap.newInstance();

 

Disadvantages when only provide static factory method

1. Classes without Public/Protected constructor cannot be subclassed.

2. Not readily distinguishable from other static methods

转载于:https://www.cnblogs.com/joycelee/p/7625508.html

你可能感兴趣的文章
拓扑排序
查看>>
NYOJ--32--SEARCH--组合数
查看>>
JMS
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>
pdo
查看>>
21世纪经济网APP
查看>>
解决NetworkOnMainThreadException
查看>>
1039 到底买不买
查看>>
农银电商项目学习笔记(一)
查看>>
MockObject
查看>>
Chukwa
查看>>
(转)Maven仓库——私服介绍
查看>>
设计模式之工厂模式
查看>>
仿复制粘贴功能,长按弹出tips的实现
查看>>
Docker第三方项目小结
查看>>