10 DesignPatterns
10 DesignPatterns
Design Patterns
Outline
// A C++ Implementation
ListIterator<Employee> itr = list.iterator();
for(itr.First(); !itr.IsDone(); itr.Next()) {
cout << itr.CurrentItem().toString();
Java version of Iterator
interface Iterator
boolean hasNext()
Returns true if the iteration has more elements.
Object next()
Returns the next element in the iteration and updates the iteration to
refer to the next (or have hasNext() return false)
void remove()
Removes the most recently visited element
Java’s Iterator interface
// The Client code
List<BankAccount> bank =
new ArrayList<BankAccount>();
bank.add(new BankAccount("One", 0.01) );
// ...
bank.add(new BankAccount("Nine thousand", 9000.00));
String ID = "Two";
Iterator<BankAccount> itr = bank.iterator();
while(itr.hasNext()) {
if(itr.next().getID().equals(searchAcct.getID()))
System.out.println("Found " + ref.getID());
}
UML Diagram of Java's
Iterator with a few Collections
<<interface>>
<<interface>>
List Iterator
iterator(): Iterator hasNext()
… next()
Rick Mercer
CSC 335: Object-Oriented
Programming and Design
The Decorator Pattern from GoF
Intent
– Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to sub classing to extend
flexibility
Also Known As Wrapper
Motivation
– Want to add properties to an existing object.
2 Examples
• Add borders or scrollbars to a GUI component
• Add stream functionality such as reading a line of input or
compressing a file before sending it over the wire
Applicability
Use Decorator
– To add responsibilities to individual objects
dynamically without affecting other objects
– When extending classes is impractical
• Sometimes a large number of independent extensions
are possible and would produce an explosion of
subclasses to support every combination (this
inheritance approach is on the next few slides)
An Application
Suppose there is a TextView GUI component and
you want to add different kinds of borders and/or
scrollbars to it
You can add 3 types of borders
– Plain, 3D, Fancy
and 1 or 2 two scrollbars
– Horizontal and Vertical
An inheritance solution requires15 classes for one
view
That’s a lot of classes!
1.TextView_Plain
2.TextView_Fancy
3.TextView_3D
4.TextView_Horizontal
5.TextView_Vertical
6.TextView_Horizontal_Vertical
7.TextView_Plain_Horizontal
8.TextView_Plain_Vertical
9.TextView_Plain_Horizontal_Vertical
10.TextView_3D_Horizontal
11.TextView_3D_Vertical
12.TextView_3D_Horizontal_Vertical
13.TextView_Fancy_Horizontal
14.TextView_Fancy_Vertical
15.TextView_Fancy_Horizontal_Vertical
Disadvantages
// and later on …
list = (ArrayList<String>)inFile.readObject();
Another Decorator Example
GZIPInputStream
GZIPInputStream
Summary
Strategy
Pattern: Strategy
Name: Strategy (a.k.a Policy)
Problem: You want to encapsulate a family of
algorithms and make them interchangeable. Strategy
lets the algorithm vary independently from the clients
that use it (GoF)
Solution: Create an abstract strategy class (or
interface) and extend (or implement) it in numerous
ways. Each subclass defines the same method
names in different ways
Design Pattern: Strategy
Consequences:
– Allows families of algorithms
Known uses:
– Critters seen in section for Rick’s 127B / 227
– Layout managers in Java
– Different Poker Strategies in a 335 Project
– Different PacMan chase strategies in a 335 Project
– Different Jukebox policies that can be
Java Example of Strategy
this.setLayout(new FlowLayout());
this.setLayout(new GridLayout());
In Java, a container HAS-A layout manager
– There is a default
– You can change a container's layout manager with
a setLayout message
Change the stategy at runtime
Demonstrate LayoutControllerFrame.java
private class FlowListener
implements ActionListener {
// There is another ActionListener for GridLayout
public void actionPerformed(ActionEvent evt) {
// Change the layout strategy of the JPanel
// and tell it to lay itself out
centerPanel.setLayout(new FlowLayout());
centerPanel.validate();
}
}
12-43
interface LayoutManager
setPreferredSize(di:Dimension)
implements
Name: Observer
Problem: Need to notify a changing number of
objects that something has changed
Solution: Define a one-to-many dependency
between objects so that when one object
changes state, all its dependents are notified
and updated automatically
Examples
16-49
Examples