<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vishakh on Medium]]></title>
        <description><![CDATA[Stories by Vishakh on Medium]]></description>
        <link>https://medium.com/@vishakhashokkumar?source=rss-73185b0fa0aa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RXYPDtqdRIETgtgP</url>
            <title>Stories by Vishakh on Medium</title>
            <link>https://medium.com/@vishakhashokkumar?source=rss-73185b0fa0aa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:03:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vishakhashokkumar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Protocol Oriented Programming in Swift]]></title>
            <link>https://medium.com/@vishakhashokkumar/protocol-oriented-programming-in-swift-4dc3300d5103?source=rss-73185b0fa0aa------2</link>
            <guid isPermaLink="false">https://medium.com/p/4dc3300d5103</guid>
            <category><![CDATA[uikit]]></category>
            <category><![CDATA[protocols-in-swift]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Vishakh]]></dc:creator>
            <pubDate>Thu, 05 Jun 2025 08:57:05 GMT</pubDate>
            <atom:updated>2025-06-05T08:57:05.254Z</atom:updated>
            <content:encoded><![CDATA[<h3>🚀 Introduction</h3><blockquote><em>Swift encourages </em><strong><em>Protocol-Oriented Programming (POP)</em></strong><em> over classical Object-Oriented Programming (OOP). This article explores why protocols are powerful and how to use them effectively — from basics to advanced usage with handy concepts like </em>Opaque Types<em> and </em>Associated Types<em>.</em></blockquote><h4>🧱 1. OOP in Swift: The Old Way</h4><pre>class Animal {<br>    func makeSound() {<br>        print(&quot;Some sound 𝇉&quot;)<br>    }<br>}</pre><pre>class Dog: Animal {<br>    override func makeSound() {<br>        print(&quot;Bark&quot;)<br>    }<br>}</pre><blockquote><em>Inheritance creates a tightly coupled hierarchy. What if we want a </em><em>Dog to also be a </em><em>Pet, </em><em>Guard, or </em><em>Sniffer? Swift doesn&#39;t support multiple inheritance — this is where protocols shine.</em></blockquote><p>🔁 <strong>How protocol helps</strong>:<br>You can define behavior like SoundMaking as a protocol and apply it to multiple unrelated types (structs, enums, or classes) <strong>without inheritance</strong>.</p><h4>🔗 2. Problems with Deep Inheritance Trees</h4><pre>class View {<br>    // Base class with 1000 lines<br>}</pre><pre>class Button: View {<br>    // 300 more lines<br>}<br>class SubmitButton: Button {<br>    // Overrides behavior<br>}</pre><blockquote><em>A small change in </em><em>View can cause </em><em>SubmitButton to break unexpectedly. This tight coupling is dangerous and hard to scale.</em></blockquote><p>✅ <strong>Why protocols are better:</strong></p><blockquote>With deep hierarchies, code reuse becomes fragile.</blockquote><h4>💬 3. Protocol Properties</h4><pre>protocol Nameable {<br>    var name: String { get set }<br>    var designation: String { get }<br>}</pre><pre>struct College: Nameable {<br>    var name: String<br>    let designation: String<br>}</pre><p>✅ <strong>Why use protocol here</strong>:</p><ul><li>Allows any struct, class, or even enum to describe <strong>nameable entities</strong>.</li><li>Encourages consistency across models (e.g. User, Company, Team can all be Nameable).</li></ul><h4>🔄 4. Dependency Injection with Protocols</h4><pre>protocol DataManager {<br>    func fetchData()<br>}</pre><pre>class NetworkManager: DataManager {<br>    func fetchData() {<br>        print(&quot;Fetching data...&quot;)<br>    }<br>}<br>class MyViewModel {<br>    var dataManager: DataManager<br>    init(dataManager: DataManager) {<br>        self.dataManager = dataManager<br>    }<br>    func loadData() {<br>        dataManager.fetchData()<br>    }<br>}</pre><p>✅ <strong>Why protocol is better</strong>:</p><ul><li>MyViewModel doesn’t care how data is fetched.</li><li>You can inject a <strong>mock or different implementation</strong> of DataManager (e.g. for unit testing).</li><li>Enables <strong>loose coupling</strong> and follows <strong>SOLID</strong> principles (especially Dependency Inversion).</li></ul><h4>🧃 5. Mutating Methods in Protocol</h4><pre>protocol Togglable {<br>    mutating func toggle()<br>}</pre><pre>struct Switch: Togglable {<br>    var isOn = false<br>    <br>    mutating func toggle() {<br>        isOn.toggle()<br>    }<br>}</pre><p>✅ <strong>Why protocol is better</strong>:</p><ul><li>Works beautifully with value types (struct), making state changes predictable.</li><li>Keeps behavior encapsulated inside a reusable protocol.</li></ul><h4>🧬 6. Protocol Inheritance</h4><pre>protocol Identifiable {<br>    var id: Int { get }<br>}</pre><pre>protocol User: Identifiable {<br>    var name: String { get set }<br>}<br>struct Employee: User {<br>    var name: String<br>    var id: Int<br>}</pre><p>✅ <strong>Why this is powerful</strong>:</p><ul><li>Combines multiple contracts: User must now also be Identifiable.</li><li>Promotes <strong>code reuse</strong> and clarity (no redundant definitions).</li></ul><h4>🔗 7. Function Parameters via Protocol Composition</h4><pre>protocol Runnable {<br>    func run()<br>}<br>protocol Jumpable {<br>    func jump()<br>}</pre><pre>func perform(action: Runnable &amp; Jumpable) {<br>    action.run()<br>    action.jump()<br>}</pre><p>✅ <strong>Why use protocol composition</strong>:</p><ul><li>Accepts <strong>multiple capabilities</strong> without inheritance.</li><li>Cleaner than making a superclass that tries to “do everything”.</li></ul><h4>🖍️ 8. Protocol Extensions</h4><pre>protocol Drawable {<br>    func draw()<br>}</pre><pre>extension Drawable {<br>    func draw() {<br>        print(&quot;Drawing default shape&quot;)<br>    }<br>}<br>struct Circle: Drawable {} // Gets default implementation</pre><p>✅ <strong>Why protocol + extension &gt; base class</strong>:</p><ul><li>You provide <strong>default behavior</strong> only when needed.</li><li>Types can <strong>override selectively</strong>, unlike class inheritance which forces full override.</li></ul><h4>🧪 9. Associated Types</h4><pre>protocol Stack {<br>    associatedtype Item<br>    mutating func push(_ item: Item)<br>    mutating func pop() -&gt; Item?<br>}</pre><p>✅ <strong>Why this rocks</strong>:</p><ul><li>Enables <strong>type-safe collections</strong> without being tied to Array, Set, etc.</li><li>Stack can be of Int, String, CustomModel, etc.</li></ul><h4>📦 10. Existential Types (any)</h4><pre>func printStack(_ stack: any Stack) {<br>    // Accepts any stack <br>}</pre><p>✅ <strong>Why useful</strong>:</p><ul><li>Enables passing around <strong>heterogeneous</strong> types with a shared protocol.</li><li>Think of this like saying: “any type that behaves like a Stack.”</li></ul><h4>🔁 11. Self vs self</h4><pre>protocol Cloneable {<br>    func clone() -&gt; Self<br>}</pre><p>✅ <strong>Why </strong><strong>Self matters</strong>:</p><ul><li>Self refers to the <strong>type conforming to the protocol</strong>.</li><li>Return type Self allows <strong>chainable APIs</strong> or <strong>fluent interfaces</strong>.</li></ul><h4>🧙‍♂️ 12. Opaque Types (some)</h4><pre>func createShape() -&gt; some Shape {<br>    return Square()<br>}</pre><p>✅ <strong>Why opaque types</strong>:</p><ul><li>Hides concrete type, but <strong>preserves type safety</strong>.</li><li>Useful for returning protocol types <strong>without exposing implementation</strong>.</li></ul><h3>✔️ Conclusion: Embrace Protocol-Oriented Thinking</h3><p>Swift was designed with <strong>protocols at its core</strong>. While classical OOP still has its place, <strong>Protocol-Oriented Programming (POP)</strong> enables:</p><ul><li>✅ <strong>Loosely coupled, testable architecture</strong></li><li>✅ <strong>Behavior sharing across unrelated types</strong></li><li>✅ <strong>Powerful composition over rigid inheritance</strong></li><li>✅ <strong>Default implementations with protocol extensions</strong></li><li>✅ <strong>Cleaner, scalable code using associated types and generics</strong></li></ul><p>By choosing protocols over base classes, you make your Swift codebase <strong>more flexible, reusable, and future-proof</strong>. Whether you’re building apps in UIKit, SwiftUI, or server-side Swift — embracing protocol-oriented principles will elevate your architecture and maintainability.</p><blockquote><em>🔁 Shift your mindset from “what should this inherit?” to “what behavior should this conform to?”</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4dc3300d5103" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>