Design patterns in brief

29
Design Patterns Don’t reinvent the wheel Hanoi - April, 2009 Duong Trong Tan, ([email protected])

description

This presentation shows some basics of design patterns and explanation of some common used design patterns such as Singleton, Observer, Command, etc.

Transcript of Design patterns in brief

Page 1: Design patterns in brief

Design PatternsDon’t reinvent the wheel

Hanoi - April, 2009Duong Trong Tan, ([email protected])

Page 2: Design patterns in brief

What are design patterns?

They› are common ways of structuring programs› typically deal with a small number of

classes, but can be of any size› provide a communication framework in

which ideas can be discussed at a high level

› can specify how objects are created, how they interact, or how they are structured

Page 3: Design patterns in brief

Why design patterns?

Reuse solutions Vocabulary for development Better software

Page 4: Design patterns in brief

Who?

Alexandre Christopher ‘Gang of Four’: Erich Gamma, Richard

Helm, Ralph Johnson, and John M. Vlissides

Page 5: Design patterns in brief

What?

Gang of Four common design patterns Some new J2EE design patterns

Page 6: Design patterns in brief

How can design patterns be derived?

They do use the Design Principles

Programming to interfaces not implementations Favor composition over inheritance Encapsulate what varies Strive for loosely coupled between objects that interact Classes should OPEN for extension and CLOSE for

modification Depends on abstraction, not concrete classes Don't call us, we'll call you A class should have only one reason to change

Page 7: Design patterns in brief

Com

mon

patte

rns

Desig

n Pa

ttern

sC

ate

gorie

s

Singleton Builder Factory method Facade Decorator Observer Command Strategy

Creational: How to create objects (higher level than constructors)

Behavioral: Describing how objects interact

Structural: Common relationships in class diagrams

C

BS

Page 8: Design patterns in brief
Page 9: Design patterns in brief

Singleton

public static Counter getInstance() { if(uInstance == null) { this.uInstance = new Counter(); return this.uInstance; } else { return this.uInstance; }}

C

Page 10: Design patterns in brief

The purpose of Singleton

› Makes sure only one instance of an object exists within an application.

› A better solution compared to global variables

› Provide global access to the object instance.

› Example: Counter, Application Menu System, Application objects, Services, Clients

› Not as easy as you think: What about threading?

Page 11: Design patterns in brief

Factory Method

Defines an interface for creating an object but lets subclasses decide which class to instantiate.

Lets classes defer instantiation to subclasses

C

Page 12: Design patterns in brief

Benefits of Factories

We can choose what to instantiate at runtime

We can code to an interface› Don’t have to know exactly what kind of

object we’ll get back We centralize code

› In order to add new types of objects, we only have to modify one place, the factory

12

Page 13: Design patterns in brief

Builder C

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Page 14: Design patterns in brief

Façade

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

S

Page 15: Design patterns in brief

The DBFacade Example Common JDBC uses:

› Open a connection to DB

› Close a connection› Get all tables from

DB› Executes a SQL

statement› Retrieve result from

a SELECT statementAll That I Need

Page 16: Design patterns in brief

DatabaseFacade Outlook

JDBC APIDatabaseFacade

Clientuses

Connection

Statement

ResultSet

DatabaseMetadata

ResultSetMetadata

Driver…

Page 17: Design patterns in brief

Decorator

BufferedReader in = new BufferedReader(new FileReader(“foo.in"));

S

Page 18: Design patterns in brief

Strategy B

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it

Page 19: Design patterns in brief

Observer

GoF

B

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Page 20: Design patterns in brief

MVC

What does it hire?

Page 21: Design patterns in brief

Command B

“Let’s hide (encapsulate) the way we call (invoke) methods.”

Page 22: Design patterns in brief

Example: Menus

Consider an application menu system. How do we de-couple our menu system from our document class?

Command Interface

Client

We can do a lot of things (actions) with a document. Each interface might be different!

Receiver

ConcreteCommandMenu contains many menu items.

Composite Pattern!

invoker

Page 23: Design patterns in brief

AllGoF patterns

Page 24: Design patterns in brief

J2EE patterns

Categorized by tiers: Presentation Tier Business Tier Integration Tier

Page 25: Design patterns in brief

Data Access Object

Alur et al.

Page 26: Design patterns in brief

Business Objects

Alur et al.

Page 27: Design patterns in brief

Session Façade

Alur et al.

Page 28: Design patterns in brief

Application Service

Alur et al.

Page 29: Design patterns in brief

References

E. Gamma, R. Helm, R. Johnson, & J. Vlissides. Design Patterns: Elements of Object-Oriented Software. Addison-Wesley, Boston, 1995.

D. Alur, J. Crupi and D. Malks, Core J2EE Patterns: Best Practices and Design Strategies, Second Edition, 2003