Open Closed Principle: O in SOLID — Practical with Swift
<p>Open-closed Principle (OCP) is one of principle inside SOLID. OCP states that objects or entities should be <strong>open for extension</strong> but <strong>closed for modification</strong>. This means that when there is a new requirement in a system, we should not modify the existing code, but rather extend it.</p>
<p>This can be a bit confusing. How do we create a class that cannot be modified? How do we do that?</p>
<blockquote>
<p>I documented a complete theoretical explanation of this principle in my previous article titled: <a href="https://medium.com/@mrezkys/open-closed-principle-o-in-solid-theory-fbd164d6288" rel="noopener">Open Closed Principle: O in SOLID — T</a>heory. You can access it to learn the theoretical side.</p>
</blockquote>
<p>In this article, I will discuss the practical part. Code by code and line by line.</p>
<h1>Study Case</h1>
<p>Imagine you are hired to work at one of the e-commerce companies. Currently, they are expanding the scale of their applications. One of them is updating their payment system. In the past, they only accepted payments via debit and credit cards. Now they want to add a Payoneer (online payment providers) as payment feature. Here is a rough overview of the existing system.</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*1v7jZAF74zLtjxjYw4e4Dg.png" style="height:308px; width:700px" /></p>
<p>If we refer back to the Open-Closed Principle, the code above clearly violates the principle. Why? Because when we want to add another payment option, we will definitely have to modify the existing code by adding another case.</p>
<p><a href="https://mrezkys.medium.com/open-closed-principle-o-in-solid-practical-with-swift-ddcc3c5e5778">Click Here</a></p>