Category Archives: design pattern

Design Pattern summary by tech lead.

link 1. @3:00 Break programs into logically independent components, allowing component dependencies where necessary. 2. @3:10 Inspect your data objects and graph their interactions/where they flow. Keep classes simple. Keep one-way dataflow/interactions simple. @3:29 Simplicity — keep a small number of class types (controller, views, data objects) avoid creating classes that aliases those classes (managers,… Read More »

SRP

SRP, if a class is doing too much, and can’t tell what is its responsibility. Maybe it’s time for code refactoring and break down to more isolated components.

Singleton

Singleton with public final field Problem of this is that this can create another instance by reflection. The fix is that in private method, add a INSTANCE check. public class SingletonWithPublicFinalField { public static final SingletonWithPublicFinalField INSTANCE = new SingletonWithPublicFinalField(); private SingletonWithPublicFinalField() { // if (INSTANCE != null) { // throw new RuntimeException(“Singleton instance already… Read More »

Anti Pattern – Javabean Pattern

Call a non-argument constructor, then call a series of set method. Avoid using it. class Person { String firstName; String lastName; int age; public Person() {} public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName… Read More »

Anti Pattern: Telescoping constructor pattern

It lists all constructors. This is an anti pattern. Avoid using it. class Person { String firstName; String lastName; int age; public Person() {} public Person(String firstName) { this.firstName = firstName; } public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public Person(String firstName, String lastName, int age) { this.firstName =… Read More »

static factory pattern

static factory method over constructors: 1. Constructor don’t have meaningful names, but static factory method has 2. Static factory method returns same type, subtype, primitives 3. Static factory could return same instance if needed Sample of static factory method: String value1 = String.valueOf(1); String value2 = String.valueOf(1.0L); Optional<String> value1 = Optional.empty(); Arrays.asList(1, 2, 3); link… Read More »