visitor pattern by steve hayes

53
Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au ) Licensed under a Creative Commons Attribution-Noncommercial 3.0 License. Visitor Steve Hayes Cogent Consulting

Upload: melbournepatterns

Post on 05-Jul-2015

1.488 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

VisitorSteve Hayes

Cogent Consulting

Page 2: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

The Problem

Page 3: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 4: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Show me everything in a consistent manner...

Page 5: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Show me everything in a consistent manner...

... and send it to my friend via email

Page 6: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Show me everything in a consistent manner...

... and send it to my friend via email

oh, and send it to her phone as well...

Page 7: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Show me everything in a consistent manner...

... and send it to my friend via email

oh, and send it to her phone as well...

and print it.....

Page 8: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 9: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Some analysis music please...

Page 10: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 11: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Commonality is in the eye of the beholder

Page 12: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 13: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

With a little casual application of polymorphism...

Page 14: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 15: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Does this look bad to you?

Page 16: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

The solution

Page 17: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Visitors

Page 18: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

“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”

-- GOF

Page 19: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 20: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

A solution to a problem in a context

Page 21: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 22: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Headings, Details, ...

Page 23: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

and the Iterator

Page 24: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 25: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Let’s show it in code!

Page 26: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 27: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 28: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 29: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 30: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 31: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Time for visitors...

Page 32: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 33: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 34: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 35: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Melbourne Pushes Boundaries

The Wolves in the Wall

Redhill Consulting Pty Ltd - Ruby on Rails Plugins

Let's talk about tests, baby...

Page 36: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 37: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Melbourne Pushes BoundariesTim ColebatchMelbourne is experiencing its biggest growth surge since the 1960's

The Wolves in the Wall12.06

Redhill Consulting Pty Ltd - Ruby on Rails PluginsHere are some Ruby on Rails plugins we've developed...

Let's talk about tests, baby...Some long babbling about test naming heuristics

Page 38: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Cool!

Page 39: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

When?

Page 40: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

“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”

Page 41: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 42: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

“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. Visitor lets you keep unrelated operations together by defining them in one class. When the object structure is

shared by many applications, use Visitor to put operations in just those applications

that need them”

Page 43: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Show me everything in a consistent manner...

... and send it to my friend via email

oh, and send it to her phone as well...

and print it.....

Page 44: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

“the classes defining the object structure rarely change, but you often want to

define new operations over the structure. Changing the object structure classes requires redefining the interface to all

visitors, which is potentially costly. If the object structure classes change often, then

it’s probably better to define the operations in those classes”

Page 45: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Page 46: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Consequences

Page 47: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Visitor makes adding new operations easy

Page 48: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

A visitor gathers related operations and separates unrelated ones

Page 49: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Adding new ConcreteElement classes is hard

Page 50: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Visitors can cross object hierarchies

Page 51: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Visitors can accumulate state

Page 52: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Visitors may compromise encapsulation

Page 53: Visitor Pattern By Steve Hayes

Copyright Cogent Consulting Pty Ltd (http://www.cogentconsulting.com.au)Licensed under a Creative Commons Attribution-Noncommercial 3.0 License.

Discussion