avatarDheeru N

Summary

The provided web content discusses the implementation of Protocol-Oriented Programming (POP) alongside the Model-View-ViewModel (MVVM) architectural pattern in software development, emphasizing the benefits of enhanced code maintainability, extensibility, and testability.

Abstract

The article delves into the concept of Protocol-Oriented Programming (POP), highlighting its ability to revolutionize code writing by enabling the creation of more extensible and maintainable code through the use of protocols. It explains how POP complements the MVVM architectural pattern, which separates concerns into Model, View, and ViewModel components. The Model represents the domain objects and data management, while the ViewModel acts as an intermediary, preparing observable data for the View and handling view-related logic without being tightly coupled to the View itself. The View remains responsible for presenting the data and updating the UI accordingly. The article also provides sample code snippets illustrating the implementation of POP with MVVM, including the use of FlowController for navigation, Presentable for view initialization, and Configurable for defining view model requirements. The advantages of adopting POP and MVVM are listed, including the ability to adopt protocols across multiple types, extend standard library protocols, and simplify code maintenance, reusability, and testing. Additionally, the article suggests that using MVVM with POP allows for easier changes to core business logic and decouples the view controllers from the specific types of view models they use.

Opinions

  • The author believes that POP transforms the way code is written, making it more adaptable and easier to manage.
  • There is an emphasis on the decoupling of components in MVVM, particularly the independence of the ViewModel from the View, which is seen as an important design principle.
  • The article suggests that POP can significantly reduce complexity in view models, which in turn simplifies the overall codebase.
  • The author advocates for the reusability and maintainability of code as major benefits of combining MVVM with POP.
  • The article promotes the use of AI services like ZAI.chat as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating a belief in the value and performance of such services.

Protocol Oriented Programming (POP) with MVVM

To explain briefly what MVVM and POP is:

Protocol OrientedProgramming:
Protocols are extremely powerful and can transform the way you write code. Here, you’ll explore the ways you can create and use protocols, as well as use protocol-oriented programming patterns to make your code more extensible and maintainable.
Model:
The model is something referred to as the domain object. The model speaks to the actual information and additional data we are managing.
Ex:
1. Card (last4Digits, cardType, cardName, expiryDate and so on)
2. Employee(name, phoneNumber, department, email and so on)
The way to recall with the model is that it holds the data. Business rationale is ordinarily kept separate from the model and typified in different classes that follow up on the model. Now and again, a few models may contain approval.
ViewModel:
ViewModel associates with model and furthermore gets ready observable(s) that can be seen by a View. ViewModel can alternatively give snares to the view to pass occasions to the model.
One of the imperative execution methodologies of this layer is to decouple it from the View, i.e, ViewModel should not know about the view who is dealing with.
The viewModel additionally shows techniques, charges, and different focuses that help keep up the condition of the view, control the model as the consequence of activities on the view, and trigger occasions in the view itself.
View:
The view part in this example is to watch a ViewModel to get information keeping in mind the end goal to refresh UI components as needs are.
MVVM Flow

Now let’s dive into actual coding:

Here is the sample code for different types:

FlowController: This helps your app in controlling the navigation/transition. This is a protocol which will have different presentables. The flowDelegate object helps you showing different views at different times.
Presentable: This helps your code to initialize your view controller and view models.

Configurable: This protocol helps you track of what you need to be in the view model with appropriate getter and setter. This can have the methods(just declarations) which can be used by the view controller or any other places.

Advantages:
1. Protocols can adopt from one to many other protocols.
2. Protocols can be adopted by value types like struct and enum, not just classes.
3. Protocol extensions can not only be used to extend your own protocols, but can extend and provide default behavior to protocols in the Swift standard library, Cocoa, Cocoa Touch, or any third party library.
4. You can have your core business logic in a protocol.
5. Minimizing the complexity in view models.
6. Maintaining and reusing the code is very simple.
7. Writing the test cases is easy.
The other major advantages of using MVVM + POP is:
1. Maintenance and Testing
2. Reusability
3. If you have to change the core business logic entirely for some reason, you can just change the viewModel with a new viewModel.
4. Your viewController will never know what type of a viewModel it is getting.
5. Intializing the code and its maintenance is done at one place.
6. Any screen(presentable) can be called from anywhere in the app.

You can refer to RayWenderlich site to read in depth for a POP specific tutorial here.

Please let me know if you have any kind of questions or concerns or improvements. Good luck.

iOS
Design Patterns
Mobile App Development
Mobile
Swift
Recommended from ReadMedium