GoF์ ๋์์ธ ํจํด, ๋ช ๋ น ํจํด์ ๋ํด ์์๋ณธ๋ค.
ํด๋น ๊ธ์, ๋ค์์ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ดํดํ๋ ๊ฒ์ด ํธ๋ฆฌํฉ๋๋ค.
ํต์ฌ ์์ฝ
- ํ๋์ ๋ช ๋ น์ ๊ฐ์ฒดํํ ํจํด
- ๊ฐ์ฒด๋ ์ ๋ฌํ ์ ์๊ณ , ๋ณด๊ดํ ์ ์๋ค.
- ์ฆ, ๋ช ๋ น(๊ธฐ๋ฅ)์ ์ ๋ฌํ๊ณ ๋ณด๊ดํ ์ ์๊ฒ ๋๋ค.
- Batch ์คํ, Undo/Redo, ์ฐ์ ์์๊ฐ ๋์ ๋ช ๋ น์ ๋จผ์ ์คํ ๊ณผ ๊ฐ์ ๊ฒ๋ค์ด ๊ฐ๋ฅํ๋ค.
์์
Code
Command
//
// Command.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal protocol Command {
func run()
// func undo() ํ์ํ๋ค๋ฉด ๊ตฌํํ๋ฉด ๋จ
}
EnterCommand
//
// EnterCommand.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal class EnterCommand: Command {
internal func run() {
(0...4).forEach { _ in print() }
}
}
PrintCommand
//
// PrintCommand.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal class PrintCommand: Command {
internal init(content: String) {
self.content = content
}
internal func run() {
print(self.content)
}
private let content: String
}
TabCommand
//
// TabCommand.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal class TabCommand: Command {
internal init(times: Int) {
self.times = times
}
internal func run() {
(0..<self.times).forEach { _ in print(" ", terminator: "") }
}
private let times: Int
}
DecorationCommand
//
// DecorationCommand.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal class DecorationCommand: Command {
internal func run() {
print("======================================")
}
}
CommandGroup
//
// CommandGroup.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal class CommandGroup: Command {
internal func add(command: Command) {
self.commands.append(command)
}
internal func run() {
self.commands.forEach { $0.run() }
}
private var commands = [Command]()
}
main
//
// main.swift
// Command
//
// Created by Choiwansik on 2023/01/16.
//
import Foundation
internal func main() {
// ํ๋์ฉ ํ
์คํธ ํด๋ณด๊ธฐ
let enterCommand: Command = EnterCommand()
enterCommand.run()
let tabCommand: Command = TabCommand(times: 3)
tabCommand.run()
let printCommand: Command = PrintCommand(content: "์ถ๋ ฅํด์ฃผ์ธ์.")
printCommand.run()
let decorationCommand: Command = DecorationCommand()
decorationCommand.run()
// ๊ธฐ์กด์ ๋ง๋ค์๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํด๋ณด๊ธฐ (print)
let tabCommand2: Command = TabCommand(times: 6)
tabCommand2.run()
printCommand.run()
// Command Group์ ํตํด Batch๋ก ๋ง๋ค๊ธฐ
let commandGroup = CommandGroup()
commandGroup.add(command: enterCommand)
commandGroup.add(command: tabCommand)
commandGroup.add(command: printCommand)
commandGroup.add(command: decorationCommand)
commandGroup.run()
}
main()
๋๊ธฐ
- ๋ฉ๋ด๊ฐ ์๋ ์ด๋ค ํ๋ก๊ทธ๋จ์ ์๊ฐํด๋ณด์.
- ๋ซ๊ธฐ ๋ฒํผ์ ๋ฉ๋ด์๋ ์๊ณ , X๋ฒํผ์ ๋๋ฌ๋ ๋ด๋ ค๊ฐ๋ค.
- ์ด๋ ๊ฒ ๋ค๋ฅธ ๋์์ ๋ํด ๊ฐ์ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ ํ์๊ฐ ์๋ค.
- ์ด๋ด ๊ฒฝ์ฐ โ๋์โ ์์ฒด(ํน์ ์์ฒญ)๋ฅผ ๋ฌถ์ด์ฃผ๋ฉด ์ข๋ค.
- ๋ํ ์ฌ๋ฌ ๋ช ๋ น์ ๋ณตํฉ์ ์ผ๋ก ์ฒ๋ฆฌํด์ผํ ๋๋ ์๋ค.
- ์ด๋ฐ ๊ฒฝ์ฐ๋ ์๊ฐํ์ฌ ์์์ ๋ฌถ์ โ๋ช ๋ นโ์ ๋ค์ ๋ฌถ์ด์ฃผ๋ ๊ฐ์ฒด๋ ์์ผ๋ฉด ์ข๊ฒ ๋ค.
ํ์ฉ์ฑ
- ์ํํ ๋์์ ๊ฐ์ฒด๋ก ๋งค๊ฐ๋ณ์ํ ํ๊ณ ์ ํ ๋
- Callback๊ณผ ๊ฐ์ด ์ด๋ค ๋์์ ๋ฑ๋กํด์ผํ ํ์๊ฐ ์๋ค.
- Command ํจํด์ ์ด์ ๊ฐ์ด ๋์์ ๊ฐ์ฒด ์งํฅ ๋ฐฉ์์ผ๋ก ๋ํ๋ธ ๊ฒ์ด๋ผ ์๊ฐํ๋ฉด ๋๊ฒ ๋ค.
- ์คํ ์ทจ์ ๊ธฐ๋ฅ์ ์ง์ํ๊ณ ์ถ์ ๋
- ์ํ๋ฅผ ๊ด๋ฆฌํ๋ Group๊ณผ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๊ธฐ ๋๋ฌธ์ ๋๋๋ฆฌ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค.
- ๋ณ๊ฒฝ๊ณผ์ ์ ๋ํ ๋ก๊น
์ ์ง์ํ๊ณ ์ถ์ ๋
- Command ๊ฐ์ฒด์
load()
,store()
์ ๊ฐ์ ์ฐ์ฐ์ ์ง์ํ๋ฉด ์ ์ฅ์์ ์ ์ฅํ๋ค๊ฐ ๋ถ๋ฌ์ค๋ ๊ฒ๋ ๊ฐ๋ฅํด์ง๋ค. - ์์คํ ์ด ๊ฐ์๊ธฐ ์ข ๋ฃ(์๋ ๊ฐ์ ๊ฑฐ)๋์์ ์ ๋ณต๊ตฌํ๊ณ ์ถ๋ค๋ฉด ํด๋น ํจํด์ ๊ณ ๋ คํด๋ณด์.
- Command ๊ฐ์ฒด์
๋ค๋ฅธ ์ด๋ฆ
- Action
- Transaction
๊ฒฐ๊ณผ
- ์ฐ์ฐ์ ํธ์ถํ๋ ๊ฐ์ฒด์ ์ํ๋ฐฉ๋ฒ์ ๊ตฌํํ๋ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ๋ค.
- ๋ณตํฉ ๋ช ๋ น์ ๋ง๋ค ์ ์๋ค.
- ์๋ก์ด Command ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํ๊ธฐ ์ฝ๋ค.
์๊ฐํด๋ณผ ์
- ๊ฒฐ๊ตญ Command ๋ ์ด๋ค ๋์์ ์บก์ํ ํ ๊ฒ์ผ๋ก ์๊ฐํ ์ ์๊ฒ ๋ค.
- ๊ทธ๋์ ํ๋ ํจํด์ ๋ค์ด๊ฐ ๊ฒ์ผ ๋ฏ
- ๊ทธ๋ฆฌ๊ณ ๊ทธ Command๋ผ๊ณ ๊ฐ์ธ์ง ๋๋ถ์, ์ด ๋์๋ค์ ์ ์ฐํ๊ฒ ์ฎ๊ณ , ๋ฐ๋๋ก ์ฒ๋ฆฌํ ์ ์๊ฒ ๋๋ค.
- ๊ฒฐ๊ตญ ์บก์ํ๊ฐ ๊ฐ์ฅ ์ค์ํ ํ๋๋ผ๋ ์๊ฐ์ด ๋ ๋ค.
- ๋์์ ๋๋๋ ๊ฒ์์ โ๋ฌด์โ์ ๋ํด ์ด์ ์ ๋ง์ถ ๊ฒ์ด ๋ช
๋ น ํจํด์ด๋ค.
- โ์ด๋ป๊ฒโ๋ ์ ๋ต ํจํด
IBAction
์ผ๋ก ํน์ ๋์ ์์ฒด๋ฅผ ๋๊ฒจ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ์์ด ๋ช ๋ น ํจํด์ผ ๋ฏ ํ๋ค.- ์ด๋ฒคํธ๊ฐ ์์ ์ ์ฒ๋ฆฌ๋ ๋์ ์์ฒด๋ฅผ ์บก์ํ(ํจ์ํ)ํด์ ๋๊ธฐ๊ธฐ ๋๋ฌธ