Visitor pattern

Study case: employee structure

  • start with a "flat" data structure
  • transform it into an in-memory object tree
  • add the ability to print the structure into HTML
  • introduce visitor pattern
    • define traversing order
    • refactor "printing" using the visitor pattern

Data model

  • Manager
    • name, profile picture
    • role
    • list of subordinaries
  • Team lead
    • name, profile picture
    • list of team members
  • Engineer
    • name, profile picture
    • technology

Bueno

  • add external behavior without affecting the underlying data structure
  • remove auxiliary behavior from the data structures

No bueno

  • when a new "type" of data structure is added (or removed), all visitors have to be updated
  • [if needed] visitor has to internally track the state of traversal
  • exposing richer public interface

Object tree

  • 12 different data types
  • primitive
    • text, numeric, date, select
  • composite
    • root, struct, array, array item
  • dynamic
    • computed
    • reference

Visitors

  • populate data structure
    • from the incoming client request
    • from the database
  • finding nodes in a tree
  • export(JSON)
  • computing dynamic fields
  • resolving references
  • persisting the tree (3 variations)

I'm the last slide

Questions?

Visitor pattern

By vrabac

Visitor pattern

  • 296