Command, interpreter, iterator, and visitor patterns

The command, interpreter, and visitor patterns are grouped in this section only because we have already covered their use cases earlier in this book.

The command pattern is used to parameterize actions that are to be performed. In Chapter 9, Miscellaneous Patterns, in the Singleton type dispatch pattern section, we explored a use case where the GUI invokes different commands and reacts to specific actions that the user has requested. By defining singleton types, we can leverage Julia's multiple dispatch mechanism to execute the proper function. We can extend this to new commands by simply adding new functions that take new singleton types.

The interpreter pattern is used to model an abstract syntax tree for a particular domain model. As it turns out, we have already done this in Chapter 7Maintainability Patterns, in the Domain-specific language section. Every Julia expression can be modeled as an abstract syntax tree without any additional work, and so we can develop a DSL using regular metaprogramming facilities, such as macros and generated functions.

The iterator pattern is used to iterate over a collection of objects using a standard protocol. In Julia, there is already an officially established iteration interface that can be implemented by any collection framework. As long as an iterate function is defined for a custom object, the elements in the object can be iterated as part of any looping construct. More information can be found from the official Julia reference manual.

Finally, the visitor pattern is used to extend functionalities of an existing class in the OOP paradigm. In Julia, adding new functions to an existing system can be done easily via an extension of generic functions. For example, there are many array-like data structure packages in the Julia ecosystem, such as OffsetArrays, StridedArrays, and NamedArrays. All of these are extensions to the existing AbstractArray framework. 

We are now finished with behavioral patterns. Let's move on and take a look at the last group—structural patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.157