A spreadsheet object, bar chart object, and pie chart object can depict information in the same application data object by using different presentations.
When the user changes the information in the spreadsheet, the bar chart reflects the changes immediately, and vice versa.
Both a spreadsheet object and bar chart object can depict information in the same application data object by using different presentations.
Decorator Pattern
Design Aspect
Responsibilities of an object without subclassing
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Problem
A class will be modified if you want to attach additional responsibilities(decorators) to an object dynamically.
One way to add responsibilities is with inheritance.
A client is difficult to control how and when to decorate the component with a object.
Graph
Steps
Step 1.
1
2
3
4
// Shape.java
public interface Shape{
void draw();
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Rectangle.java
public class Rectangle implement Shape {
@Override
public void draw(){
System.out.println("Shape: Rectangle");
}
}
// Circle.java
public class Circle implement Shape{
@Override
public void draw(){
System.out.println("Shape: Circle");
}
}
Step 3.
1
2
3
4
5
6
7
8
9
10
11
12
// ShapeDecorator.java
public abstract class ShapeDecorator implements Shape{
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
Step 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// RedShapeDecorator.java
public class RedShapeDecorator extends ShapeDecorator{
public RedShapeDecorator(Shape decoratedShape){
super(decoratedShape);
}
@Override
public void draw(){
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}
Step 5.
1
2
3
4
5
6
7
8
9
10
11
12
// DecoratorPatternDemo.java
public class DecoratorPatternDemo{
public static void main(String[] args){
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System.out.println("\nCircle of red border");
redCircle.draw();
System.out.println("\nRectangle of red border");
redRectangle.draw();
}
}
Example Statement
In FileViewer,
We have a TextView object that displays text in a window.
TextView has no scroll bars by default, because we might not always need them.
We can also add a thick black border around the TextView.
It is highly likely that we will support various file formats for display in the future.
Starbuzz Coffee,
Starbuzz Coffee shops are scrambling to update their ordering systems to match their beverage offerings (e.g. HouseBlend, DarkRoast, Decaf and Espresso) to summate how they cost.
In addition to your coffee, you can also ask for several condiments like steamed milk, soy, and mocha, and have these, so they really need to get them built into their order system
Factory Method Pattern
Design Aspect
Subclass of object that is instantiated
Define an interface for creating an object, but let subclasses decide which class to instantiate. Facotry Method lets a class defer instantiation to subclasses.
Problem
As the objects being created changes over time, we need to modify the code of the creator object for the creations over and over again.
public class MotifWidgetFactory implements WidgetFactory{
public Window createWindow(){
return new MotifWindow();
}
public Scrollbar createScrollbar(){
return new MotifScrollbar();
}
public Button createButton(){
return new MotifButton();
}
}
// PMWidgetFactory.java
public class PMWidgetFactory implements WidgetFactory{
public Window createWindow(){
return new PMWindow();
}
public Scrollbar createScrollbar(){
return new PMScrollbar();
}
public Button createButton(){
return new PMButton();
}
}
Step 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
// GUIApplication.java
public class GUIApplication{
public static void main(String[] args){
WidgetFactory motifWidgetFactory = new MotifWidgetFactory();
WidgetFactory pmWidgetFactory = new PMWidgetFactory();
motifWidgetFactory.createWindow().build();
motifWidgetFactory.createScrollbar().build();
motifWidgetFactory.createButton().build();
pmWidgetFactory.createWindow().build();
pmWidgetFactory.createScrollbar().build();
pmWidgetFactory.createButton().build();
}
}
Example Statement
A GUI Application with Multiple Styles
A GUI Application consists of some kinds of widgets like window, scroll bar, and button.
Each widget in the GUI application has two or more implementations according to different lookand-feel standards, such as Motif and Presentation Manager.
The GUI application can switch its look-and-feel style from one to another.
Pizza Store
In a pizza store system, two flavors of pizza are offered: Cheese Pizza and Pepperoni Pizza.
Each flavor of pizza can be categorized into two styles: New York Style and Chicago Style.
Different pizza style needs different dough and sauce:
NY Style: Thin Crust Dough, Marinara Sauce
Chicago Style: Thick Crust Dough, Plum Tomato Sauce
Iterator Pattern
Design Aspect
How an aggregate’s elements are accessed, traversed
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Problem
The method of accessing the elements of two aggregate objects with different representations will be modified if a new aggregate object with different representation is added.
Graph
Steps
Step 1.
1
2
3
4
5
6
7
8
9
10
// Iterator.java
public interface Iterator{
public boolean hasNext();
public Object next();
}
// Container.java
public interface Container{
public Iterator getIterator();
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// NameRepository.java
public class NameRepository implements Container{
public String name[] = {"Robert", "John", "Julie", "Lora"};
@Override
public Iterator getIterator(){
return new NameIterator();
}
private class NameIterator implements Iterator{
int index;
@Override
public boolean hasNext(){
if (index < names.length){
return true;
}
return false;
}
@Override
public Object next(){
if (this.hasNext()){
return names[index++];
}
return null;
}
}
}
Step 3.
1
2
3
4
5
6
7
8
9
10
// IteratorPatternDemo.java
public class IteratorPatternDemo{
public static void main(String[] args){
NameRepository nameRepository = new NameRepository();
for (Iterator iter = nameRepository.getIterator(); iter.hasNext();){
String name = (String)iter.next();
System.out.println("Name : " + name);
}
}
}
Example Statement
Print Out Items in Different Data Structures,
A List data structure is implemented with a String array which can contain a series of String objects.
We can access List by calling the get() method with an index, and know how many Strings inside the List with a public attribute: length.
Furthermore, another data structure called SkipList which consists of a series of SkipNodes.
Each SkipNode can be accessed by invoking the getNode() method in SkipList with an index. And we have the idea about the size of SkipList with its size() method.
Now we have to traverse both List and SkipList to print out those object items in the two different data structures for some purpose.
Merge Tow Menus
A waitress of Pancake House keeps a breakfast menu which uses an ArrayList to hold its menu items.
And a waitress of Diner keeps a lunch menu which uses an Array to hold its menu items.
Now, these two restaurants are merged and tend to provide service in one place, so a waitress should keep both menus on hand.
How to the waitress print two different menu representations at a time?
Composite Pattern
Design Aspect
Structure and composition of an object
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Problem
The user can group components to form larger components, which in turn can be grouped to form still larger components.
Graph
Steps
Step 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Employee.java
import java.util.ArrayList;
import java.util.List;
public class Employee{
private String name;
private String dept;
private int salary;
private List<Employee> subordinates;
public Employee(String name, String dept, int sal){
Employee CEO = new Employee("John", "CEO", 30000);
Employee headSales = new Employee("Robert", "Head Sales", 20000);
Employee headMarketing = new Employee("Micheal", "Head Marketing", 20000);
Employee clerk1 = new Employee("Laura", "Marketing", 10000);
Employee clerk2 = new Employee("Bob", "Marketing", 10000);
Employee salesExcutive1 = new Employee("Richard", "Sales", 10000);
Employee salesExcutive2 = new Employee("Rob", "Sales", 10000);
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExcutive1);
headSales.add(salesExcutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
System.out.println(CEO);
for (Employee headEmployee : CEO.getSubordinates()){
System.out.println(headEmployee);
for (Employee employee : headEmployee.getSubordinates()){
System.out.println(employee);
}
}
}
}
Example Statement
In schematic capture application,
There are some basic components can be drawn such as Text, Line, and Rectangle.
The user can group basic components to form larger components, which in turn can be grouped to form still larger components.
Merge two menus extends,
A dessert submenu is going to be added to the Diner menu.
Facade Pattern
Design Aspect
Interface to a subsystem
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Problem
A common design goal is to minimize the communication and dependencies between subsystems.
Graph
Steps
Step 1.
1
2
3
4
// Shape.java
public interface Shape{
void draw();
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Rectangle.java
pulic class Rectangle implements Shape{
@Override
public void draw(){
System.out.println("Rectangle::draw()");
}
}
// Circle.java
pulic class Circle implements Shape{
@Override
public void draw(){
System.out.println("Circle::draw()");
}
}
// Square.java
pulic class Square implements Shape{
@Override
public void draw(){
System.out.println("Square::draw()");
}
}
Step 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ShapeMaker.java
public class ShapeMaker{
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker(){
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
Step 4.
1
2
3
4
5
6
7
8
9
// FacadePatternDemo.java
public class FacadePatternDemo{
public static void main(String[] args){
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMakder.drawSquare();
}
}
Example Statement
A Programming Environment
A compiler subsystem contains classes such as Scanner, Parser, ProgramNode, and BytecodeStream.
The client classes need to use Scanner, Parser, ProgramNode, and BytecodeStream to compile some code.
Home Sweet Home Theater
A Home Theater consists of an amplifier, a DVD player, a projector, a screen, a popcorn popper, and theater lights.
A user can watch a movie through the following process:
Turn on the popcorn popper
Start the popper popping
Dim the lights
Put the screen down
Turn the projector on
Turn the sound amplifier on
Turn the DVD player on
Start the DVD player playing
Template Method Pattern
Design Aspect
Steps of an algorithm
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Templete Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
Problem
Two classes with code duplications would be modified at the same time if the duplicate code is being changed.
Graph
Steps
Step 1.
1
2
3
4
5
6
7
8
9
10
11
12
// Game.java
public abstract class Game{
abstract void initialize();
abstract void statePlay();
abstract void endPlay();
public final void play(){
initialize();
startPlay();
endPlay();
}
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Cricket.java
public class Cricket extends Game{
@Override
void endPlay(){
System.out.println("Cricket Game Finished!");
}
@Override
void initialize(){
System.out.println("Cricket Game Inisialized!");
}
@Override
void startPlay(){
System.out.println("Cricket Game Start!");
}
}
// Football.java
public class Football extends Game{
@Override
void endPlay(){
System.out.println("Football Game Finished!");
}
@Override
void initialize(){
System.out.println("Football Game Inisialized!");
}
@Override
void startPlay(){
System.out.println("Football Game Start!");
}
}
Step 3.
1
2
3
4
5
6
7
8
9
10
// TemplatePatternDemo.java
public class TemplatePatternDemo{
public static void main(String[] args){
Game game = new Cricket();
game.play();
System.out.println();
game = new Football();
game.play();
}
}
Example Statement
Opening Documents in Applications
In order to open a text document, a text application
will:
Check if the text document can be opened
Create a text document object and hold a reference of the text document object.
Add the text document object to the Application.
Read text document.
Opening Documents in Applications
There is another new requirement.
Opening a spreadsheet document with a spreadsheet application carries the same steps in the algorithm(process) as the text document.
Check if the spreadsheet document can be opened.
Create a spreadsheet document object and hold a reference of the spreadsheet document object.
Add the spreadsheet document object to the Application.
Read spreadsheet document.
Prepare Caffeine Beverages
Please follow these recipes precisely when preparing Starbuzz beverages
Starbuzz Coffee Recipe
Boil some water
Brew coffee in boiling water
Pour Coffee in cup
Add sugar and milk
Starbuzz Tea Recipe
Boil some water
Steep tea in boiling water
Pour tea in cup
Add lemon
Command Pattern
Design Aspect
When and how a request is fulfilled
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Problem
The invoker object is subject to be modified once the set of the actions on a receiver is changed.
private List<Order> orderList = new ArrayList<Order>();
public void takeOrder(Order order){
orderList.add(order);
}
public void placeOrders(){
for (Order order : orderList){
order.execute();
}
orderList.clear();
}
}
Step 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// CommandPatternDemo.java
public class CommandPatternDemo{
public static void main(String[] args){
Stock abcStock = new Stock();
BuyStockOrder buyStockOrder = new BuyStockOrder(abcStock);
SellStockOrder sellStockOrder = new SellStockOrder(abcStock);
Broker broker = new Broker();
broker.takeOrder(buyStockOrder);
broker.takeOrder(sellStockOrder);
broker.placeOrder();
}
}
Example Statement
Remote Control,
The remote control can control a stereo remotely.
While a stereo is switched on by the remote control, the CD and volume will be set at the same time.
Cut, Copy, Paste on a Document,
An editor application carries a document.
A menu in the editor application contains some menu items which performs three specific operations such as cut, copy, and paste on a document.
Adapter Pattern
Design Aspect
Interface to an object
Convert the interface of a class inter another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Problem
The request method of the requester object should be modified once it changes its receiver class with a new interface.
Graph
Steps
Step 1.
1
2
3
4
5
6
7
8
9
10
// MediaPlayer.java
public interface MediaPlayer{
public void play(String audioType, String fileName);
}
// AdvancedMediaPlayer.java
public interface AdvancedMediaPlayer{
public void playVlc(String fileName);
public void playMp4(String fileName);
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// VlcPlayer.java
public class VlcPlayer implements AdvancedMediaPlayer{
A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats.
The reader converts RTF documents into TeX text or into a text widget by recognizing different RTF tokens(Character, Font Change and Paragraph).
A Vacation Planner,
Patternsland wants to build a vacation planner.
A vacation planner can choose a hotel and various types of admission tickets, make restaurant reservations, even book special events and day. The difference types vacation (normal, backpacking) will have difference options.
State Pattern
Design Aspect
states of an object
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Problem
An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state.
A class TCPConnection that represents a network connection.
When a TCPConnection object receives an Open request from other objects, it responds differently depending on its current state. A TCPConnection object can be in one of several different states: Established, Listening, and Closed.
A Gumball Machine
A GumballMachine has four actions: Insert Quarter, Eject Quarter, Turn Crank, and Dispense.
There are four states in the GumballMachine: No Quarter, Has Quarter, Out of Gumballs and Gumball Sold. As the following state diagram.
Visitor Pattern
Design Aspect
Operations that can be applied to objects without changing their classes
Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Problem
The problem is that distributing all these operations across the various classes in an object structure leads to a system that’s hard to understand, maintain, and change. Moreover, adding a new operation usually requires recompiling all of these classes.
Graph
Steps
Step 1.
1
2
3
4
// ComputerPart.java
public interface ComputerPart{
public void accept(ComputerPartVisitor computerPartVisitor);
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Keyboard.java
public class Keyboard implements ComputerPart{
@Override
public void accept(ComputerPartVisitor computerPartVisitor){
computerPartVisitor.visit(this);
}
}
// Monitor.java
public class Monitor implements ComputerPart{
@Override
public void accept(ComputerPartVisitor computerPartVisitor){
computerPartVisitor.visit(this);
}
}
// Mouse.java
public class Mouse implements ComputerPart{
@Override
public void accept(ComputerPartVisitor computerPartVisitor){
computerPartVisitor.visit(this);
}
}
// Computer.java
public class Computer implements ComputerPart{
ComputerPart[] parts;
public Computer(){
parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()};
}
@Override
public void accept(ComputerPartVisitor computerPartVisitor){
for (int i = 0; i < parts.length; i++){
parts[i].accept(computerPartVisitor);
}
computerPartVisitor.visit(this);
}
}
Step 3.
1
2
3
4
5
6
7
// ComputerPartVisitor.java
public interface ComputerPartVisitor{
public void visit(Computer computer);
public void visit(Mouse mouse);
public void visit(Keyboard keyboard);
public void visit(Monitor monitor);
}
Step 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ComputerPartDisplayVisitor.java
public class ComputerPartDisplayVisitor implements ComputerPartVisitor{
There are several nodes in an abstract syntax tree (AST), such as VariableRefNode and AssignmentNode, which represent respective parts in source code and keep the code information.
Each node currently provides three interfaces for the compiler to use in order to check its type, generate code and print out the content.
Nutrition Retrieval from A Restaurant Menu,
The menu components of the Diner restaurant which comprises menu items and diner menus can be printed by a waitress.
Each diner menu consists of several menu items.
The Diner restaurant would like to provide calories, protein and carbs information for each menu item.
Equipment Power Consumption,
There are three types of equipment in the inventory, such as chassis, buses, and floppies. Among all the equipment, Chassis is composited of others.
Chassis provides an interface for the creation of an iterator, which iterates all the equipment in one chassis with next and hasNext operations.
Each equipment provides its power consumption and cost in addition.
Chassis provides a sum of power consumption or cost for all its components.
Singleton Pattern
Design Aspect
the sole instance of a class
Ensure a class only has one instance, and provide a global point of access to it
Problem
It’s important for some classes to have exactly one instance and ensure that the instance is easily accessible.
A global variable makes an object accessible, but it doesn’t keep you from instantiating multiple objects.
Graph
Steps
Step 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// SingleObject.java
public class SingleObject{
private static SingleObject instance = new SingleObject();
private SingleObject(){}
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
Step 2.
1
2
3
4
5
6
7
// SingletonPatternDemo.java
public class SingletonPatternDemo{
public static void main(String[] args){
SingleObject object = SingleObject.getInstance();
object.showMessage();
}
}
Example Statement
Chocolate Boiler,
A chocolate boiler is used to boil chocolate.
Before boiling chocolate with the boiler, you have to make sure that the boiler is now empty and then fill chocolate in. Besides, you can’t boil chocolate again while the chocolate has already been boiled.
After boiling, it is time to drain out the boiled chocolate and make the boiler empty again.
In order to prevent some unexpected situation, it is not allowed to have multiple instances of the chocolate boiler in the system.
FlyWeight Pattern
Design Aspect
Storage costs of objects
Use sharing to support large numbers of finegrained objects efficiently.
Problem
Some applications could benefit from using objects throughout their design, but a naive implementation would be prohibitively expensive.
Graph
Steps
Step 1.
1
2
3
4
// Shape.java
public interface Shape{
void draw();
}
Step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Circle.java
public class Circle implements Shape{
private String color;
private int x;
private int y;
private int radius;
public Circle(String color){
this.color = color;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setRadius(int radius){
this.radius = radius;
}
@Override
public void draw(){
System.out.println("Circle: draw() [ Color: " + color + ", x: " + x + ", y: " + y + ", radius: " + radius + "]");
}
}
Step 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ShapeFactory.java
import java.util.HashMap;
public class ShapeFactory{
private static final HashMap<String, Shape> circleMap = new HashMap();
public static Shape getCircle(String color){
Circle circle = (Circle)circleMap.get(color);
if (circle == null){
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("Creating circle of color: " + color);
}
return circle;
}
}
Step 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// FlyWeightPatternDemo.java
public class FlyWeightPatternDemo{
public static final String colors[] = {"Red", "Green", "Blue", "White", "Black"};
public static void main(Sting[] args){
for (int i = 0; i < 20; i++){
Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
A document editor uses objects to represent embedded elements like rows, columns and characters. The characters have X-Y locations, and they can draw themselves dynamically.