More on this book
Community
Kindle Notes & Highlights
“Spouse of me this night today manufactures the unusual meal in a home. You will join?”
This book addresses your third need: customary and effective usage.
Small is beautiful, but simple ain’t easy.
The most significant set of changes was the addition of generics, enum types, annotations, autoboxing, and the for-each loop in Java 5. A close second was the addition of the new concurrency library, java.util.concurrent, also released in Java 5.
The other big change in the platform is the widespread adoption of modern Integrated Development Environments (IDEs), such as Eclipse, IntelliJ IDEA, and NetBeans, and of static analysis tools, such as FindBugs.
I borrowed the format from Scott Meyers’s Effective C++, which consists of fifty items, each conveying one specific rule for improving your programs and designs.
I naturally think in terms of exported APIs (Application Programming Interfaces), and I encourage you to do likewise. Even if you aren’t developing reusable components, thinking in these terms tends to improve the quality of the software you write.
Extreme Programming. These methodologies emphasize writing the simplest program that could possibly work.
a focus on API design serves you well in the refactoring process. The fundamental goals of refactoring are the improvement of system structure and the avoidance of code duplication.
THIS book is designed to help you make effective use of the Java programming language and its fundamental libraries: java.lang, java.util, and java.io, and subpackages such as java.util.concurrent and java.util.function.
Clarity and simplicity are of paramount importance. The user of a component should never be surprised by its behavior. Components should be as small as possible but no smaller.
the term component refers to any reusable software element, from an individual method to a complex framework consisting of multiple packages.)
Code should be reused rather than copied. The dependencies between components should be kept to a minimum. Errors should be detected as soon as possible af...
This highlight has been truncated due to consecutive passage length restrictions.
The language supports four kinds of types: interfaces (including annotations), classes (including enums), arrays, and primitives. The first three are known as reference types.
The term exported API, or simply API, refers to the classes, interfaces, constructors, members, and serialized forms by which a programmer accesses a class, interface, or package.
In Java 9, a module system was added to the platform.
Item 1: Consider static factory methods instead of constructors
A class can provide a public static factory method, which is simply a static method that returns an instance of the class.
Flyweight pattern
Bridge pattern
builder object
A minor advantage of builders over constructors is that builders can have multiple varargs parameters because each parameter is specified in its own method.
The Builder pattern has disadvantages as well. In order to create an object, you must first create its builder. While the cost of creating this builder is unlikely to be noticeable in practice, it could be a problem in performance-critical situations.
the Builder pattern is more verbose than the telescoping constructor pattern, so it should be used only if there are enough parameters to make it worthwhile, say four or more.
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 often the best way to implement a singleton.
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
The AssertionError isn’t strictly required, but it provides insurance in case the constructor is accidentally invoked from within the class.
Item 5: Prefer dependency injection to hardwiring resources
Static utility classes and singletons are inappropriate for classes whose behavior is parameterized by an underlying resource.
pass the resource into the constructor when creating a new instance. This is one form of dependency injection: the dictionary is a dependency of the spell checker and is injected into the spell checker when it is created.
A useful variant of the pattern is to pass a resource factory to the constructor.
Factory Method
Although dependency injection greatly improves flexibility and testability, it can clutter up large projects, which typically contain thousands of dependencies. This clutter can be all but eliminated by using a dependency injection framework
Item 6: Avoid creating unnecessary objects
While String.matches is the easiest way to check if a string matches a regular expression, it’s not suitable for repeated use in performance-critical situations.
lazily initializing
As is often the case with lazy initialization, it would complicate the implementation with no measurable performance improvement
When an object is immutable, it is obvious it can be reused safely,
adapters [Gamma95], also known as views. An adapter is an object that delegates to a backing object, providing an alternative interface.
Another way to create unnecessary objects is autoboxing, which allows the programmer to mix primitive and boxed primitive types, boxing and unboxing automatically as needed.
Autoboxing blurs but does not erase the distinction between primitive and boxed primitive types.
prefer primitives to boxed primitives, and watch out for unintentional autoboxing.
avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight. The classic example of an object that does justify an object pool is a database connection.
Generally speaking, however, maintaining your own object pools clutters your code, increases memory footprint, and harms performance.
defensive copying.