effective_java
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| effective_java [2020/05/07 12:01] – [Item 1: Consider static factory methods instead of contructors] kalaschnikow | effective_java [2020/05/13 16:33] (current) – [Item 2: Consider a builder when faced with many contructor parameters] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Effective Java ====== | ||
| + | By Joshua Bloch | ||
| + | |||
| + | ===== Chapter 1 Introduction ===== | ||
| + | |||
| + | ... | ||
| + | |||
| + | ===== Chapter 2 Creating and Destroying Objects ===== | ||
| + | |||
| + | ==== Item 1: Consider static factory methods instead of contructors ==== | ||
| + | |||
| + | * Static factory method : static method that returns an instance of a class. | ||
| + | |||
| + | Advantages of static factory methods over constructors: | ||
| + | - static methods have names | ||
| + | - static methods must not create a **new** object (example: '' | ||
| + | * // | ||
| + | - static methods can return instances of any subtype of their return type | ||
| + | * possibility to hide subtypes from public -> // | ||
| + | - class of the returned object can vary from call to call as a function of the input parameters | ||
| + | - class of the returned object need not exist when the class containing the method is written | ||
| + | * //service provider frameworks// | ||
| + | * -> // | ||
| + | |||
| + | Limitations/ | ||
| + | - the main limitation of providing only static factory methods is that classes without public or protected constructors cannot be subclassed | ||
| + | - hard for programmers to find | ||
| + | * to " | ||
| + | * **from** - makes object from one parameter, e.g. '' | ||
| + | * **of** - makes " | ||
| + | * **valueOf** - more verbose alternative to '' | ||
| + | * **instance** or **getInstance** - returns maybe not new instance | ||
| + | * **create** or **newInstance** - returns new instance | ||
| + | * **get// | ||
| + | * **new// | ||
| + | * **// | ||
| + | |||
| + | ==== Item 2: Consider a builder when faced with many contructor parameters ==== | ||
| + | |||
| + | Problem: many fields where some of them are not always necessary. | ||
| + | |||
| + | * // | ||
| + | * Disadvantages | ||
| + | * telescoping contructor works but it is hard to write client code, and harder to read it. | ||
| + | |||
| + | * // | ||
| + | * Disadvantages | ||
| + | * construction is split across multiple calls, so the bean can be in inconsistent state partway through it construction | ||
| + | * JavaBeans precludes the possibility to make a class immutable | ||
| + | |||
| + | Solution: Builder pattern | ||
| + | * Don't create the object directly, but with the help of a builder object (typically '' | ||
| + | |||
| + | <code java> | ||
| + | NutritionFacts cocaCola = new NutritionFacts.Builder(240, | ||
| + | </ | ||
| + | |||
| + | * The Builder pattern is well suited to class hierarchies. | ||
| + | |||
| + | * //Covariant return typing// - subclass method is declared to return a subtype of the return type declared in the superclass | ||
| + | * allows clients to use these builder without the need for casting | ||
| + | |||
| + | Disadvantages of Builder: | ||
| + | * more verbose than telescoping constructor | ||
| + | * so it should be used if we really have more than a handful of parameters | ||
| + | |||
| + | ==== Item 3: Enforce the singleton property with a private constructor or an enum type ==== | ||
| + | |||
| + | ... | ||
| + | ==== Item 11: Always override hashCode when you override equals ==== | ||
| + | ... | ||
