Professional Documents
Culture Documents
09 DesignPatterns PDF
09 DesignPatterns PDF
Alexandre Bergel
http://bergel.eu
25/09/2019
Design Patterns: Elements of Reusable
Object-Oriented Software
Erich Gamma, Richard Helm, Ralph Johnson, John
M. Vlissides, 1994
Outline
1.Adapter
2.Proxy
3.Observer
4.Factory
Adapter Pattern
Consequences
The client and the adapted object remain independent
<<interface>>
Client
Subject
doAction()
Proxy RealSubject
doAction() doAction()
Proxy Pattern - Example
<<interface>>
Client
Image
displayImage()
ProxyImage RealImage
displayImage() displayImage()
Proxy Pattern - Example
delegate request
public class ProxyImage implements Image { to real subject
private String filename;
private Image image;
}
Proxy Pattern - Example, the client
images.get(0).displayImage();
images.get(1).displayImage();
images.get(0).displayImage(); // already loaded
}
}
Proxies are used for remote object
access
Example
A Java “stub” for a remote object accessed by Remote Method
Invocation (RMI)
Consequences
A Proxy decouples clients from servers. A Proxy introduces a level
of indirection
Example
A Button expects its observers to implement the ActionListener
interface.
(see the Interface and Adapter examples)
Consequences
Notification can be slow if there are many observers for an
observable, or if observers are themselves observable!
In Java < 8
package obs;
package obs;
import java.util.Observable;
import java.util.Observer; /* this is Event Handler */
// create an observer
final ResponseHandler respHandler = new ResponseHandler();
public EventSource() {
changes = new PropertyChangeSupport(this);
}
public void addObserver(ResponseHandler resp) {
changes.addPropertyChangeListener(resp);
}
// create an observer
final ResponseHandler respHandler = new ResponseHandler();
How would you make Lara fire arrows, bullets and so on?
26
Factory Pattern
27
Factory Pattern - UML
uses
Client
<<interface>>
Product
Concrete Factory
Product createProduct(): Product
28
Factory Pattern - UML
Uses
Lara
Fire ammo
<<interface>>
<<interface>> AmmoFactory
Ammo create(): Ammo
29
Factory Pattern - Example
30
Factory Pattern - Example
31
Lara is attacking
@Test
public void test() {
Lara lara = new Lara();
lara.ammoFactory(new ArrowFactory());
assertEquals(lara.fire().getClass(), Arrow.class);
}
32
Factory and Object Initialization
33
Factory and Object Initialization
34
Factory and Object Initialization
35
Terminology
36
What Problems do Design Patterns
solve?
Patterns:
document design experience
http://creativecommons.org/licenses/by-sa/2.5
Original version of this lecture from Oscar Nierstrasz, Uni - Bern
Attribution-ShareAlike 2.5
You are free:
• to copy, distribute, display, and perform the work
• to make derivative works
• to make commercial use of the work
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting
work only under a license identical to this one.
• For any reuse or distribution, you must make clear to others the license terms of this work.
• Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.