Wanna Know How to Change Your Whole App with 1 Line of Code?

Matthew Campbell, August 10th

You can change the look and behavior of your app quickly using one of my favorite design patterns – the abstract factory!

Abstract Factory? Ok – so what is that? An abstract factory is a design pattern, which is a way to organize objects over your entire application. You may have heard of the Model-View-Controller (MVC) since this is another popular design pattern that is used throughout iPhone development.

To help put the abstract factory design pattern into perspective, let’s review what the MVC pattern is. As you might guess from the name, the MVC pattern means keeping your app’s View (UI), your app’s Model (data) and your Controller (mediator) logically separated.

This is not a hard and fast rule in the same way as the syntax of our programming languages, but more of a rule of thumb. There are other types of design patterns – I was introduced to another pattern called the factory pattern after reading the famous book on the subject, Design Patterns Explained.

The factory pattern has to do with how you create objects in your system. You can think of it as an assembly line which on has one function – to return objects to the rest of the program.

How here how a factory works:

An abstract factory pattern is a refinement of the factory pattern above. What we are trying to do with the abstract factory is to not only centrally manage the production of objects, but we want to be able to change how objects are produced at our whim.

eBookAd1.001.jpg

This is how you can change your entire program in one line of code. If you can change the objects an application uses you can change everything about that application.

What we need to do is:

That last bit may need a little more explanation. If you design your app with a factory in mind you will need to define what a factory is (essentially what functions return what objects). Each component in your system will expect something that fits your factory definition to be available. But, HOW you fulfill this definition depends on the object in play.

Any object may be your factory object as long as it fulfills the requirements of your definition. If you wanted to you could have two+ variations of your app available and users could choose which one to launch. Your app will look and feel differently because different factories will be available to return different objects.

I have a confession, while I have used this pattern in other development areas I have never really applied it to iPhone programming. This week I am going to try each day to implement this way of doing this in an iPhone app to see if it is worth it.

Stay tuned to this blog to see my progress in implementing this design pattern!

PS:  lf you have any ideas about what to expect with applying an abstract factory pattern to an app let me know in the comments!