为什么swift是面向协议的编程--对面向对象机制的改进

主要目标是提供抽象能力和解决值类型的多态问题

Actually, Abrahams says, those are all attributes of types, and classes are just one way of implementing a type. Yet, they exact a heavy toll on programmers in that they may cause:

  • Implicit sharing, such that if two objects refer a third object, then both can modify it without the other knowing about it. This leads to worarounds such as duplicating the referred object to avoid sharing, which in turn leads to inefficiencies; alternatively, sharing may require using locks to avoid race conditions and this can cause more inefficiency and even lead to deadlocks. What this entails is more complexity, which means more bugs.
  • Inheritance issues: in many OOP language, there can be one just superclass, and it has to be chosen at the very start. Changing it later can be extremely hard. A superclass, furthermore, forces any stored property on derived classes and this can make it complex to handle initialization and not to break any invariants that the superclass require. Finally, there are usually limitations to what can be overridden, and how, or when it should not be, and those constraints are usually left to the docs.
  • Lost type relationship, which ensues from the conflation of interface and implementation. This usually manifests itself through some base class’ methods where no implementation is possible and thus the necessity to downcast to the concrete derived class in that method’s implementation.

According to Abrahams, protocol-oriented programming is a better abstraction mechanism in that it allows:

  • value types (besides classes)
  • static type releationships (besides dynamic dispatch)
  • retroactive modeling
  • no forcing of data on models
  • no initialization burden
  • clarity as to what shall be implemented.

解决的问题:

1、面向对象的问题

2、值类型多态的支持

3、泛型与接口的结合 typeclass类型

一、面向对象的机制不支持值类型

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.

二、协议提供了抽象和多态机制

虚函数表和存在容器的多态支持;

接口函数+泛型编程的多态

三、协议支持值类型和引用类型;

  • 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.

四、协议本身支持类型的组织

五、模拟多重继承

Simply put and without quibbling over inanities, OOP and POP share most of these attributes, with one major exception: Classes can only inherit from one other class while protocols can inherit from multiple protocols.

参考文献:

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

https://www.appcoda.com/pop-vs-oop/

https://www.infoq.com/news/2015/06/protocol-oriented-swift

原文地址:https://www.cnblogs.com/feng9exe/p/10623120.html

时间: 2024-08-29 06:04:22

为什么swift是面向协议的编程--对面向对象机制的改进的相关文章

Swift中面向协议的编程

什么是面向协议的编程? 面向协议的编程,是一种编程范式. 编程范式,是一个计算机科学用语.维基百科中的解释是,计算机编程的基本风格或典型模式.通俗来说,就是解决某一个问题的方法不同方法和思路. 像大家很熟悉的,面向对象编程以及面向过程编程,都是一种编程范式. 面向过程编程,关心的焦点是解决某一个问题需要多少步.而面向对象的编程关心的是解决问题需要多少个对象,以及这些对象之间的组织联系. 解释完了编程范式这个名字含义之后,我们继续回到正题上来. 既然面向协议编程,与面向对象,面向过程一样,是一种编

Swift -POP( 面向协议编程)与OOP

面向协议编程(Protocol Oriented Programming,简称POP),是Swift的一种编程范式,Apple于2015年WWDC提出的,如果大家看Swift的标准库,就会看到大量POP的影子. 同时Swift也是一门面向对象的编程语言(Object Oriented Programming,简称OOP),在Swift开发中,OOP和POP是相辅相成的,任何一方并不能取代另一方. 回顾OOP OOP的三大特性:封装.继承.多态 继承的经典使用场合 当多个类(比如A.B.C类)具有

用 Swift 编写面向协议的视图

