为什么协议在swift中比class好? [英] Why protocol is better than class in swift?

查看:101
本文介绍了为什么协议在swift中比class好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过观看Apple提供的视频教程,似乎swift是面向协议的编程语言,苹果鼓励程序员使用协议而不是类。
但是从我个人的观点来看,我认为协议没有明显的优势。 class可以符合协议,但它们也可以从超类继承。我们可以添加扩展到协议,但我们也可以添加扩展到类。我们可以在符合协议的类中实现函数,但我们也可以在子类中覆盖func。
我仍​​然感到困惑,为什么我们需要使用协议而不是类。当我们应该使用协议而不是类?

By watching the video tutorial provided by Apple, it seems that swift is protocol-oriented programming langue and apple encourage programmers to use protocol than class. But from my personal view, I see no apparent advantages for protocol. class can conform to protocol, but they can also inherit from superclass. We can add extension to protocol, but we can also add extension to class. We can implement functions in classes which conforms to protocol, but we can also override func in subclass. I am still confused that why we need to use protocol rather than class. And when we should use protocol instead of class ?

推荐答案

让我们下载一个例子。

您有一个基类 FileDownloadModel ,并有3个子类 AudioFileDownloadModel,VideoFileDownloadModel,ImageDownloadModel

You has a Base class FileDownloadModel and have 3 subclasses AudioFileDownloadModel, VideoFileDownloadModel, ImageDownloadModel.

您有一个 DownloadManager ,其输入为 FileDownloadModel ,并使用其 urlToDownload 属性下载该文件的模型。

You have a DownloadManager that takes input as FileDownloadModel and uses its urlToDownload Property of model to download the file.

稍后您会被告知,还有一个模型即将推出,但其类似 UserDownloadModel <来自用户的em> subclass 。

Later down the line you are told that, there is one more model coming but its like UserDownloadModel which subclass from User.

现在看到很难处理这样的情况,你必须改变很多代码来合并下载方法。

See now it becomes difficult to handle the such scenario where you will have to change a lot of code to incorporate downloading methods.


协议导向编程如何帮助你:

How protocol oriented programming will help you here:




  1. 创建名为 DownloadingFileProtocol 的协议,并添加下载文件所需的
    方法。例如。 urlToDownload,
    pathToSave,extension etc。

  2. FileDownloadModel
    中实施相同的协议UserDownloadModel 即可。查看您无需在 UserDownloadModel 中更改
    批量代码的好处。您只需从 DownloadingFileProtocol 实施
    方法。

  3. 查看新实体是否再次下线,您将不会
    更改任何代码都只是实现协议方法。

  4. 现在你的 DownloadManager 可以输入
    DownloadingFileProtocol 而不是特定的模型,现在您可以
    将任何模型设为可下载。

  1. Create a protocol named DownloadingFileProtocol and add methods that you need for downloading file. eg. urlToDownload, pathToSave, extension etc.
  2. Implement the same protocol in FileDownloadModel and UserDownloadModel. See the benefit that you don't have to change a lot of code in UserDownloadModel. You will just implement the methods from DownloadingFileProtocol.
  3. See if again a new entity comes down the line, you will not change any code just implement the protocol methods.
  4. And now your DownloadManager can take input as DownloadingFileProtocol instead of a specific model and now you can make any model as Downloadable.

这篇关于为什么协议在swift中比class好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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