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

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

最新文章

  1. 解决CHROME中画布中无法显示图片的方法
  2. SQL SERVER几种数据迁移/导出导入的实践
  3. IOS开发证书显示“此证书的签发者无效”解决方法
  4. Java 面向对象概念
  5. eclipse导入javax.servlet.*的方法
  6. cvThreshold
  7. C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)
  8. react中文API解读二(教程)
  9. 理解javascript:void(0);和href="#"
  10. 关于solaris中 crontab -e 出现数字0的解决办法
  11. verilog实验3:AD转换后串口输出到PC端
  12. ubuntu16.04 安装常见问题解决方案------输入法黑框
  13. SQL server SELECT 语句的基本结构
  14. ABP中的拦截器之ValidationInterceptor(下)
  15. vue_表单 input 的绑定
  16. Java构造器与构建器的使用
  17. express入门
  18. virtualenv and virtualenvwrapper
  19. 利用gulp 插件gulp.spritesmith 完成小图合成精灵图,并自动输出样式文件
  20. WinForm 应用程序禁止多个进程运行

热门文章

  1. MyBatis:事务回滚
  2. JS 操作svg画图
  3. js 毫秒转天时分秒
  4. vuejs2.0运用原生js实现简单的拖拽元素功能
  5. python实现简单的计算器功能
  6. 【代码笔记】Web-JavaScript-JavaScript 类型转换
  7. h5笔记02
  8. Suricata规则编写——常用关键字
  9. 使用SQL查看表字段和字段说明
  10. JDCP连接池连接数据库报错:java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z