Design Patterns
239 pág.

Design Patterns

DisciplinaEngenharia de Software I7.132 materiais71.076 seguidores
Pré-visualização50 páginas
a large object in memory,
a file, or some other resource that is
expensive or impossible to duplicate.
A well-known example of the proxy
pattern is a reference counting pointer
In situations where multiple copies of a
complex object must exist, the proxy
pattern can be adapted to incorporate
the flyweight pattern in order to reduce
the application's memory footprint.
Typically, one instance of the complex
object and multiple proxy objects are
created, all of which contain a
reference to the single original
complex object. Any operations
performed on the proxies are
forwarded to the original object. Once
all instances of the proxy are out of
scope, the complex object's memory may be deallocated.
Proxy pattern 113
The following Java example illustrates the "virtual proxy" pattern. The ProxyImage class is used to access a
remote method.
The example creates first an interface against which the pattern creates the classes. This interface contains only one
method to display the image, called displayImage(), that hast to be coded by all classes implementing it.
The proxy class ProxyImage is running on another system than the real image class itself and can represent the
real image RealImage over there. The image information is accessed from the disk. Using the proxy pattern, the
code of the ProxyImage avoids multiple loading of the image, accessing it from the other system in a
memory-saving manner.
interface Image {
 public abstract void displayImage();
//on System A 
class RealImage implements Image {
 private String filename = null;
 * Constructor
 * @param FILENAME
 public RealImage(final String FILENAME) { 
 filename = FILENAME;
 * Loads the image from the disk
 private void loadImageFromDisk() {
 System.out.println("Loading " + filename);
 * Displays the image
 public void displayImage() { 
 System.out.println("Displaying " + filename); 
//on System B 
class ProxyImage implements Image {
 private RealImage image = null;
 private String filename = null;
Proxy pattern 114
 * Constructor
 * @param FILENAME
 public ProxyImage(final String FILENAME) { 
 filename = FILENAME; 
 * Displays the image
 public void displayImage() {
 if (image == null) {
 image = new RealImage(filename);
class ProxyExample {
 * Test method
 public static void main(String[] args) {
 final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1");
 final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2"); 
 IMAGE1.displayImage(); // loading necessary
 IMAGE1.displayImage(); // loading unnecessary
 IMAGE2.displayImage(); // loading necessary
 IMAGE2.displayImage(); // loading unnecessary
 IMAGE1.displayImage(); // loading unnecessary
The program's output is:
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
Proxy pattern 115
External links
\u2022 Proxy pattern in Java (http:/ / wiki. java. net/ bin/ view/ Javapedia/ ProxyPattern)
\u2022 Proxy pattern in UML and in LePUS3 (a formal modelling language) (http:/ / www. lepus. org. uk/ ref/
companion/ Proxy. xml)
\u2022 Take control with the Proxy design pattern (http:/ / www. javaworld. com/ javaworld/ jw-02-2002/
jw-0222-designpatterns. html) by David Geary,
\u2022 PerfectJPattern Open Source Project (http:/ / perfectjpattern. sourceforge. net/ dp-proxy. html), Provides
componentized implementation of the Proxy Pattern in Java
\u2022 Adapter vs. Proxy vs. Facade Pattern Comparison (http:/ / www. netobjectives. com/ PatternRepository/ index.
\u2022 Proxy Design Pattern (http:/ / sourcemaking. com/ design_patterns/ proxy)
\u2022 Proxy pattern description from the Portland Pattern Repository (http:/ / c2. com/ cgi/ wiki?ProxyPattern)
Behavioral patterns
Behavioral pattern
In software engineering, behavioral design patterns are design patterns that identify common communication
patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out
this communication.
Examples of this type of design pattern include:
\u2022 Chain of responsibility pattern: Command objects are handled or passed on to other objects by logic-containing
processing objects
\u2022 Command pattern: Command objects encapsulate an action and its parameters
\u2022 "Externalize the Stack": Turn a recursive function into an iterative one that uses a stack.[1]
\u2022 Hierarchical visitor pattern: Provide a way to visit every node in a hierarchical data structure such as a tree.
\u2022 Interpreter pattern: Implement a specialized computer language to rapidly solve a specific set of problems
\u2022 Iterator pattern: Iterators are used to access the elements of an aggregate object sequentially without exposing its
underlying representation
\u2022 Mediator pattern: Provides a unified interface to a set of interfaces in a subsystem
\u2022 Memento pattern: Provides the ability to restore an object to its previous state (rollback)
\u2022 Null Object pattern: designed to act as a default value of an object
\u2022 Observer pattern: aka Publish/Subscribe or Event Listener. Objects register to observe an event that may be raised
by another object
\u2022 Weak reference pattern: De-couple an observer from an observable.[2]
\u2022 Protocol stack: Communications are handled by multiple layers, which form an encapsulation hierarchy.[3]
\u2022 Scheduled-task pattern: A task is scheduled to be performed at a particular interval or clock time (used in
real-time computing)
\u2022 Single-serving visitor pattern: Optimise the implementation of a visitor that is allocated, used only once, and then
\u2022 Specification pattern: Recombinable Business logic in a boolean fashion
\u2022 State pattern: A clean way for an object to partially change its type at runtime
\u2022 Strategy pattern: Algorithms can be selected on the fly
\u2022 Template method pattern: Describes the program skeleton of a program
\u2022 Visitor pattern: A way to separate an algorithm from an object
[1] "Externalize The Stack" (http:/ / c2. com/ cgi/ wiki?ExternalizeTheStack). 2010-01-19. Archived from the original (http:/ / c2. com/ )
on 2010-01-19. . Retrieved 2012-05-21.
[2] [ |Ashod Nakashian (http:/ / c2. com/ cgi/ wiki?AshodNakashian)] (2004-04-11). "Weak Reference Pattern" (http:/ / c2. com/ cgi/
wiki?WeakReferencePattern). Archived from the original (http:/ / c2. com/ ) on 2004-04-11. . Retrieved 2012-05-21.
[3] "Protocol Stack" (http:/ / c2. com/ cgi/ wiki?ProtocolStack). 2006-09-05. Archived from the original (http:/ / c2. com/ ) on
2006-09-05. . Retrieved 2012-05-21.
Chain-of-responsibility pattern 117
Chain-of-responsibility pattern
In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of
command objects and a series of processing objects. Each processing object contains logic that defines the types of
command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also
exists for adding new processing objects to the end of this chain.
In a variation of the standard chain-of-responsibility model, some handlers