Photo by Ant Rozetsky on Unsplash

Creating Objects without Specifying their Concrete Classes

Swift — Problems Catalogue #1

Alex Ilovan
Published in
4 min readJun 3, 2022

--

Problem Definition:

Consider the following scenario. You have to implement a rocket management system with all the necessary facilities from building the rocket to refuelling it and finally launching it. BUT then you need to use the same facilities for other types of rockets with different shapes, sizes and colours. Things quickly escalate in complexity and you want something to clear the air.

Problem Solution:

Factory Method — it’s a creational design pattern which solves the afermentioned problem of creating objects without specifying their concrete classes.

Basically, it defines a method, which can be used for creating objects instead of a direct constructor call. Subclasses can override this method to change the class of objects that will be created.

Real-World Usage:

Let’s create a rocket App:

Considering that there are different kinds of rockets, the wisest decision would be to create a Rocket protocol. For the sake of simplicity, let’s add three variables that revolve around the fuel stats of the rocket and three methods for now:

  • The first one is for updating the fuel tank with a certain amount of our fuel of choice. Of course, considering that we want to change the available fuel, we need to mark the func as a mutating one.
  • The second one is for checking our subsystems prior to launch. That’s a very important step 🧐
  • The third one is for executing the launch sequence of the rocket in our specified time.

*also we need to add an enum for specifying our type of fuel.

Rocket Protocol

Alsssoooo, let’s add some default implementations for two of our methods. We don’t worry about the business logic here. These are optional. You can configure them however you feel like it. (Just please don’t overflow the fuel tank 😓)

Default implementations for Rocket Protocol

Now, we’re ready to create the factory for our Rocket..but wait, our business is expanding much faster than we thought and now we’re also selling weather control devices. Let’s make two or multiple factories 😮

We can start by creating a Protocol called RocketFactory and from there we can create all sorts of factories that conform to it.

Rocket Factories

The next thing on the list is to create the actual Rocket classes that conform to the Rocket Protocol. Let’s create two types of them, the Space Rocket and a Weather Rocket for hail control. By subclassing the Rocket protocol we can adjust its methods accordingly to its specifications. It would be quite hard to send a Weather Rocket to space…just saying 😰

Different types of rockets

From here we can create even Toy Rockets, Two-barrel Rockets, and even Inter-Space Rockets and we can configure and tweak however we want the creation of the Rocket Object by creating them without specifying their concrete class.

Last but not least, let’s put everything together and launch our rockets. For comparison, I have added in the image below the two methods to see how easy it is to create just one method for launching that can be used both for big space rockets and for weather control ones. ✨

RocketApp Definition

From this point on, the sky is the limit 🚀 well…almost.

Of course, this design pattern has its limitations but used in moderation, it’s a great tool in our development toolbox.

This is the first article in the series that I plan to write called the Swift Problems Catalogue in which I’ll tackle general software development problems. The aim is to have a quick reference guide that can be easily accessed when having a design/algorithm dillemma.

Let me know what you think and don’t be shy to share where and when this pattern simplified your coding experience 🎶

--

--

Alex Ilovan
salt&pepper

🚀Head of Mobile Development @S&P 💻Comp. Engineer 🪐Engineering Manager. You can visit at: https://www.linkedin.com/in/alex-ilovan-129161b4/