zoukankan      html  css  js  c++  java
  • Java高效编程(2) -- Creating and Destroying Objects

    Item 1: Consider static factory methods instead of constructors

    Advantage:

    One advantage of static factory methods is that, unlike constructors, they have names.

    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.

    A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.

    A fourth advantage of static factory methods is that they reduce the verbosity(冗长) of creating parameterized type instances.

    Disadvantage:

    The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

    A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.

    Item 2: Consider a builder when faced with many constructor parameters

    the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.

    a JavaBean may be in an inconsistent state partway through its construction.

    the JavaBeans pattern precludes the possibility of making a class immutable.

    Advantage:

    The Builder pattern simulates named optional parameters.

    Class.newInstance breaks compile-time exception checking.

    the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

    Item 3: Enforce the singleton property with a private constructor or an enum type

    Making a class a singleton can make it difficult to test its clients.

    a single-element enum type is the best way to implement a singleton.

    #1. Singleton with public final field

    public class Elvis {
        public static final Elvis INSTANCE = new Elvis();
        private Elvis() { ... }
        public void leaveTheBuilding() { ... }
    }

    #2. Singleton with static factory

    public class Elvis {
        private static final Elvis INSTANCE = new Elvis();
        private Elvis() { ... }
        public static Elvis getInstance() { return INSTANCE; }
        public void leaveTheBuilding() { ... }
    }

    #3. readResolve method to preserve singleton property

    private Object readResolve() {
        // Return the one true Elvis and let the garbage collector
        // take care of the Elvis impersonator.
        return INSTANCE;
    }

    #4. Enum singleton - the preferred approach

    public enum Elvis {
        INSTANCE;
        public void leaveTheBuilding() { ... }
    }

    Item 4: Enforce noninstantiability with a private constructor

    Attempting to enforce noninstantiability by making a class abstract does not work.

    a class can be made noninstantiable by including a private constructor.

    public class UtilityClass {
        // Suppress default constructor for noninstantiability
        private UtilityClass() {
            throw new AssertionError();
        }
        ... // Remainder omitted
    }    
  • 相关阅读:
    oracle 各个版本下载地址
    学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记
    mysql5.7安装教程图解
    myeclipse2017 安装包及破解插件的下载
    MyEclipse 2017 ci6 安装反编译插件(本人自己摸索的方法,亲测可行)
    Myeclipse10.7安装git插件并将Java项目上传到码云(github)
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    IntelliJ IDEA使用教程
    IntelliJ idea 中使用Git
    IntelliJ Idea 集成svn 和使用
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3945415.html
Copyright © 2011-2022 走看看