标签:组织 bug can lead oop sse tip 协议 ack
主要目标是提供抽象能力和解决值类型的多态问题
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:
According to Abrahams, protocol-oriented programming is a better abstraction mechanism in that it allows:
解决的问题:
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.
二、协议提供了抽象和多态机制
虚函数表和存在容器的多态支持;
接口函数+泛型编程的多态
三、协议支持值类型和引用类型;
四、协议本身支持类型的组织
五、模拟多重继承
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
标签:组织 bug can lead oop sse tip 协议 ack
原文地址:https://www.cnblogs.com/feng9exe/p/10623120.html