Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

12
Composite and Related Patterns Kirk Scott

Transcript of Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Page 1: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Composite and Related Patterns

Kirk Scott

Page 2: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

The pitohui, a poisonous bird of New Guinea

Page 3: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

• This is an introductory unit.• These are the units/chapters belonging to this

section of the course:• Unit 25, Composite, book chapter 5• Unit 26, Chain of Responsibility, book chapter

12• Unit 27, Interpreter, book chapter 25• Unit 28, Visitor, book chapter 29

Page 4: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

• What will be given next is an extremely brief overview of these topics.

• You will find that the composite design pattern has a lot in common with trees

• Chain of responsibility, interpreter, and visitor all follow naturally, because interesting examples of those patterns can be built on top of composites/trees

Page 5: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Composite

• Book definition:• The intent of the Composite pattern is to let

clients treat individual objects and compositions of objects uniformly

• Comment mode on:• A composite can consist of one or more

components• Each component may be a single item or

another composite

Page 6: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

• In the verbal description above you may recognize the elements of a definition of a tree-like structure

• It is reminiscent of directory structures which can contain individual files as well as other directories

• This pattern has a distinctive UML diagram.• See the next overhead

Page 7: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.
Page 8: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Chain of Responsibility

• Book definition:• The intent of the Chain of Responsibility pattern is

to avoid coupling the sender of a request to its receiver, by giving more than one object a chance to handle the request.

• Comment mode on:• If a set of objects is linked together, you may call a

method on one.• Potentially that one calls a method on the next in

the chain, and so on down the line

Page 9: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Interpreter

• Book definition:• The intent of the Interpreter pattern is to let you

compose executable objects according to a set of composition rules that you define.

• Comment mode on:• This directly parallels composite• Interpreters interpret commands• Commands can consist of single commands or

collections (composites) of commands

Page 10: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

Visitor

• Book definition:• The intent of Visitor is to let you define a new

operation for a hierarchy without changing the hierarchy classes.

• Comment mode on:• The relationship with composite is that the

hierarchy you define the operation for may be the component/leaf/composite hierarchy which generates a tree

Page 11: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

In Summary and Mnemonic Devices:

• Composite: = Tree• Chain of Responsibility: Think “Tree of

Responsibility”• Interpreter: Remember the analogy between

directory structures and program structures (routines and subroutines)

• Visitor: Visiting the branches and leaves of trees…

Page 12: Composite and Related Patterns Kirk Scott. The pitohui, a poisonous bird of New Guinea.

The End