GoF의 디자인 패턴, 전략 패턴에 대해 알아본다.
해당 글은, 다음의 코드를 기반으로 이해하는 것이 편리합니다.
핵심 요약
- 실행 중에 기능을 변경해야 하는 경우 사용
예시

Code
main
//
// main.swift
// Strategy
//
// Created by Choiwansik on 2023/02/13.
//
import Foundation
internal func main() {
let printer = SumPrinter()
printer.printValue(with: SimpleSumStrategy(), n: 10)
printer.printValue(with: GaussSumStrategy(), n: 10)
}
main()
SumPrinter
//
// SumPrinter.swift
// Strategy
//
// Created by Choiwansik on 2023/02/13.
//
import Foundation
internal class SumPrinter {
internal func printValue(with strategy: SumStrategy, n: Int) {
print("1에서 \(n)까지의 합")
print(strategy.calculate(with: n))
}
}
SumStrategy
//
// SumStrategy.swift
// Strategy
//
// Created by Choiwansik on 2023/02/13.
//
import Foundation
internal protocol SumStrategy {
func calculate(with n: Int) -> Int
}
SimpleSumStrategy
//
// SimpleSumStrategy.swift
// Strategy
//
// Created by Choiwansik on 2023/02/13.
//
import Foundation
internal class SimpleSumStrategy: SumStrategy {
internal func calculate(with n: Int) -> Int {
(1...n).reduce(.zero, +)
}
}
GaussSumStrategy
//
// GaussSumStrategy.swift
// Strategy
//
// Created by Choiwansik on 2023/02/13.
//
import Foundation
internal class GaussSumStrategy: SumStrategy {
internal func calculate(with n: Int) -> Int {
(n+1)*n / 2
}
}활용성
- 알고리즘의 변형이 필요할 때 (여러개 일 때)
결과
- 장점
- 조건문을 없앨 수 있다.
- 서브클래싱을 사용하지 않을 수 있다.
- 알고리즘의 자사용이 가능하다.
- 구현의 선택이 가능하다.
- 단점
- 클라이언트는 서로 다른 전략들에 대해 이해해야 한다. (장단점에 대해)
- Strategy와 Context 사이의 의사소통 오버헤드가 생긴다. 결합도가 높아질 수 있다.
- 객체수가 증가한다.
생각해볼 점
- 최적화가 필요한 부분에 대해 이런 패턴을 사용하면 좋을 것 같다.
- 알고리즘의 장단점이 명확하다면, 구현체를 두 개 두고 상황에 따라 선택하도록 만들면 좋겠다.