为什么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

上一篇:Learning WCF Chapter1 Summary


下一篇:django 第四天