-
Compound Pattern
State Pattern Definition The compound pattern is that combines at least two patterns into a solution that solves a recurring or general problem. For example, Model–view–controller (MVC) pattern for implementing user interfaces on computers. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user. Components A typical collaboration of the MVC components. The model...
-
State & Proxy Pattern
State Pattern Definition The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern’s superclass. Implementation State Interface & Concrete class public interface State { void insertQuarter(); void ejectQuarter(); void turnCrank(); void dispense(); }...
-
RetryUtils with Java 8. How to retry block of codes when exception occurs with functional way?
Basic There are already many ways exist to retry logics when exception occurs. For example, you can use framework like @retry with Spring. I just want to make a simple util to retry logic with java 8 like below. RetryUtils @Slf4j public class RetryUtils { private static final int RETRY = 3; private static final long DELAY = 1000l; @FunctionalInterface public interface RunnableWithException { void run() throws Exception; } public static <V> V retry(Callable<V> callable,...
-
Iterator & Composite Pattern
Iterator Pattern Definition The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. Implementation Base object public interface Menu { public Iterator<MenuItem> createIterator(); } public class MenuItem { String name; String description; boolean vegetarian; double price; public MenuItem(String name, String description, boolean vegetarian, double...
-
Template Method Pattern
Template Method Pattern Definition The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm’s structure. Template method’s abstract class may also define hook methods that may be overridden by subclasses. Implementation Template Method Abstract Class public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup();...
-
Adapter & Facade Pattern
Adapter Pattern Definition The adapter pattern is a software design pattern (also known as Wrapper, an alternative naming shared with the Decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code. Both Adapter pattern and Decorator pattern is usually called as Wrapper. But the purpose is basically different. At decorator pattern, we usually...
-
Command Pattern
Definition Command Pattern The command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Command A command object knows about receiver and invokes a method of the receiver. Values for parameters of the receiver method are stored...
-
Singleton Pattern
Definition Singleton Pattern The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton. Implementation Classical Example...
-
Factory Pattern
Definition Factory Method Pattern The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor....
-
Decorator Pattern
Definition The Decorator pattern (also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. OCP (Open-Closed Principle)...
-
Dynamic Extension with Intersection Type & Lambda
This is based on Toby Lee’s LiveCoding tv. https://www.youtube.com/watch?v=PQ58n0hk7DI FunctionalInterface Interface that has only one abstract method. private static void hello1(Function p0) { } public static void main(String[] args) { hello1(s -> s); } Old way, it normally created with anonymous class. At java8, lambda changes a lot of things! public static void anonymousClassExample() { Function<String, String> f = new Function<String, String>() { @Override public String apply(String s) { return null; } }; hello1(f); }...
-
Java Generics
Generics This is based on Toby Lee’s LiveCoding tv. https://www.youtube.com/watch?v=PQ58n0hk7DI static <T> void method1(List<T> list) { } static void unboundTypeWildCard(List<?> list) { // only allow add null list.add(null); // allow list's feature list.size(); list.clear(); Iterator<?> it = list.iterator(); } when we use generic methods with T, then we have intention to use type T, at below case, we just need to use List’s methods, so, ? is more suitable than T static <T> boolean isEmptyT(List<T>...
-
JUnit5 Summary
Overview Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit Platform : The Foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. It also defines the TestEngine API for developing a testing framework that runs on the platform. JUnit Jupiter...
-
Observer Pattern
Definition The Observer pattern is a software design pattern in which an object, called the Subject, maintains a list of its dependent object, called Observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. Loose Coupling Loose Coupling means that they interact each other, but they don’t know each other well. It is loosely coupled between Subject and Observer....