我最近做了个 Swift 面向协议编程实践 (POP:boom:) 的演讲.视频还在处理中.另一方面,这是演讲中 POP 视图部分的文本记录,供我和其他任何人作参考! 简单的任务 假设你要写一个由一张图片和一个按钮构成的简单应用,产品经理希望按钮被点击的时候图片会抖动,就像这样: 由于这个动画常常在用户名或者密码输入错误时被用到,所以我们很容易就能 在 StackOverflow 上找到代码 (就像每个好的开发者都会做的一样:grin:) 这个需求最难的地方就是决定实现抖动的代码应该写在哪儿,但

用 Swift 编写面向协议的网络请求

和我一起参加9 月 1 日 - 9月 2 日在纽约举办的 Swift 社区庆典??吧!使用优惠码 NATASHATHEROBOT 可以获得 $100 的折扣! 我最近做了个 Swift 面向协议编程实践(POP??) 的演讲.视频还在处理中.另一方面,这是演讲中 POP 视图部分的文本记录,供我和其他任何人作参考! 普通的配置方式 假设我们要做一款展示全球美食图片和信息的 App.这需要从 API 上拉取数据,那么,用一个对象来做网络请求也就是理所当然的了: struct FoodService

Swift 学习笔记(面向协议编程)

在Swift中协议不仅可以定义方法和属性,而且协议是可以扩展的,最关键的是,在协议的扩展中可以添加一些方法的默认实现,就是在协议的方法中可以实现一些逻辑,由于这个特性,Swift是可以面向协议进行编程的. 扩展协议和默认实现 protocol Record { var wins: Int{get} var losses:Int{get} func winningPercent() -> Double } struct BasketballRecord:Record,CustomStringCon

【C++系列小结】面向过程的编程风格

前言 编程语言有面向过程和面向对象之分,因此编程风格也有所谓的面向过程的编程和面向对象的编程,而且语言的性质不会限制编程的风格. 这里主要说一下面向过程的编程. "面向过程"(Procedure Oriented)是一种以过程为中心的编程思想. C语言是面向过程的编程语言,但是依然可以写出面向对象的程序,同样C++也当然可以写出面向过程的程序咯. 如果我们把所有的程序代码都写在一个main函数里面,那么这个程序显然会显得很不和谐吧.理想一点的做法是我们把一些看起来和main函数逻辑上关

iOS-Swift 面向协议编程/组件化(模块化)编程思想

转载注明出处:http://blog.csdn.net/qxuewei/article/details/53945445 因为OC 的局限性, 使得iOS 开发组件化编程变得不可能,得益于面向对象语言的特性 (封装,继承,多态) 在我们熟悉的设计模式中渐渐形成统一的软件开发思想. 在抽取某些功能作为基类的不断运用中,代码的可移植性逐渐减弱. 就如同一棵树,从主干到各个分支,每个分支再长成细枝末叶.代码的耦合性也相应增加. 随着苹果 swift 语言的推出, 对于传统OC 语言取其精华,弃其糟粕.

Swift和C混合Socket编程实现简单的ping命令

这个是用Mac下的Network Utility工具实现ping命令,用Wireshark抓取的ICMP数据包: 发送ICMP数据包内容 接受ICMP数据包内容 一.icmp结构 要真正了解ping命令实现原理,就要了解ping命令所使用到的TCP/IP协议.ICMP(Internet Control Message,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机制,使它们在遇到差错时能把错误报告给报文源发方.ICMP协议是IP层的 一个协议,但是由于差错报告在发送给报文源发方时可能

造轮子 | 怎样设计一个面向协议的 iOS 网络请求库

近期开源了一个面向协议设计的网络请求库 MBNetwork,基于 Alamofire 和 ObjectMapper 实现,目的是简化业务层的网络请求操作. 须要干些啥 对于大部分 App 而言,业务层做一次网络请求通常关心的问题有例如以下几个: 怎样在任何位置发起网络请求. 表单创建. 包括请求地址.请求方式(GET/POST/--).请求头等-- 载入遮罩. 目的是堵塞 UI 交互,同一时候告知用户操作正在进行. 比方提交表单时在提交按钮上显示 "菊花",同一时候使其失效. 载入进度