Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

The Visitor Pattern (in Swift)

Let’s imagine you are working on an iOS form. Your screen is a UIViewController and you are using the MVVM pattern.

The form is simple enough with just three fields:

  • Name
  • Email
  • Birthdate

Since you don’t want to end up with a massive view controller, you split your code to have a FormViewController with some child view controllers: NameViewController, EmailViewController, and BirthdateViewController. Each of those is backed by its own View Model, so you end up with FormViewModel, NameViewModel, EmailViewModel, and BirthdateViewModel.

To make things nicer, you make all your View Models implement a simple protocol, so each View Model has means to access its child View Models.

Your View Models, then, would look something like this:

At some point, you’ll need to gather the information entered in the form, and send it in the format your backend requires (JSON, URL request parameters, etc).

The question then is: how do you gather that data? Your first approach would be to create a protocol, say JSONSerializable, with a asJSON method, and have all your View Models implement it.

I’d like to introduce you to a better approach using the Visitor Pattern.

The Visitor Pattern allows you to separate two concerns:

  1. Iterating over an object tree.
  2. Performing some operation on each element of that tree.

In order to implement it, we’ll need a new protocol that our visitors will implement:

We also need to extend our ViewModel protocol with one method (we’ll do it by creating a new protocol):

Finally, on each View Model we need to implement the accept method. The accept method has to:

  1. Call visit on the visitor it received as a parameter.
  2. Forward the visitor to its children, so the visitor can continue visiting the object tree.

With this in place, we only need to create a Visitor that does something with the objects in the tree.

Let’s first create a tree we’ll use for test, and a simple visitor that prints the value of each object.

As you can see, we have split the responsibilities of iterating over a tree of objects and actually done something with them.

If we wanted to create a query string or a JSON of the Object Tree, it will only require us to add a new visitor:

And just use it!

Conclusion

By using a Visitor, you split the logic of iterating a (potentially) complex object tree with the actions you want to perform on each of its objects.

Writing each Visitor is easy, and replacing one visitor with a new one is trivial. In our case, if we were to switch from Query String to JSON as the format to send data to the server, we would only need to rewrite that visitor. The rest of the code would remain the same.

Additionally, it provides a clean separation of ownership: if you own the object tree, you can provide the visitor infrastructure for your users (as programming language parsers usually do). If you are the user, you are only responsible of using that visitor infrastructure to operate on the object tree.

If you want to play around, you can download this article as a Swift Playground.

Would you like to know more? Do you need our help? Contact Us!
www.quadiontech.com


The Visitor Pattern (in Swift) was originally published in Quadion Technologies on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on Quadion Technologies, please read the originial post: here

Share the post

The Visitor Pattern (in Swift)

×

Subscribe to Quadion Technologies

Get updates delivered right to your inbox!

Thank you for your subscription

×