본문 바로가기
Computer Science

Visitor Pattern

by OKOK 2021. 7. 29.

Purpose

  • Allowing one or more operations to be applied to a set of objects at runtime
  • Decoupling the operations from the object structure(=the set of objests)

 

Use when

  • An object structure must have many unrelated operations performed upon it.
  • The object structure can't cahnge but operations on it can
  • Operations must be performed on the concrete classes of an object structure
  • Operations should be albe to operate on multiple object structures that implement the same interface sets.

 

More operation types are expected

  • There will be one class for every single thing that needs to be performed.
  • Therefore, distrubuting operations among various node classes is hard to understand, maintain and change

 

Motivations and Benefits

  • Motivations
    - An obeject structure contains many classes of objects
    - Many distinct and unrelated operations on these obejcts
    - Classes defining the object structure rarely change, but operations chage frequently
  • Benefits
    - Makes adding new operations easy.
    - Gathers related operations and separates unrelated operations
    - Visitors can visit objects that don't have a common parent class.

  • Compare the normalized pattern above with the Trash module below.
  • It is used when the characteristics of the operation in the existing class and the newly added operation are different.

  • A client must create a ConcreteVisitor object and then traverse the object structre, visiting each element with the visiotor   
  • When the elemment is visited, it calls the Visitor operation that corresponds to its class.
  • The element supplies itself as an argument to this operation to let the visitor acccess its state, if necessary

 

Method Overloading

  • Method overloading, however, is done at compile time.
    - using "name mangling" where the internal name of the method has the argument's type encoded in it
    - Different from method overriding (run-time polymorphism)

 

Technical Details

  • Double Dispatch- Speical form of multiple dispatch
    - a mechanism that dispatches a method call to different concrete methods depending on the runtime types of two objects involved in the call.
  • Single Dispatch
    - In most object-oriented systems, the concrete method that is called from a method call in the code depends on the dynamic type of a single object and therefore they are known as single dispatch calls or simply virtual function(method) calls.

 

Double Dispatch

  • Double Dispatch- Speical form of multiple dispatch
    - a mechanism that dispatches a method call to different concrete methods depending on the runtime types of two objects involved in the call.
  • Single Dispatch
    - In most object-oriented systems, the concrete method that is called from a method call in the code depends on the dynamic type of a single object and therefore they are known as single dispatch calls or simply virtual function(method) calls.

 

Visitor Pattern and Double Dispatch

  • Visitor pattern requires a programming language that supports double dispatch.
  • An element ahs an accept method that can take the visitor as an argument
  • The accept method calls a visit method of the visitor. The element passes itself as an argument to the visit method

 

Benefits

  • Vistor makes adding new operations easy
  • A visitor gathers related operations and separates unrelated ones

 

Liabilities

  • Adding new ConcreteElement classes is hard
    - Each new ConcreteElement gives rise to a new abstract operation on Visitor and a corresponding implementation in every ConcreteVisitor class
  • Breaking encapsulation
    - The pattern often forces you to provide public operations that access an element's internal state, which may compromise its encapsulation

'Computer Science' 카테고리의 다른 글

Facade pattern  (0) 2021.07.29
Things to think about when designing a sofware structure  (0) 2021.07.29
Design Patter - Command Pattern  (0) 2021.07.29
Design Pattern - Strategy Pattern  (0) 2021.07.29
Design Pattern (2)  (0) 2021.07.28

댓글