Patterns

16
Patterns

description

Patterns. Outline. Iterators The Pattern Concept The OBSERVER Pattern Layout Managers and the STRATEGY Pattern Components, Containers, and the COMPOSITE Pattern How to Recognize Patterns Putting Patterns to Work. Design patterns. history - PowerPoint PPT Presentation

Transcript of Patterns

Page 1: Patterns

Patterns

Page 2: Patterns

Outline

• Iterators • The Pattern Concept • The OBSERVER Pattern • Layout Managers and the STRATEGY Pattern • Components, Containers, and the COMPOSITE

Pattern How to Recognize Patterns Putting Patterns to Work

Page 3: Patterns

3

Design patterns• history

– the concept of a "pattern" was first expressed in Christopher Alexander's work A Pattern Language in 1977 (2543 patterns)

– in 1990 a group called the Gang of Four or "GoF" (Gamma, Helm, Johnson, Vlissides) compile a catalog of design patterns

• design pattern: a solution to a common software problem in a context– example: Iterator pattern

The Iterator pattern defines an interface that declares methods for sequentially accessing the objects in a collection.

Page 4: Patterns

List Iterators

LinkedList<String> list = . . . ;ListIterator<String> iterator = list.listIterator();

while (iterator.hasNext()){String current = iterator.next();. . .}

Why iterators?

Page 5: Patterns

Classical Data structure method

• Traverse links directly

• Link currentLink = list.head;while (currentLink != null){Object current = currentLink.data;currentLink = currentLink.next;}

• Exposes implementation• Error-prone

Page 6: Patterns

High level view of data strcutures

• Queue

• Array with random access

• List???

Page 7: Patterns

List with cursor

• for (list.reset(); list.hasNext(); list.next()){Object x = list.get();. . .}

• Disadvantage: Only one cursor per list• Iterator is superior concept

Page 8: Patterns

Pattern Concept

• History: Architectural Patterns• Christopher Alexander• Each pattern has – a short name– a brief description of the context– a lengthy description of the problem– a prescription for the solution

Page 9: Patterns

Short Passages Pattern

Page 10: Patterns

Short Passages Pattern

• Context"...Long, sterile corridors set the scene for everything bad about

modern architecture..."

• ProblemA lengthy description of the problem, including

– a depressing picture– issues of light and furniture– research about patient anxiety in hospitals– research that suggests that corridors over 50 ft are considered

uncomfortable

Page 11: Patterns

Short Passages Pattern

• Keep passages short. Make them as much like rooms as possible, with carpets or wood on the floor, furniture, bookshelves, beautiful windows. Make them generous in shape and always give them plenty of light; the best corridors and passages of all are those which have windows along an entire wall.

Page 12: Patterns

Iterator Pattern

• Context– An aggregate object contains element objects– Clients need access to the element objects– The aggregate object should not expose its

internal structure– Multiple clients may want independent access

Page 13: Patterns

Iterator Pattern

• Define an iterator that fetches one element at a time Each iterator object keeps track of the position of the next element If there are several aggregate/iterator variations, it is best if the aggregate and iterator classes realize common interface types.

Page 14: Patterns
Page 15: Patterns

• Names in pattern are examples• Names differ in each occurrence of pattern

N a m e i n D e s i g n P a tt e r n A c t u a l N a m e ( l i n k e d l i s t s )

A g g r e g a t e L i s t

C o n c r e t e A g g r e g a t e L i n k e d L i s t

I t e r a t o r L i s t I t e r a t o r

C o n c r e t e I t e r a t o r a n o n y m o u s c l a s s i m p l e m e n ti n g L i s t I t e r a t o r

c r e a t e I t e r a t o r ( ) l i s t I t e r a t o r ( )

n e x t ( ) n e x t ( )

i s D o n e ( ) o p p o s i t e o f h a s N e x t ( )

c u r r e n t I t e m ( ) r e t u r n v a l u e o f h a s N e x t ( )

Page 16: Patterns

16

More about patterns

• A pattern describes a recurring software structure– is abstract from concrete design elements such as problem domain,

programming language– identifies classes that play a role in the solution to a problem,

describes their collaborations and responsibilities– lists implementation trade-offs– patterns are not code or designs; must be instantiated/applied

• the software engineer is required to:– evaluate trade-offs and impact of using a pattern in the system at hand– make design and implementation decision how best to apply the

pattern, perhaps modify it slightly– implement the pattern in code and combine it with other patterns