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.
考虑一个审核应用程序的主持人的示例。 审核过程可能具有不同的状态: inReview , approved和rejected 。 我们要为主持人设置特定的命令,并在延迟的情况下一对一地执行它们。 因此,让我们开始创建所需的组件。
(Receiver)
Our receiver is an App class that has a dependency on the ModerationState. Its initial state is submittedForReview:
我们的接收者是一个依赖于ModerationState的App类。 其初始状态为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:
如预期的那样,以下消息会在控制台中打印,每个消息都会经过特定的延迟:
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中的设计模式? 随时查看我的其他相关文章:
- Leverage the Coordinator Design Pattern in Swift 5 利用Swift 5中的协调器设计模式
- Implement the Facade Design Pattern in Swift 5 在Swift 5中实现外观设计模式
- Implement the Adapter Design Pattern in Swift 5 在Swift 5中实现适配器设计模式
- Implement the Strategy Design Pattern in Swift 5 在Swift 5中实施策略设计模式
- Implement the Builder Design Pattern in Swift 5 在Swift 5中实现构建器设计模式
Thanks for reading!
谢谢阅读!
翻译自: https://medium.com/cleansoftware/implement-the-command-design-pattern-in-swift-5-a3a07352af09
swift 命令