`

Effective Java notes-Creating and Destroying Objects(1)

    博客分类:
  • Java
阅读更多

Item 1 Consider static factory method instead of constructors

 

Advantages:

  1. One advantage of static factory methods is that, unlike constructors, they have names. (Mostly used in Java.utils.Collections)
  2. A second advantage of static factory methods is that, unlike constructors,they are not required to create a new object each time they're invoked.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. (See service provider framework , eg : The JDBC Driver Manager)
  4. A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.(可以通过为具有泛型参数的类提供static factory method来消除实例化该类时的泛型参数)

 

Disadvantages:

  1. The main disadvantage of providing only static factory method is that classes without public or protected constructors cannot be subcalssed. (See Collections Framework, encourages progammers to use composition instead of inheritance)
  2. A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods. (In my point, this issue could be ignored, just seeking for an appropriate name for the method is ok)

 Key points of item1:

  • The static factory methods descriped in item1 has no direct equivalent in Design Patterns.
  • Avoid the reflex to provide public constructors without first considering static factories.
分享到:
评论

相关推荐

    CSharp - Module 9_Creating and Destroying Objects

    在IT领域,尤其是在编程语言的学习与应用中,C#(C Sharp)作为一款由微软开发的面向对象的、类型安全的、垃圾回收的程序设计语言,其在创建和销毁对象方面的处理方式是开发者必须掌握的核心技能之一。...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    2 Creating and Destroying Objects Item 1: Consider static factory methods instead of constructors Item 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton ...

    KafkaOffsetMonitor监控工具2017年1月发布的版本

    Instead, closing and destroying the client and re-creating it. Sleeping on error before re-creating client and continuing to process Deal with thread-safety issues on shared memory between threads ...

    微软.NET程序员高级培训教程系列资料.C#(下)【英文版】

    7. **创建与销毁对象(Creating and Destroying Objects)**:"CSharp_Module 9_Creating and Destroying Objects.pdf"将涵盖对象的生命周期,包括构造函数、析构函数和垃圾回收机制,以及何时和如何适当地分配和释放...

    JAVA小实验雷电(打飞机)

    1 You will find more useful informations in the following lessons: “Java GUI”, “Java Graphics”, “Applet and Multimedia”. The lesson ‘multithreading ’ maybe is useful to some advanced ...

    Unreal Engine 4.X By Example

    - **Trigger Volumes and Destroying Actors**: Detecting when actors should be removed. - **Debugging Our Blueprints**: Identifying and fixing issues. - **Masking Our Destruction with Particles**: ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part1

    Q520547 - TcxVirtualVerticalGrid - Possible exception in the TcxvgPainter.DrawRow method when destroying and recreating rows within the BeginUpdate/EndUpdate block Q525420 - The Styles....

    Msdn Training - Microsoft Visual Studio Net (With Source)

    模块9:《C# - Module 9_Creating and Destroying Objects.pdf》 本模块讲解了对象的生命周期,包括构造函数和析构函数的使用,以及垃圾回收机制。学习者将了解如何在适当的时间创建和释放对象,以优化内存管理。 ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part2

    Q520547 - TcxVirtualVerticalGrid - Possible exception in the TcxvgPainter.DrawRow method when destroying and recreating rows within the BeginUpdate/EndUpdate block Q525420 - The Styles....

    C# 微软基础教程PPT

    7. **Creating and Destroying Objects**:学习如何创建和销毁对象,理解对象生命周期和垃圾回收机制。 8. **Inheritance in C#**:子类继承父类的属性和方法,实现代码重用和扩展。 9. **Aggregation, Namespaces...

    convmv-1.15.tar.gz

    If this is not done, JFS does not behave like a POSIX filesystem and it might happen that certain files cannot be created at all, for example filenames in ISO-8859-1 encoding. Only when ...

    ns原版手册.pdf

    接着,3.4节“Creating and Destroying Tcl Objects”介绍了如何创建和销毁Tcl对象,3.4.2节“Variable Bindings”讲解了变量绑定,3.4.3节“Variable Tracing”讲述了变量追踪,3.4.4节“command Methods: ...

    超酷的UNIX IPC 解析与代码例子 - Beej Unix IPC Guide!

    - **Creating the segment and connecting**:介绍如何创建共享内存段并连接到现有段。 - **Attach me—getting a pointer to the segment**:演示如何获取指向共享内存段的指针。 - **Reading and Writing**:解释...

    Java版电子宠物游戏代码

    Java版电子宠物游戏代码,package Petgame; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Petgame extends MIDlet { private static Petgame instance; private ...

    TCL TK 语言8.5编程指导

    Chapter 12: Creating and Managing Menus 169 Introduction 169 Creating a menu 170 Adding menu buttons 175 Displaying a pop-up menu 178 Data entry application 180 Chapter 13: Creating the Address Book ...

    TMS Pack for FireMonkey2.3.0.1

    Improved : block refreshing columns when destroying the component in TTMSFMXSpinner Fixed : Small issue in HTML Rendering engine for anchor rendering in TTMSFMXHTMLEngine Fixed : Issue with ...

    20190201版PythonForDelphi含例程源码.zip

    PythonForDelphi 修改 最后修改日期2019-2-1这一版,需要注意的是python 3.7 要安装32位版 需要在Delphi中先安装上Python...Demo34 Dynamically creating, destroying and recreating PythonEngine. Uses PytonVersions

    U盘量产工具

    - Option for post image verification for both creating from and writing from usb drives. Previously, writing to drives always was verified. Verification may double the imaging time. - Each image ...

    推选文档Java小程序PPT.ppt

    Applet的生命周期由四个主要状态组成:初始化(Initialization)、加载(Loading)、启动(Starting)、绘画(Painting)、停止(Stopping)和销毁(Destroying)。这些状态反映了Applet在浏览器中从创建到结束的...

    C++语言程序设计第三版试卷

    - Destroying..1 - Destroying..0 程序首先创建了一个Samp类型的动态数组,然后逐个设置元素值并输出,最后释放内存。析构函数在每个对象被删除时执行,显示其内部存储的i的值。 10. Vector类是一个动态数组的...

Global site tag (gtag.js) - Google Analytics