어떤 객체의 행위를 런타임에 바꿀수 있도록 한다.
-> 객체의 행위를 Strategy로 만들어 두고, 객체의 행동(메소드) 수정이 필요할때, Strategy를 교체함으로서 객체의 행동을 바꾼다.
import Foundation
protocol PlayStrategy {
func play(game: Gamable)
}
class ClassicStrategy: PlayStrategy {
func play(game: Gamable) {
print("play classic")
}
}
class ModernStrategy: PlayStrategy {
func play(game: Gamable) {
print("play modernly with \(game.playerCount) people")
}
}
protocol Gamable {
var playerCount: Int { get set }
var playStrategy: PlayStrategy { get set }
init(playerCount: Int, playStrategy: PlayStrategy)
}
extension Gamable {
func play() {
playStrategy.play(game: self)
}
}
class Game: Gamable {
var playerCount: Int
var playStrategy: PlayStrategy
required init(
playerCount: Int,
playStrategy: PlayStrategy = ClassicStrategy()
) {
self.playerCount = playerCount
self.playStrategy = playStrategy
}
}
let game = Game(playerCount: 4)
game.play()
var modernGame = Game(playerCount: 6, playStrategy: ModernStrategy())
modernGame.play()
modernGame.playStrategy = ClassicStrategy()
modernGame.play()
제 사이드 프로젝트에 적용된 코드를 간략하게 다시 적었습니다. 허술해도 이해 부탁드려요..
새로운 play 전략이 생길 경우 PlayStrategy프로토콜(인터페이스)를 채택하는 Stratgy클래스를 만들어 주면 됨.
기존의 play 코드를 수정할 필요가 없음. Gamable을 채택한 클래스 객체의 playStrategy 만 교체하면 됨.
--> 확장성을 보장해 줌. OCP 잘 지킴.
객체 지향이란 무엇일까.. 클래스 스트럭트는 아직도 고민이 됨..