swift 命令_在Swift 5中实现命令设计模式

swift 命令

In this article, we will learn about the Command design pattern and implement it in an Xcode Playground.

在本文中,我们将学习Command设计模式并在Xcode Playground中实现它。

In short, the Command is a behavioral design pattern that helps us encapsulate actions (commands) and execute them at a later point in time.

简而言之,命令是一种行为设计模式,可以帮助我们封装动作 (命令)并在以后的某个时间执行。

It involves the following components:

它包含以下组件:

  • Invoker — an object that executes its commands

    调用器 -执行其命令的对象

  • Command — an action that needs to be performed

    命令 -需要执行的操作

  • Receiver — an object that is being acted upon.

    接收器 -正在作用的对象。

开始吧 (Let’s Start)

Consider an example of a moderator who reviews an app. The moderation process may have different states: inReview, approved, and rejected. We want to set specific commands for the moderator and execute them one-by-one with some delay. So let’s start creating the required components.

考虑一个审核应用程序的主持人的示例。 审核过程可能具有不同的状态: inReviewapprovedrejected 。 我们要为主持人设置特定的命令,并在延迟的情况下一对一地执行它们。 因此,让我们开始创建所需的组件。

接收者 (Receiver)

Our receiver is an App class that has a dependency on the ModerationState. Its initial state is submittedForReview:

我们的接收者是一个依赖于ModerationStateApp类。 其初始状态为submittedForReview

Next, let’s create a Command.

接下来,让我们创建一个Command

命令 (Command)

We start with a base class that each specific command with inherit from:

我们从一个基类开始,每个特定的命令都继承自该基类:

The command depends on the single App object that we will mutate accordingly in subclasses. Now let’s create these subclasses:

该命令取决于我们将在子类中进行相应突变的单个App对象。 现在让我们创建这些子类:

As we can see, each command changes the moderationState property of the App object inside the execute() method.

如我们所见,每个命令都会在execute()方法中更改App对象的moderationState属性。

With commands done, now we need to create an Invoker.

完成命令后,现在我们需要创建一个Invoker

召唤者 (Invoker)

We create the Moderator class that will act an invoker of the aforementioned commands. It has a [ModerationCommand] array and an App object. Once its commands property is set, it starts reviewing the App by executing each command with some delay. Using a DispatchSemaphore we allow only one command to be executed at a time:

我们创建了Moderator类,它将充当上述命令的调用者。 它具有一个[ModerationCommand]数组和一个App对象。 一旦设置了commands属性,它就会通过延迟执行每个命令来开始检查App 。 使用DispatchSemaphore我们一次只允许执行一个命令:

Now we can test what we have created. Let’s initialize the Moderator and provide it with four moderation commands as follows:

现在,我们可以测试所创建的内容。 让我们初始化Moderator并为其提供四个主持命令,如下所示:

As expected, the following messages are printed in the console, each after a specific delay:

如预期的那样,以下消息会在控制台中打印,每个消息都会经过特定的延迟:

Image for post

We have implemented the Command design pattern.

我们已经实现了Command设计模式。

资源资源 (Resources)

The source code of the playground is available in a Gist.

游乐场的源代码在Gist中可用。

结语 (Wrapping Up)

Want to learn more about design patterns in Swift? Feel free to check out my other relevant pieces:

想更多地了解Swift中的设计模式? 随时查看我的其他相关文章:

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/cleansoftware/implement-the-command-design-pattern-in-swift-5-a3a07352af09

swift 命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值