Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley Professional Computing Series)

Author: Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Grady Booch
4.5

Comments

by anonymous   2018-08-01
I'm talking about the Factory Method pattern from the Gang of Four [Patterns book](https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8) here. There might be another Factory Method pattern in C++ literature but that's not what a majority of developers will spontaneously relate to.
by anonymous   2018-08-01

The intent of the Visitor pattern as stated by the Gang of Four is 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 [1].

Consider a compiler that represents programs as abstract syntax trees. It will need to perform operations on abstract syntax trees for “static semantic” analyses like checking that all variables are defined. It will also need to generate code. So it might define operations for type-checking, code optimization, flow analysis, checking for variables being assigned values before they’re used, and so on. Moreover, we could use the abstract syntax trees for pretty-printing, program restructuring, code instrumentation, and computing various metrics of a program [1].

The problem here is that distributing all these operations across the various node classes leads to a system that’s hard to understand, maintain, and change. It will be confusing to have type-checking code mixed with pretty-printing code or flow analysis code. Moreover, adding a new operation usually requires recompiling all of these classes. It would be better if each new operation could be added separately, and the node classes were independent of the operations that apply to them [1].

Check out my blog for more details here [2]

[1] https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8

[2] https://medium.com/@ravindraranwala/visitor-design-pattern-e282dd1a5625