Design Patterns - University of Washington• If your design or implementation has a problem,...
Transcript of Design Patterns - University of Washington• If your design or implementation has a problem,...
![Page 1: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/1.jpg)
Emina Torlak [email protected]
CSE 403: Software Engineering, Winter 2016courses.cs.washington.edu/courses/cse403/16wi/
Design Patterns
![Page 2: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/2.jpg)
Outline
2
• Overview of design patterns
• Creational patterns
• Structural patterns
• Behavioral patterns
![Page 3: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/3.jpg)
introoverview of design patterns
![Page 4: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/4.jpg)
What is a design pattern?
4
![Page 5: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/5.jpg)
What is a design pattern?
4
• A standard solution to a common programming problem• a design or implementation structure that achieves a particular purpose
• a high-level programming idiom
![Page 6: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/6.jpg)
What is a design pattern?
4
• A standard solution to a common programming problem• a design or implementation structure that achieves a particular purpose
• a high-level programming idiom
• A technique for making code more flexible or efficient• reduce coupling among program components
• reduce memory overhead
![Page 7: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/7.jpg)
What is a design pattern?
4
• A standard solution to a common programming problem• a design or implementation structure that achieves a particular purpose
• a high-level programming idiom
• A technique for making code more flexible or efficient• reduce coupling among program components
• reduce memory overhead
• Shorthand for describing program design• a description of connections among program components
• the shape of a heap snapshot or object model
![Page 8: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/8.jpg)
Why should you care?
5
• You could come up with these solutions on your own …
• But you shouldn't have to!
• A design pattern is a known solution to a known problem.
![Page 9: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/9.jpg)
Types of design patterns
6
• Creational patterns • how objects are instantiated
• Structural patterns • how objects / classes can be combined
• Behavioral patterns • how objects communicate
• Concurrency patterns • how computations are parallelized / distributed
![Page 10: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/10.jpg)
When (not) to use design patterns
7
![Page 11: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/11.jpg)
When (not) to use design patterns
7
• Rule 1: delay • Understand the problem & solution first, then improve it
![Page 12: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/12.jpg)
When (not) to use design patterns
7
• Rule 1: delay • Understand the problem & solution first, then improve it
• Design patterns can increase or decrease understandability of code• Add indirection, increase code size
• Improve modularity, separate concerns, ease description
![Page 13: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/13.jpg)
When (not) to use design patterns
7
• Rule 1: delay • Understand the problem & solution first, then improve it
• Design patterns can increase or decrease understandability of code• Add indirection, increase code size
• Improve modularity, separate concerns, ease description
• If your design or implementation has a problem, consider design patterns that address that problem
• References:• Design Patterns: Elements of Reusable Object-Oriented Software, by
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, 1995.
• Effective Java: Programming Language Guide, by Joshua Bloch, 2001.
![Page 14: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/14.jpg)
createcreational patterns
![Page 15: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/15.jpg)
Kinds of creational patterns
9
• Factory (method)
• Abstract factory
• Builder
• Prototype
• Flyweight
• Singleton
Creational patterns address inflexibility of constructors in Java:
1. Can't return a subtype of the class they belong to
2. Always return a fresh new object, never re-use one
![Page 16: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/16.jpg)
Factory patterns (problem)
10
interface Matrix { ... } class SparseMatrix implements Matrix { ... } class DenseMatrix implements Matrix { ... }
• Clients use the supertype (Matrix)• But still need to use a SparseMatrix or DenseMatrix constructor
• Must decide concrete implementation somewhere
• Don’t want to change code to use a different constructor
![Page 17: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/17.jpg)
Factory method pattern (one solution)
11
class MatrixFactory { public static Matrix createMatrix() { return new SparseMatrix(); } }
• Clients call createMatrix instead of a particular constructor
• Advantages:• To switch the implementation, change only one place
• createMatrix can do arbitrary computations to decide what kind of matrix to make
• Frequently used in frameworks (e.g., Java swing)• BorderFactory.createRaisedBevelBorder()
![Page 18: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/18.jpg)
Abstract factory pattern (another solution)
12
A factory class that can be subclassed (to make new kinds of factories) and that has an overridable method to create its objects
![Page 19: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/19.jpg)
shapestructural patterns
![Page 20: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/20.jpg)
Kinds of structural patterns
14
• Composite
• Decorator
• Adapter
• Proxy
• …
Structural patterns enable client code to
1. modify the interface
2. extend behavior
3. restrict access
4. unify access
![Page 21: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/21.jpg)
Composite pattern
15
A client can manipulate the whole or any part uniformly.
![Page 22: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/22.jpg)
Composite pattern example: Java GUI
16
Container north = new JPanel(new FlowLayout()); north.add(new JButton("Button 1")); north.add(new JButton("Button 2"));
Container south = new JPanel(new BorderLayout()); south.add(new JLabel("Southwest"), BorderLayout.WEST); south.add(new JLabel("Southeast"), BorderLayout.EAST);
Container overall = new JPanel(new BorderLayout()); overall.add(north, BorderLayout.NORTH); overall.add(new JButton("Center Button”), BorderLayout.CENTER); overall.add(south, BorderLayout.SOUTH);
frame.add(overall);
![Page 23: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/23.jpg)
Decorator pattern
17
A decorator is a wrapper object that modifies behavior of, or adds features to, another object.
![Page 24: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/24.jpg)
Decorator pattern example: Java IO
18
// InputStreamReader/BufferedReader decorate InputStream InputStream in = new FileInputStream("hardcode.txt"); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr);
// With a BufferedReader decorator, read an // entire line from the file in one call // (InputStream only provides public int read() ) String wholeLine = br.readLine();
• InputStream class has only public int read() method to read one letter at a time.
• Decorators such as BufferedReader add functionality to read the stream more easily.
![Page 25: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/25.jpg)
behavebehavioral patterns
![Page 26: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/26.jpg)
Kinds of behavioral patterns
20
• Null object
• Template method
• Iterator
• Strategy
• …
Behavioral patterns identify and capture common patterns of communication between objects.
![Page 27: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/27.jpg)
Null object pattern
21
A client can treat the absence of an object transparently.
![Page 28: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/28.jpg)
Null object pattern example: empty list
22
List<Object> search(String value) { if (“”.equal(value)) return Collections.emptyList(); // null object (empty list)
else return …;
}
if (search(userInput).isEmpty()) // no NullPointerException …
else …
![Page 29: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/29.jpg)
Template method pattern
23
Subclasses can redefine certain steps of an algorithm without changing the algorithm's structure.
![Page 30: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/30.jpg)
Template method example: games
24
abstract class Game { protected int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner();
// template method public final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); } } class Monopoly extends Game { … } class Chess extends Game { … }
![Page 31: Design Patterns - University of Washington• If your design or implementation has a problem, consider design patterns that address that problem • References: • Design Patterns:](https://reader034.fdocuments.us/reader034/viewer/2022042210/5eae844adca44b60bb1eb7e1/html5/thumbnails/31.jpg)
Summary
25
• A design pattern is a known solution to a known problem.
• Creational, structural, behavioral
• If your design or implementation has a problem, then (and only then) consider design patterns that address that problem.