Top Banner
Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson , and John Vlissides (The Gang of Four ).
19

Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Design Patterns

Elements of Reusable Object-Oriented Software

by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (The Gang of Four).

               

Page 2: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Abstract Factory

Page 3: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Adapter

Page 4: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Bridge

Page 5: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Command

Page 6: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Composite

Page 7: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Façade

Page 8: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Observer

Page 9: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Proxy

Page 10: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Strategy

Page 11: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Visitor Pattern

Page 12: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Visitor Intent

• Given an existing structure such as a tree or graph whose nodes are objects from different classes

• Add new behavior to the system• Without adding new operations to the classes• By encapsulating the new behavior in visitor

objects• Which traverse the structure performing tasks

when they visit the nodes

Page 13: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Parse Tree Visitors

• A source programming file is parsed

• A parse tree is constructed

• Its nodes represents represent productions in the grammar (~100 different classes)

• We need to add compiler functionality:– Checking, code generation, optimization

• Without modifying the node classes

Page 14: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Sample Parse Tree

Page 15: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

The Solution

• Add operations to the parse tree nodes

• To support a depth-first traversal of the tree

• By an abstract Visitor object.

• The Visitor has before and after methods for each kind of node.

• Class visitor{void before(expr e){}; void after(expr e){}

Page 16: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Double Dispatch

• The traversal code calls the Visitor’s before method when the visitor arrives at a node, its after method before it leaves the node.

• Concrete visitors do specific tasks such as checking and code generation

• The specific before and after methods called depend on the type of the concrete visitor and the type of the node. (double dispatch)

Page 17: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

Visiting a Train

• <Train> -> <Engine> {<Car>}* abstract class Visitor{ public Visitor(){}

public void before(Train host){}

public void after(Train host){}

public void before(Engine host){}

public void after(Engine host){}

public void before(Car host){}

public void after(Car host){}

}

Page 18: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

A visit method in the Train class

public void visit(Visitor v){ v.before(this);

if (engine!= null) engine.visit(v);

Enumeration enumCars = getCars().elements();

while(enumCars.hasMoreElements()){

Car it = (Car) enumCars.nextElement();

it.visit(v); }

v.after(this);

}

Page 19: Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.

When to use the Visitor Pattern

• When an object structure contains many classes of objects with differing interfaces and you want to perform operations on these objects that depend on their concrete classes.

• You want to encapsulate functionality in a single concrete visitor