Design Patterns
239 pág.

Design Patterns

DisciplinaEngenharia de Software I4.699 materiais52.164 seguidores
Pré-visualização50 páginas
 public void ClientClassMethod()
 // call AdapteeClass's method to implement ClientClassMethod
[1] Freeman, Eric; Freeman, Elisabeth; Kathy, Sierra; Bert, Bates (2004). Hendrickson, Mike. ed (in English) (paperback). Head First Design
Patterns (http:/ / it-ebooks. info/ book/ 252/ ). 1. O'REILLY. pp. 244. ISBN 978-0-596-00712-6. . Retrieved 2012-07-02.
External links
\u2022 Adapter in UML and in LePUS3 (a formal modelling language) (http:/ / www. lepus. org. uk/ ref/ companion/
Adapter(Class). xml)
\u2022 Description in Portland Pattern Repository's Wiki (http:/ / www. c2. com/ cgi/ wiki?AdapterPattern)
\u2022 Adapter Design Pattern (http:/ / www. codeproject. com/ Articles/ 186003/ Adapter-Design-Pattern. aspx) in
\u2022 PerfectJPattern Open Source Project (http:/ / perfectjpattern. sourceforge. net/ dp-adapter. html), Provides
componentized implementation of the Adapter Pattern in Java
\u2022 The Adapter Pattern in Python (http:/ / ginstrom. com/ scribbles/ 2009/ 03/ 27/ the-adapter-pattern-in-python/ )
(detailed tutorial)
\u2022 The Adapter Pattern in Eclipse RCP (http:/ / www. eclipse. org/ articles/ article. php?file=Article-Adapters/ index.
\u2022 Adapter Design Pattern (http:/ / sourcemaking. com/ design_patterns/ adapter)
Bridge pattern 87
Bridge pattern
The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction
from its implementation so that the two can vary independently".[1] The bridge uses encapsulation, aggregation, and
can use inheritance to separate responsibilities into different classes.
When a class varies often, the features of object-oriented programming become very useful because changes to a
program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful
when both the class as well as what it does vary often. The class itself can be thought of as the implementation and
what the class can do as the abstraction. The bridge pattern can also be thought of as two layers of abstraction.
The bridge pattern is often confused with the adapter pattern. In fact, the bridge pattern is often implemented
using the class adapter pattern, e.g. in the Java code below.
Variant: The implementation can be decoupled even more by deferring the presence of the implementation to the
point where the abstraction is utilized.
defines the abstract interface
maintains the Implementor reference.
extends the interface defined by Abstraction
defines the interface for implementation classes
implements the Implementor interface
Bridge pattern 88
Bridge in LePUS3 (legend [2])
The following Java (SE 6) program illustrates the 'shape' example given below and will output: at 1.000000:2.000000 radius 7.5000000 at 5.000000:7.000000 radius 27.500000
/** "Implementor" */
interface DrawingAPI {
 public void drawCircle(double x, double y, double radius);
/** "ConcreteImplementor" 1/2 */
class DrawingAPI1 implements DrawingAPI {
 public void drawCircle(double x, double y, double radius) {
 System.out.printf(" at %f:%f radius %f\n", x, y, 
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
 public void drawCircle(double x, double y, double radius) {
 System.out.printf(" at %f:%f radius %f\n", x, y, 
/** "Abstraction" */
abstract class Shape {
 protected DrawingAPI drawingAPI;
Bridge pattern 89
 protected Shape(DrawingAPI drawingAPI){
 this.drawingAPI = drawingAPI;
 public abstract void draw(); // 
 public abstract void resizeByPercentage(double pct); // 
/** "Refined Abstraction" */
class CircleShape extends Shape {
 private double x, y, radius;
 public CircleShape(double x, double y, double radius, DrawingAPI 
drawingAPI) {
 this.x = x; this.y = y; this.radius = radius;
 // low-level i.e. Implementation specific
 public void draw() {
 drawingAPI.drawCircle(x, y, radius);
 // high-level i.e. Abstraction specific
 public void resizeByPercentage(double pct) {
 radius *= pct;
/** "Client" */
class BridgePattern {
 public static void main(String[] args) {
 Shape[] shapes = new Shape[] {
 new CircleShape(1, 2, 3, new DrawingAPI1()),
 new CircleShape(5, 7, 11, new DrawingAPI2()),
 for (Shape shape : shapes) {
Bridge pattern 90
[1] Gamma, E, Helm, R, Johnson, R, Vlissides, J: Design Patterns, page 151. Addison-Wesley, 1995
[2] http:/ / lepus. org. uk/ ref/ legend/ legend. xml
External links
\u2022 Bridge in UML and in LePUS3 (http:/ / www. lepus. org. uk/ ref/ companion/ Bridge. xml) (a formal modelling
\u2022 "C# Design Patterns: The Bridge Pattern" (http:/ / www. informit. com/ articles/ article. aspx?p=30297). Sample
Chapter. From: James W. Cooper. C# Design Patterns: A Tutorial (http:/ / www. informit. com/ store/ product.
aspx?isbn=0-201-84453-2). Addison-Wesley. ISBN 0-201-84453-2.
Composite pattern
In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that
a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to
"compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets
clients treat individual objects and compositions uniformly.[1]
When dealing with tree-structured data, programmers often have to discriminate between a leaf-node and a branch.
This makes code more complex, and therefore, error prone. The solution is an interface that allows treating complex
and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition
of one-or-more similar objects, all exhibiting similar functionality. This is known as a "has-a" relationship between
objects.[2] The key concept is that you can manipulate a single instance of the object just as you would manipulate a
group of them. The operations you can perform on all the composite objects often have a least common denominator
relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define
resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.
Composite pattern 91
When to use
Composite can be used when clients should ignore the difference between compositions of objects and individual
objects.[1] If programmers find that they are using multiple objects in the same way, and often have nearly identical
code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives
and composites as homogeneous.
Composite pattern in UML.
\u2022\u2022 is the abstraction for all components, including composite ones
\u2022\u2022 declares the interface for objects in the composition
\u2022\u2022 (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if
that's appropriate
\u2022\u2022 represents leaf objects in the composition .
\u2022\u2022 implements all Component methods
\u2022\u2022 represents a composite Component (component having children)
\u2022\u2022 implements methods to manipulate children
\u2022\u2022 implements all Component methods, generally by