Understanding Refactoring OOP to Protocol Oriented Programming in Swift
Автор: vlogize
Загружено: 2025-04-04
Просмотров: 1
Описание:
Explore the transition from Object-Oriented Programming to Protocol-Oriented Programming in Swift, focusing on practical examples and solutions for ViewControllers.
---
This video is based on the question https://stackoverflow.com/q/69182307/ asked by the user 'jackbob' ( https://stackoverflow.com/u/16450719/ ) and on the answer https://stackoverflow.com/a/69183029/ provided by the user 'Rob Napier' ( https://stackoverflow.com/u/97337/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Refactoring OOP to Protocol Oriented Programming
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Refactoring OOP to Protocol Oriented Programming in Swift
In the world of iOS development, there’s an ongoing debate regarding the use of Object-Oriented Programming (OOP) versus Protocol-Oriented Programming (POP). With Swift emphasizing the latter, many developers are eager to refactor their existing code to embrace this modern paradigm. However, the transition can be confusing, especially when dealing with situations where two view controllers share similar functionality.
The Problem at Hand
Let's break down the problem faced by a developer who has two view controllers: FirstViewController and SecondViewController. Both of these view controllers share similar IBOutlets and functionality, with SecondViewController containing one additional function and an additional outlet.
The question arises: Is using inheritance better than protocols, given the overlapping functionality? The developer is also curious about how to implement a protocol while avoiding code duplication, particularly for the IBOutlets involved.
Exploring Inheritance
The traditional approach to solve this problem is to use inheritance, where SecondViewController inherits from FirstViewController. Here’s how this typically looks:
[[See Video to Reveal this Text or Code Snippet]]
With inheritance, all the shared functionality and outlets are easily accessible in SecondViewController. However, relying solely on inheritance can lead to issues, especially as the application grows in complexity.
Transitioning to Protocol-Oriented Programming
Protocol-Oriented Programming offers an alternative that can reduce code duplication and increase reusability. Here’s how one might approach this refactoring using protocols:
Define Protocols for Shared Functionality
First, we can define a protocol that encompasses the shared functionality:
[[See Video to Reveal this Text or Code Snippet]]
Implementing the Protocol in ViewControllers
Next, we will apply the protocol to FirstViewController and create a new SecondViewController:
[[See Video to Reveal this Text or Code Snippet]]
Key Considerations
However, there are some crucial points to remember when considering this approach:
Shared Code vs. Shared Concepts: It's essential to identify whether your view controllers are truly related or merely share some functionalities. If they don't share fundamental similarities, it may not make sense to force them under a single inheritance structure or protocol.
IBOutlets in Protocols: Unfortunately, protocols cannot inherit IBOutlets. Therefore, any UI components that need to be used must be specified separately in each view controller. This could lead to some repetitive code, especially if the UI components are highly similar.
Conclusion
Transitioning from OOP to POP in Swift is less about enforcing protocol conformity and more about recognizing when it's appropriate to abstract common functionality. If FirstViewController and SecondViewController evolve together or share fundamental concepts, consider inheritance. Conversely, if they merely happen to share some behaviors, extracting shared code into protocols is a valuable approach. Embrace the principles of DRY (Don’t Repeat Yourself), focusing on concepts rather than code duplication, for a cleaner and more maintainable codebase.
In summary, always evaluate your architectural choices based on the relationships and requirements of your components. This will guide you to the right decision—be it Protocol-Oriented or Object-Oriented Programming.
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: