Observer Pattern in Swift iOS

Tram Ho

Difficulty: Beginner | Easy | Normal | Challenging
Xcode 14.0.1
Swift 5.7


1. About

In this article, I will introduce to you a Fundamental Design Pattern – Observer Pattern. So what is the Observer Pattern like, its application and how to implement it?


Observer Pattern is one of the most common patterns in iOS programming. I will show an example of Observer Pattern first, then we will dive into its essence!!!

2. Example of Observer Pattern

  • First, I have a class named AppCoordinator . This class takes responsibility for navigating/managing the root view of our application

=> If you find it difficult to understand this class, you can also replace AppCoordinator with AppDelegate . That means I will register Notification.Name (“LoginSuccessNotification”) in AppDelegate


  • The next part I will have a class LoginViewController . Assuming we are at the Login screen, if we are successfully logged in our app will navigate to the Home screen.

=> Since this is an example, I do not inherit UIViewController for LoginViewController
=> So when we log in successfully. The LoginViewController class will send the Notification.Name (“LoginSuccessNotification”) notification .
And this notification has been registered to the action in AppCoordinator . This means that AppCoordinator’s loginSuccess () function will be called and print the line ” Change root view to Home screen for example


It’s a good example of the Observer Pattern that we often use in real projects. So now let’s go through a little theory about it.

3. What is the Observer Pattern?

  • The Observer Pattern belongs to the Behavioral Design Pattern group. This pattern will provide a registration mechanism for the object to issue notifications to other necessary objects when it (the registered object) has changed.
  • The observer pattern lets one object observe changes on another object – Design Pattern by Tutorials
  • Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing. – Dive Into Design Pattern

Those are some definitions of the Observer Pattern. I have a diagram of the Observer Pattern as follows: Observer_Diagram.png

  • Observer Pattern it will include 2 main parts:
    • Subscriber : A class that observes (observer) other objects, which is also the place to receive certain updates. In the 2nd example, our Subscriber is the AppCoordinator class. This is where our entire app looks, waiting to receive a notification calling Notification.Name (“LoginSuccessNotification”) (meaning AppCoordinator is receiving updates)
    • Publisher : An observable class that sends updates to Subscribers . In the example item 2, Publisher is the LoginViewController class. Because LoginViewController is observed by AppCoordinator , and LoginViewController has successfully sent login notification Notification.Name (“LoginSuccessNotification”) to AppCoordinator

=> Note : The value in the main diagram is the Notification.Name (“LoginSuccessNotification”) notification, everyone

4. When to use the Observer Pattern?

  • Observer Pattern is used when we want one object (object) to change, then one (or more) other objects are changed.
  • Use the observer pattern whenever you want to receive changes made on another object. – Design Patterns by Tutorials
  • Use the observer pattern when changes to the state of one object may require changing other objects, and the actual set of objects is unknown before or changes dynamically. – Dive Into Design Pattern
  • Use the pattern when some objects in your app must observe others, but only for a limited time or in specific cases. – Dive Into Design Pattern

5. Pros and cons

5.1. Advantage

  • True to the O principle in SOLID (Open/Closed Principle). Because when we add a Subscriber class, we don’t need to edit anything in the Publisher class. For example, in item 2, when the login is successful, I want to add a message to welcome the user (besides the notification to change the existing root view), then I just need to declare another notificaton, without editing anything. in class LoginViewController
  • We can add/remove these associations during RUNTIME . That means I can add/remove a notification while using the app.

5.2. Defect

  • Subscribers are notified in no particular order. For example, when the login is successful, I can’t manage the transfer of the root view first, or the message to welcome the user to log in first.
  • If we forget to delete Subscribers that are no longer in use. It can cause memory leak for our application. For example when we add notifications ( NotificationCenter.default.addObserver ) but we forget to remove those notifications ( NotificationCenter.default.removeObserver )

6. Conclusion

  • So we have learned what the Observer Pattern is and how to implement it through this blog.
  • If you have any questions or have a way to optimize your Observer Pattern example, don’t hesitate to comment. Thank you so much = Thank a lot = Thank you very much.
  • I have one more example of the Observer Pattern. You can refer to the link

Also can read some more examples of Observer Pattern:

Reference

Share the news now

Source : Viblo