• Non ci sono risultati.

Design Patterns 2

N/A
N/A
Protected

Academic year: 2021

Condividi "Design Patterns 2"

Copied!
11
0
0

Testo completo

(1)

© 2002-2004 Riccardo Solmi 1

TITLE

Laboratorio di Progettazione di Sistemi Software

Design Patterns 2

Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Indice degli argomenti

ƒ Catalogo di Design Patterns:

• Composite,

• Iterator,

• Visitor

(2)

© 2002-2004 Riccardo Solmi 3

Composite

ƒ Intent

• Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly

ƒ Applicability

• you want to represent part-whole hierarchies of objects.

• you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly

Composite /2

ƒ Structure

ƒ Participants

ƒ Component

declares the interface for objects in the composition.

implements default behavior for the interface common to all classes.

declares an interface for accessing and managing its child components.

(optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.

Leaf

represents leaf objects in the composition.

defines behavior for primitive objects

Composite

defines behavior for components having children.

stores child components.

Implements child-related operations in the Component interface.

Client

manipulates objects in the composition through the Component interface.

(3)

© 2002-2004 Riccardo Solmi 5

Composite /3

ƒ Collaborations

• Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.

ƒ Consequences

• defines class hierarchies consisting of primitive objects and composite objects

• makes the client simple

• makes it easier to add new kinds of components

• can make your design overly general

Composite example 1

(4)

© 2002-2004 Riccardo Solmi 7

Composite example 2

ƒ Graphics applications like drawing editors and schematic capture systems let users build complex diagrams out of simple components

Composite Questions

ƒ Part 1: How does the Composite pattern help to consolidate system-wide conditional logic?

ƒ Part 2: Would you use the composite pattern if you did not

have a part-whole hierarchy? In other words, if only a few

objects have children and almost everything else in your

collection is a leaf (a leaf can have no children), would you

still use the composite pattern to model these objects?

(5)

© 2002-2004 Riccardo Solmi 9

Iterator

ƒ Intent

• Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation

ƒ Applicability

• to access an aggregate object's contents without exposing its internal representation.

• to support multiple traversals of aggregate objects.

• to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).

Iterator /2

ƒ Structure

ƒ Participants

Iterator

• defines an interface for accessing and traversing elements.

ConcreteIterator

• implements the Iterator interface.

• keeps track of the current position in the traversal of the aggregate.

Aggregate

defines an interface for creating an Iterator object.

ConcreteAggregate

implements the Iterator creation interface to return an instance of the proper ConcreteIterator.

(6)

© 2002-2004 Riccardo Solmi 11

Iterator /3

ƒ Collaborations

• A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal

ƒ Consequences

• It supports variations in the traversal of an aggregate

• Iterators simplify the Aggregate interface

• More than one traversal can be pending on an aggregate

Iterator example 1

(7)

© 2002-2004 Riccardo Solmi 13

Iterator example 2

ƒ An aggregate object such as a list should give you a way to access its elements without exposing its internal structure

Iterator questions

ƒ Consider a composite that contains loan objects. The loan object interface contains a method called

"AmountOfLoan()", which returns the current market value

of a loan. Given a requirement to extract all loans above,

below or in between a certain amount, would you write or

use an Iterator to do this?

(8)

© 2002-2004 Riccardo Solmi 15

Visitor

ƒ Intent

• Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates

ƒ Applicability

• an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes

• many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations

• the classes defining the object structure rarely change, but you often want to define new operations over the structure

Visitor /2

ƒ Structure

(9)

© 2002-2004 Riccardo Solmi 17

Visitor /3

ƒ Collaborations

• A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor

• When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary

Visitor /4

ƒ The following interaction diagram illustrates the collaborations between

an object structure, a visitor, and two elements

(10)

© 2002-2004 Riccardo Solmi 19

Visitor /4

ƒ Consequences

• Visitor makes adding new operations easy

• A visitor gathers related operations and separates unrelated ones

• Adding new ConcreteElement classes is hard

• Visiting across class hierarchies

• Accumulating state

• Breaking encapsulation

Visitor example 1

(11)

© 2002-2004 Riccardo Solmi 21

Visitor example 2

ƒ Compiler that represents programs as abstract syntax trees

Visitor Questions

ƒ One issue with the Visitor pattern involces cyclicality. When

you add a new Visitor, you must make changes to existing

code. How would you work around this possible problem?

Riferimenti

Documenti correlati

of electrons condensated in the superconductive states n s are a fraction of the system, according to the second order phase transition theory.. If we compute the current, there is

• an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes. •

Our attention focusses mainly on the local housing patterns where the signs of the historical and 

The existence of at least two distinct solutions to ( P K ,c ) in the case of the round two-sphere follows, always for c > 0 large enough, from a general result of Bottkoll [ 7

Using the construction of Containing Spaces given in [1] we define some kind of games considered on topological classes of spaces.. Keywords: topological game, containing

If a method of a base class specifies an exception list, all derived classes that override that method must specify the same exception list, else the compiler will complain. Java

public abstract class Wind extends Instrument { abstract public void play(Note n);. public String what() { return "Wind"; } public void

Each node may have a parent node A tree consists of one root node, plus zero or more children sub-trees..