DEV Community

Sergey Leschev
Sergey Leschev

Posted on • Updated on


Swift. Design Patterns. πŸ‘« Command Pattern.

Image description

The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.


protocol DoorCommand {
    func execute() -> String

final class OpenCommand: DoorCommand {
    let doors:String
    required init(doors: String) {
        self.doors = doors
    func execute() -> String {
        return "Opened \(doors)"

final class CloseCommand: DoorCommand {
    let doors:String

    required init(doors: String) {
        self.doors = doors
    func execute() -> String {
        return "Closed \(doors)"

final class HAL9000DoorsOperations {
    let openCommand: DoorCommand
    let closeCommand: DoorCommand

    init(doors: String) {
        self.openCommand = OpenCommand(doors:doors)
        self.closeCommand = CloseCommand(doors:doors)

    func close() -> String {
        return closeCommand.execute()

    func open() -> String {
        return openCommand.execute()
Enter fullscreen mode Exit fullscreen mode


let podBayDoors = "Pod Bay Doors"
let doorModule = HAL9000DoorsOperations(doors:podBayDoors)
Enter fullscreen mode Exit fullscreen mode

Sources: Github

In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

🐝 Chain Of Responsibility
πŸ‘« Command Pattern
🎢 Interpreter Pattern
🍫 Iterator Pattern
πŸ’ Mediator Pattern
πŸ’Ύ Memento Pattern
πŸ‘“ Observer Pattern
πŸ‰ State Pattern
πŸ’‘ Strategy Pattern
πŸ“ Template Method
πŸƒ Visitor Pattern
🌰 Abstract Factory
πŸ‘· Builder Pattern
🏭 Factory Method
πŸ”‚ Monostate Pattern
πŸƒ Prototype Pattern
πŸ’ Singleton
πŸ”Œ Adapter Pattern
πŸŒ‰ Bridge Pattern
🌿 Composite Pattern
🍧 Decorator Pattern
🎁 Facade Pattern
πŸƒ Flyweight Pattern
β˜” Protection Proxy
🍬 Virtual Proxy

I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

πŸ›©οΈ #startups #management #cto #swift #typescript #database
πŸ“§ Email:
πŸ‘‹ LinkedIn:
πŸ‘‹ LeetCode:
πŸ‘‹ Twitter:
πŸ‘‹ Github:
🌎 Website:
πŸ–¨οΈ PDF Design Patterns: Download

Top comments (0)

Here is a post you might want to check out:

Regex for lazy developers

regex for lazy devs

Sorry for the callout πŸ˜†