4. Source Code

Let’s study Modlizer by looking on some actual code. This article embodies only the Swift version of the design pattern.

All the code was extracted from a project I worked on, called AdPacer, which is an outstanding app if you’re a marketing person. We’ll look only on the login page’s source files.

4.1 Hierarchy

First off, the hierarcy, inside the folder (or group, the way Xcode calls it):

The hierarcy

It may not be visible in this minimalist example, but the order is the following:

NavigationBarController ViewController (base class) Alphabetically arranged optional slices Data Network

This is the way I chose to order them, but it isn’t by any means a must. It just seemed more practical to have Data and Network at the bottom, so they are easy to access.

4.2 APLoginViewController

Naturally, this is the place where we subclass UIViewController and override the default methods.

APLoginViewController.swift

Notes:

This is the core view controller of the login page. I won’t go further into explaining IBOutlets and other concepts.

If you’re wondering where is the NavigationBarController, well, there’s simply no customization (color, text) done to the navigation bar here.

As you might’ve guessed, I’m all about commenting my code. Placing @location comments in the well-known viewDidLoad method enhances the cohesion of the project, so that we’re able to clearly identify in which slice the initial setup of the view controller occurs.

comments in the well-known method enhances the cohesion of the project, so that we’re able to clearly identify in which slice the initial setup of the view controller occurs. Notice the appendTextField ! It indicates that we execute some initialization code inside the APLoginTextFieldController .

Bottom Line: the aim is to use the APLoginViewController for subclassing the mother UIViewController and use it only for that. No other delegates or custom methods here, only the core of the core.

4.3 APLoginButtonController

The key factor here is the extension, because it enables us to access the code (apart from the fileprivate properties or methods) from any other slice. Do you start to see the beauty of Modlizer? 🙌

APLoginButtonController.swift

Notes:

Observe the @location comments , which explictly describe the source of those methods.

comments which explictly describe the source of those methods. didTapForgotPasswordButton is defined here and it’s not an IBOutlet. Later, you’ll see that this it’s assigned as a tap gesture recognizer’s selector, but it belongs in this slice due to the fact that, at the end of the day, when you tap something, that’s a button!

Bottom Line: Anything related to buttons should be kept here. Although there isn’t any snippet of code which initializes stuff, it doesn’t mean one can’t do it. For instance, there could be an appendButton method similar to appendTextField .

4.4 APLoginTextFieldController

Highly similar (in terms of slice structure) with the previous one.

APLoginTextFieldController.swift

Notes:

Observe the appendTextField called from the class slice.

called from the class slice. Look at the #selector(APLoginViewController.didTapForgotPasswordButton) , which bridges to the APLoginButtonController .

, which bridges to the . didTapBackground was implemented here because the background is not actually a button and the action is solely connected with the behaviour of the text fields.

Bottom Line: Anything related to text fields should be kept here.

4.5 APLoginData

Being a fundamenal slice, it can have two parts:

Class Extension

Both are optional and it really depends on the project if you implement one or another.

The quick explanation is that a class defined here will act as a model for the data in the view controller (eg: an array of image URLs). The view controller gains access by storing a property of type APLoginData .

Regarding the extension, it acts just as the previous ones.

APLoginData.swift

Notes:

isDataValid verifies the requirements of this specific app.

verifies the requirements of this specific app. If a model is provided, the extension doesn’t need to have any code at all. And viceversa! It’s up to you.

Bottom Line: Anything related to data should be kept here, even if in certain complex situations you can create data subslices. But this is a story for another time.

4.6 APLoginNetwork

Powers the communication with a server, be that an API, a service such as Firebase etc. There’s no class at all to be defined here.

APLoginNetwork.swift

Notes:

Both the request and the response are defined in this slice.

Notice that the login method was previously called from the APLoginButtonController