Swift Programming - Zero To Hero - Part Nine


This is part nine of Swift article series "Swift Programming - Zero to Hero". In this article, we will learn a brief of Design Patterns in Swift programming. The following is the flow of this write-up.
  • Background
  • What is Design Pattern
  • Delegate Pattern
  • Singleton Pattern
  • Model View Controller
  • Conclusion

While developing an application, I met some difficulties in software design. Thus, my Project Manager told me to learn about Design Patterns. After learning this, I felt very easy to develop an application and also designed a software. So, that made me share some knowledge about design patterns. I am not an expert in design patterns but I have shared some brief information about it here. This will be more useful for those who wish to learn design patterns.

Design Pattern

In software design, there may be some problems occurring very commonly. To resolve these, we use Design Patterns. Design Patterns are reusable solutions to common problems in software designing. It is a template which helps developers to write the code which is easy to understand and reuse.

Delegate Pattern

Delegate pattern is very simple and more powerful design pattern, in which, one object in a program acts on behalf of another program. This pattern is mostly used in Swift programs. A class can delegate a part of its responsibilities to an instance of another class. By using Protocol, the design patterns are implemented. This protocol has a set of methods with no definitions.

Singleton Pattern

Singleton Pattern provides a globally accessible, shared instance of an object. This pattern is simple to understand and very useful pattern. There are times that you want to make sure only once the instance of a class is instantiated and that your application only uses that instance. That's the primary and only goal of the singleton pattern. There are three points to remember when we use a singleton.
  1. A singleton has to be unique. Till the application exists, there should be only one instance.
  2. The Initializer of the Singleton should be Private in order to maintain the uniqueness of singleton.
  3. Singleton Patterns should be thread-safe in order to have only one instance.
Begin unique and initializing in one place is easy for an app.

MVC Pattern

The most powerful pattern for designing an app is MVC Pattern. MVC stands for Model-View-Controller. It is built on the top of Object-Oriented Programming. If anyone is good in OOP, then MVC is very simple to learn and apply for application development design.
  1. Model (M) - It handles the logical part of the application.
  2. View (V) - It handles the User Interface of the application
  3. Controller (C) - It handles all the logic that goes between Model and View. It transfers the messages between View and Model and vice-versa.

In this article, I have shared some basic knowledge of Design Patterns. Hope this was very useful. Please share your comments and feedback in the comments section.