-
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();...