Design Patterns
Desktop Application Development
Hanoi - February, 2010Duong Trong Tan, ([email protected])
for
2
Contents
• Design Issues• Design principles• What is design pattern?• Patterns classification• Commonly used patterns• Case studies
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
4
DESIGN ISSUES Design principles What is design pattern? Patterns classification Commonly used patterns Case studies
5
What is design?
6
What is a good Design?
7
A constant: CHANGE!
8
COUPLING | COHESION
9
DESIGN PRINCIPLES Design issues
What is design pattern? Patterns classification Commonly used patterns Case studies
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
11
WHAT IS DESIGN PATTERN?
Design issues Design principles
Patterns classification Commonly used patterns Case studies
12
WHAT ARE TEMPLATES?
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
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
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
16
PATTERNS CLASSIFICATION
Design issues Design principles What is design pattern?
Commonly used patterns Case studies
17
Design Patterns
Creational
BehavioralStructural
18
COMMONLY USED PATTERNS
Design issues Design principles What is design pattern?Patterns classification
Case studies
19
ScenarioCounter
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
21
Scenario
Enter the complex world through an entry
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)
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
24
DatabaseFacade Outlook
JDBC APIDatabaseFacade
Clientuses
Connection
Statement
ResultSet
DatabaseMetadata
ResultSetMetadata
Driver…
25
ScenarioSorting Employees
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
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();
}
28
What do you call this data structure?
29
Can we do this?
int totalSold = 0;Tree<Employee> sales = new BinaryTree<Employee>();sales = dataSource.getAllData();
for( Employee staff : sales){totalSold += staff.getProductsSold();
}
30
Iterator
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
31
ScenarioReflecting latest data
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.
33
MVC
Robert Eckstein, Sun
34
Demo: Observer and MVC in Swing
35
Command
“Let’s hide (encapsulate) the way we call (invoke) methods.”
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
37
GoF patterns
38
CASE STUDIES
Design issues Design principles What is design pattern?Patterns classification Commonly used 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 )
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