Java Design Pattern: Interpreter

Major revision in progress … Please come back later. Interpreter pattern is used when some context needs to be interpreted. The following example is a very simple Interpreter implementation. It interpretes letter “a” and “b” to “1” and “2”. Class Diagram Note that: the dependency is also shown in diagram to make the structure more … Read more

Java Design Pattern: Iterator

Iterator pattern is used to iterate through a collection of objects. It is a commonly used pattern, you probably have used it before. Whenever you sees something like hasNext() and next(), it is probably a iterator pattern. For example, you may iterate through a list of database query record. Iterator Pattern Class Diagram Iterator Pattern … Read more

Java Design Pattern: Flyweight

Flyweight pattern is used for minimizing memory usage. What it does is sharing as much data as possible with other similar objects. 1. Flyweight Pattern Class Diagram 2. Flyweight Pattern Java Code // Flyweight object interface interface ICoffee { public void serveCoffee(CoffeeContext context); }// Flyweight object interface interface ICoffee { public void serveCoffee(CoffeeContext context); } … Read more

Java Design Pattern: Prototype

Prototype design pattern is used when very similar objects are frequently created. Prototype pattern clones objects and set the changed feature. In this way, less resources are consumed. 1. Prototype Pattern Class Diagram 2. Prototype Pattern Java Example package designpatterns.prototype;   //prototype interface Prototype { void setSize(int x); void printSize(); }   // a concrete … Read more

Java Design Pattern: Builder

The key feature of Builder pattern is that it involves a step-by-step process to build something, i.e., every produce will follow the same process even though each step is different. In the following example, we can define a drink builder called StarbucksBuilder which will build a Starbucks drink. StarbucksBuilder has several steps to build a … Read more

Java Design Pattern: Mediator

Mediator design pattern is used to collaborate a set of colleagues. Those colleagues do not communicate with each other directly, but through the mediator. In the example below, Colleague A want to talk, and Colleague B wants to fight. When they do some action(i.e., doSomething()), they invoke mediator to do that. Mediator Class Diagram Mediator … Read more

Java Design Pattern: Command

Command design pattern takes an operation and its arguments and wraps them up in an object to be executed, logged, etc. In the example below, Command is an operation, its argument is a Computer, and they are wrapped in Switch. In another perspective, Command pattern has 4 parts: command, receiver, invoker and client. In this … Read more

Java Design Pattern: Composite

Composite pattern is relatively simple, but it has been used in many designs, such as SWT, eclipse workspace, etc. It basically produce a hierarchical tree which can be accessed by using a uniform method. Class Diagram The following code implements the following tree structure. Java Code import java.util.List; import java.util.ArrayList;   //Component interface Component { … Read more

Java Design Pattern: Abstract Factory

Abstract Factory pattern adds another layer of abstraction for Factory pattern. If we compare Abstract Factory with Factory, it is pretty obvious that a new layer of abstraction is added. Abstract Factory is a super-factory which creates other factories. We can call it “Factory of factories”. Abstract Factory class diagram Abstract Factory Java code interface … Read more

Java Design Pattern: Factory

1. The story for Factory pattern Factory design pattern is used for creating an object based on different parameters. The example below is about creating human in a factory. If we ask the factory for a boy, the factory will produce a boy; if we ask for a girl, the factory will produce a girl. … Read more

Eclipse Design Patterns – Strategy in SWT

Design patterns used in SWT is relatively straightforward. SWT is an independent module in Eclipse platform. In brief, Strategy let client dynamically set the strategy it should use. Strategy is different from State. First of all, Strategy is simpler since it is only about using interface instead of concrete class. Secondly, State involves changing the … Read more