+ All Categories
Home > Education > Introduction to design patterns

Introduction to design patterns

Date post: 21-Jan-2015
Category:
Upload: duong-tan
View: 701 times
Download: 1 times
Share this document with a friend
Description:
Org title: design patterns for desktop application development.This presentation take a very brief tour at GoF commonly used design patterns and discuss theirs principles, benefits and uses.
Popular Tags:
40
Design Patterns Desktop Application Development Hanoi - February, 2010 Duong Trong Tan, ([email protected]) for
Transcript
Page 1: Introduction to design patterns

Design Patterns

Desktop Application Development

Hanoi - February, 2010Duong Trong Tan, ([email protected])

for

Page 2: Introduction to design patterns

2

Contents

• Design Issues• Design principles• What is design pattern?• Patterns classification• Commonly used patterns• Case studies

Page 3: Introduction to design patterns

3

Notice

• This discussion involves lots of questions, not answer• I assume that you are good at core Java concepts

– and these following symbols:

A

Page 4: Introduction to design patterns

4

DESIGN ISSUES Design principles What is design pattern? Patterns classification Commonly used patterns Case studies

Page 5: Introduction to design patterns

5

What is design?

Page 6: Introduction to design patterns

6

What is a good Design?

Page 7: Introduction to design patterns

7

A constant: CHANGE!

Page 8: Introduction to design patterns

8

COUPLING | COHESION

Page 9: Introduction to design patterns

9

DESIGN PRINCIPLES Design issues

What is design pattern? Patterns classification Commonly used patterns Case studies

Page 10: Introduction to design patterns

10

Design Principles

• Encapsulate what varies• Favor composition over inheritance• Programming to interfaces not to implementations• 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

Freeman et al., 2004

Page 11: Introduction to design patterns

11

WHAT IS DESIGN PATTERN?

Design issues Design principles

Patterns classification Commonly used patterns Case studies

Page 12: Introduction to design patterns

12

WHAT ARE TEMPLATES?

Page 13: Introduction to design patterns

13

What is a design pattern?

• A design pattern – is a general reusable solution to recurring

problem• Patterns – can specify how objects are created, how they

interact, or how they are structured– typically deal with a small number of classes, but

can be of any size

Page 14: Introduction to design patterns

14

Why patterns?

• Provide a communication framework in which ideas can be discussed at a high level

• Understand current systems or APIs better• Guide you quickly find solutions• Better design: flexible, maintainable• Build sustainable products

Page 15: Introduction to design patterns

15

Brief history of design patterns

• Idea of “pattern” in civil architecture was originated by Alexandre Christopher

• 1995: GoF published: “Design Patterns: Elements of Object-Oriented Software”

• Now: Design patterns are language-independent– Java, C#, VB, Ruby, PHP,

Smalltalk, etc.

Image courtesy to www.selectorweb.com

Page 16: Introduction to design patterns

16

PATTERNS CLASSIFICATION

Design issues Design principles What is design pattern?

Commonly used patterns Case studies

Page 17: Introduction to design patterns

17

Design Patterns

Creational

BehavioralStructural

Page 18: Introduction to design patterns

18

COMMONLY USED PATTERNS

Design issues Design principles What is design pattern?Patterns classification

Case studies

Page 19: Introduction to design patterns

19

ScenarioCounter

Page 20: Introduction to design patterns

20

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

Page 21: Introduction to design patterns

21

Scenario

Enter the complex world through an entry

Page 22: Introduction to design patterns

22

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)

Page 23: Introduction to design patterns

23

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 statement

It looks simpler

Page 24: Introduction to design patterns

24

DatabaseFacade Outlook

JDBC APIDatabaseFacade

Clientuses

Connection

Statement

ResultSet

DatabaseMetadata

ResultSetMetadata

Driver…

Page 25: Introduction to design patterns

25

ScenarioSorting Employees

Page 26: Introduction to design patterns

26

Strategy

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

Page 27: Introduction to design patterns

27

Scenario

What does this code snippet do?

int totalSold = 0;List<Employee> sales = new LinkedList<Employee>();sales = dataSource.getAllData();

for( Employee staff : sales){totalSold += staff.getProductsSold();

}

//see above initIterator<Employee> itr = sales.iterator();while( itr.hasNext())

totalSold += itr.next().getProductsSold();

}

Page 28: Introduction to design patterns

28

What do you call this data structure?

Page 29: Introduction to design patterns

29

Can we do this?

int totalSold = 0;Tree<Employee> sales = new BinaryTree<Employee>();sales = dataSource.getAllData();

for( Employee staff : sales){totalSold += staff.getProductsSold();

}

Page 30: Introduction to design patterns

30

Iterator

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Page 31: Introduction to design patterns

31

ScenarioReflecting latest data

Page 32: Introduction to design patterns

32

Observer

GoF

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

Page 33: Introduction to design patterns

33

MVC

Robert Eckstein, Sun

Page 34: Introduction to design patterns

34

Demo: Observer and MVC in Swing

Page 35: Introduction to design patterns

35

Command

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

Page 36: Introduction to design patterns

36

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 37: Introduction to design patterns

37

GoF patterns

Page 38: Introduction to design patterns

38

CASE STUDIES

Design issues Design principles What is design pattern?Patterns classification Commonly used patterns

Page 39: Introduction to design patterns

39

References

• Freeman,E. , Freeman,E. , Bates, B. & Siera, K., Head First Design Patterns, O'Reilly Media, 2004.

• Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides, Design Patterns: Elements of Object-Oriented Software. Addison-Wesley, Boston, 1995.

• Robert Eckstein, Java SE Application Design With MVC, Sun (http://blogs.sun.com/JavaFundamentals/entry/java_se_application_design_with )

Page 40: Introduction to design patterns

40

Resources

• http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29

• http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf

• http://www.artima.com/lejava/articles/designprinciples.html

• http://www.oodesign.com/• http://www.javaworld.com/channel_content/j

w-patterns-index.html


Recommended