swift面向协议编程的根本原因在于值类型的存在;面向对象必须要有引用类型的支持;

Protocol Oriented approach was introduced to resolve some issues in programming and it also differs in various scenarios when compared to Object-Oriented programming. So let’s dive into the topic.

What is Protocol Oriented Programming?

Protocols basically serve as a blueprint like classes rather than a fully functional types for methods, properties, and other requirements. This is done to perform a piece of functionality and behaves like an interface and tells what to implement. It is a powerful feature of Swift language. Apple tells us:

                                                  “Don’t Start with the Class, Start with the Protocol”

Why We Need POP?

When we design any software system, then we figure out the general requirements that satisfy the given system. We, even model the relationship b/w the UI elements such as buttons, view, images etc. for which we generally start with superclass and model the relationships using inheritance.

In Swift, as we have the concept of POP so, we can even start with protocols and can model the relationships as protocol implementations. In the protocol oriented approach, we model our system with a protocol, rely on the concepts like:

  • Protocol Extensions
  • Protocol Inheritance
  • Protocol Composition 

In Swift, we use structs, enums, and tuples rather than working only with classes since, Value Semantics are preferred over Reference Types.

Also, there could be various cases where OOP is not the best solution to implement. Let’s check and figure out the drawbacks in Object-Oriented concept. We know that Inheritance is one of the most important concepts in OOP but, inheritance doesn’t work for value types and modern languages like Swift prohibits to support the feature of multiple inheritances due to complexities and value types is the first citizen of Swift. So, POP does a great job by providing the ability to add multiple abilities to the class, struct, and enum with protocols and extensions that supports multiple implementations while we code.

Other benefits with Protocol implementations are:

  • Swift checks whether the requirements of the protocol are full-filled or not for the classes we are implementing at compile-time, so, this helps us to find out if there were any issues or bugs in code even before we ran our program.
  • Also, protocols bring more abstraction mechanism than classes do in Swift.
  • We’re not restricted to only use classes since any type, including value types, can implement a protocol.
  • A type can implement multiple protocols.
  • We can create as many protocols as we need.

Before I talk about POP in detail and how to use and implement it, we must understand the basics right, so let us focus on Swift Types first. Here is the diagrammatic representation of Swift Types that we have.

Well, here I am interested in the behavior of Classes and Structure, one belongs to the reference type and other to value type and observe how objects behave through an example.

And, this is really important to understand and why is it so? If you have a very complex hierarchy and you set something in one part of the code, it should not have any wrong consequences in the other part. So, the lesson is:

  • Classes use reference i.e if you set something to other it is not a copy instead it is a reference.
  • Whereas, in value type such as structures, passes things as a copy, not as a reference.

Conclusion

Apple, always recommends going with the value type while we program in Swift. Structures are preferable when we have a small copy of data and it is much safer than having multiple references to the same objects in our code. The concept becomes more important when we talk about variable and passing their value. In Swift, we work in a multi-threaded environment and if we have copies of variable then, we don’t need to worry about them in the other place where value may get change. So, using structure is advised by Apple and Protocol-Oriented Programming serves better abstraction with structures.

Using Protocols and it’s extensions features, we can really ignore/avoid for making a huge superclass and then inheriting functionality from there. So, Hope it is now clear to you what is POP and why Apple use this.

Note: This does not mean that we don’t use Class, there are some situations where classes are the only option left for our implementation.

In the other part of this article, we will see how to implement and use protocols, protocol extensions, Generics with the protocol.

https://www.technotification.com/2018/08/protocol-oriented-programming-swift.html

An Introduction to Protocol Oriented Programming in Swift的更多相关文章

  1. 编程范式 --- 面向协议编程(Protocol Oriented Programming,简称POP)

    面向协议编程(Protocol Oriented Programming,简称POP) 是Swift的一种编程范式,Apple于2015年WWDC踢出 在Swift的标准库中,能见到大量POP的影子 ...

  2. Does Swift support aspect oriented programming?

    The foundation of Aspect Oriented Programming is the intercept pattern. We start with a crosscutting ...

  3. Aspect Oriented Programming

    AOP(Aspect Oriented Programming),面向切面编程(也叫面向方面)是目前软件开发中的一个热点.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...

  4. Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP

    http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Downl ...

  5. AOP Aspect Oriented Programming

    原理AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bus ...

  6. AOP(Aspect Oriented Programming),即面向切面编程

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION) 学习目标 ...

  8. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切 代码工程地址: https://git ...

  9. 关于面向切面编程Aspect Oriented Programming(AOP)

    最近学到spring ,出来了一个新概念,面向切面编程,下面做个笔记,引自百度百科. Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实 ...

随机推荐

  1. _ViewStart文件应用

    在这篇<MVC母版页_Layout.cshtml>http://www.cnblogs.com/insus/p/3380419.html中,把一些已经存在的视图或是新产生的视图加入母版中. ...

  2. java基础-面向对象的思想

    一.什么是面向对象 面向对象是一种思想,在java中通常我们会说一句话一切事物即对象.而面向对象到底是怎么回事呢?这里我从人们对问题的思考来阐述,人在思考的一个问题的时候比如在解决一个数学问题的时候我 ...

  3. 使用 ActiveMQ 实现JMS 异步调用

    目录 简介 启动 ActiveMQ 服务器 查看控制台 ActiveMQ 的消息通道 Queue Topic 比较 开发生产者和消费者 开发服务端(消费者) 开发客户端(生产者) 参考 简介 服务之间 ...

  4. Dagger2 单例

    解锁Dagger2使用姿势(二) 之带你理解@Scope Dagger2从0基础使用,到单例注入的正确姿势 Android之dagger2的简单运用和详细解读(入门)

  5. Codeforces729D(SummerTrainingDay01-F)

    D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Angular4.+ ngx-bootstrap Pagination 自定义分页组件

    Angular4 随笔(二)  ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...

  7. 在微信小程序中引入 Iconfont 阿里巴巴图标库

    小程序的代码包不能超过4M,为了压缩代码包的大小,可以通过第三方链接引入图标资源 Iconfont 无疑是最常用的第三方图标库,这里介绍一下在微信小程序引入 Iconfont 的方法 一.下载图标 首 ...

  8. 设计模式(23)--Visitor--访问者模式--行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义:    访问者模式是对象的行为模式.访问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦 ...

  9. js 动态声明变量

    var  object = {}; for(var i=0; i<5; i++){ object['attr'+i] = i; }

  10. Android4.4中jni的native的方法无法找到的解决方案

    1.禁用代码混淆功能 LOCAL_PROGUARD_ENABLED:= disabled 2.修改混淆规则,对于类的native 方法 不要进行混淆