在Swift中什么是面向协议的编程?它带来什么附加值? [英] What is Protocol Oriented Programming in Swift? What added value does it bring?

查看:110
本文介绍了在Swift中什么是面向协议的编程?它带来什么附加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自苹果公司自己的网站:"Swift设计的核心是两个非常强大的想法:面向协议的编程和一流的价值语义."

From Apple's own website: "At the heart of Swift's design are two incredibly powerful ideas: protocol-oriented programming and first class value semantics."

有人可以详细说明什么是面向协议的编程,它带来了哪些附加值?

Can someone please elaborate what exactly is protocol oriented programming, and what added value does it bring?

我已阅读

I have read this and watched the Protocol-Oriented Programming in Swift video, but coming from an Objective-C background still haven't understood it. I kindly ask for a very plain English answer along with code snippets & technical details about how it's different from Objective-C.

我正在使用<tableViewDelegate, CustomDelegate>的困惑中的一个 我们也不能在Objective-C中也遵循多种协议吗?那么Swift又是如何新的呢?

Just one of the confusions I have is using <tableViewDelegate, CustomDelegate> Couldn't we also conform to multiple protocols in Objective-C as well? So again how is Swift new?

请参见面向协议的视图视频.我发现该视频更加基础,更容易掌握有意义的用例. WWDC视频本身有点先进,并且需要更大的广度.另外,这里的答案有些抽象.

See Protocol-Oriented Views video. I find this video to be more basic and easier to grasp a meaningful use case. The WWDC video itself is a bit advanced and requires more breadth. Additionally the answers here are somewhat abstract.

推荐答案

前言:POP和OOP不互斥.它们是与设计息息相关的范例.

Preface: POP and OOP are not mutually exclusive. They're design paradigms that are greatly related.

基于OOP的POP的主要方面是组合优于继承.这样做有几个好处.

The primary aspect of POP over OOP is that is prefers composition over inheritance. There are several benefits to this.

在大型继承层次结构中,祖先类倾向于包含大多数(通用)功能,而叶子类仅贡献最小.这里的问题是,祖先类最终会做很多事情.例如,Car驱动,存储货物,为乘客座位,播放音乐等.这些功能各有千秋,但它们却不可分割地归入Car类. Car的后代,例如FerrariToyotaBMW等,都对该基类进行了最小的修改.

In large inheritance hierarchies, the ancestor classes tend to contain most of the (generalized) functionality, with the leaf subclasses making only minimal contributions. The issue here is that the ancestor classes end up doing a lot of things. For example, a Car drives, stores cargo, seats passengers, plays music, etc. These are many functionalities that are each quite distinct, but they all get indivisibly lumped into the Car class. Descendants of Car, such as Ferrari, Toyota, BMW, etc. all make minimal modifications to this base class.

其结果是减少了代码重用.我的BoomBox也播放音乐,但不是汽车.无法从Car继承音乐播放功能.

The consequence of this is that there is reduced code reuse. My BoomBox also plays music, but it's not a car. Inheriting the music-playing functionality from Car isn't possible.

Swift鼓励的是将这些大型整体类分解为较小的组件组​​成.然后可以更轻松地重用这些组件. CarBoomBox都可以使用MusicPlayer.

What Swift encourages instead is that these large monolithic classes be broken down into a composition of smaller components. These components can then be more easily reused. Both Car and BoomBox can use MusicPlayer.

Swift提供了多种功能来实现此目的,但是到目前为止,最重要的是协议扩展.它们允许协议的实现与实现类分开存在,因此许多类可以简单地实现该协议并立即获得其功能.

Swift offers multiple features to achieve this, but the most important by far are protocol extensions. They allow implementation of a protocol to exist separate of its implementing class, so that many classes may simply implement this protocol and instantly gain its functionality.

这篇关于在Swift中什么是面向协议的编程?它带来什么附加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