Slide 1

Slide 1 text

Design Patterns Kerry Buckley, 29 July 2019 Software

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Always 23 there are: no more, no less.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 7

Slide 7 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 8

Slide 8 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 9

Slide 9 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 10

Slide 10 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 11

Slide 11 text

– Christopher Alexander “The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 12

Slide 12 text

Patterns … • Balance opposing forces • Are based on many real-world solutions • Are generic enough to be flexible • Are specific enough to be useful

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

GoF Pattern Language • Pattern Name and Classification • Intent • Also Known As • Motivation (Forces) • Applicability • Structure • Participants • Collaboration • Consequences • Implementation • Sample Code • Known Uses • Related Patterns

Slide 15

Slide 15 text

Creational Structural Behavioural Concurrency • Abstract factory • Builder • Factory method • Singleton • … • Adapter • Composite • Decorator • Façade • … • Chain of reponsibility • Command • Iterator • Visitor • … • Active object • Monitor object • Reactor • Thread-specific storage • …

Slide 16

Slide 16 text

Example: the Iterator pattern

Slide 17

Slide 17 text

Intent Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

Slide 18

Slide 18 text

Problem There are many different types of collection (arrays, lists, trees etc). We often need to be able to traverse all elements of a collection. How do we separate the traversal itself from the operation we want to perform on each element?

Slide 19

Slide 19 text

Solution The main idea of the Iterator pattern is to extract the traversal behaviour of a collection into a separate object called an iterator.

Slide 20

Slide 20 text

Structure

Slide 21

Slide 21 text

Example

Slide 22

Slide 22 text

Applicability • Use the Iterator pattern when your collection has a complex data structure under the hood, but you want to hide its complexity from clients • Use the pattern to reduce duplication of the traversal code across your app • Use the Iterator when you want your code to be able to traverse different data structures or when types of these structures are unknown beforehand

Slide 23

Slide 23 text

Pros and Cons • Single Responsibility Principle (separate traversal algorithms) • Open/Closed Principle (implement new types of collection without changing client code) • You can iterate over the same collection in parallel because each iterator object contains its own iteration state • For the same reason, you can delay an iteration and continue it when needed

Slide 24

Slide 24 text

Pros and Cons • Applying the pattern can be an overkill if your app only works with simple collections • Using an iterator may be less efficient than going through elements of some specialized collections directly

Slide 25

Slide 25 text

Relations with Other Patterns • You can use Iterators to traverse Composite trees • You can use Factory Method along with Iterator to let collection subclasses return different types of iterators that are compatible with the collections • You can use Memento along with Iterator to capture the current iteration state and roll it back if necessary • You can use Visitor along with Iterator to traverse a complex data structure and execute some operation over its elements, even if they all have different classes

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Designery
 patterns?