클래스에 관한 고찰

ulls12·2023년 12월 8일
0

Swift TIL

목록 보기
12/60
post-thumbnail

코드의 길이를 압축시키는 기술

class AddOperation {
    func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber + secondNumber)
    }
}

class SubtractOperation {
    func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber - secondNumber)
    }
}

class MultiplyOperation {
    func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber * secondNumber)
    }
}

class DivideOperation {
    func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber / secondNumber)
    }
}

class Calculator {
    private let addOperation: AddOperation
    private let subtractOperation: SubtractOperation
    private let multiplyOperation: MultiplyOperation
    private let divideOperation: DivideOperation
    // 200개 넘는 Operation 추가
    
    init(addOperation: AddOperation,
         subtractOperation: SubtractOperation,
         multiplyOperation: MultiplyOperation,
         divideOperation: DivideOperation
         // 200개 또 추가해야하는 부분) {
        self.addOperation = addOperation
        self.subtractOperation = subtractOperation
        self.multiplyOperation = multiplyOperation
        self.divideOperation = divideOperation
        // 200개 또 또 추가해야하는 부분
    }
    
    func calculate(operator: String, firstNumber: Int, secondNumber: Int) -> Double {
        if `operator` == "+" {
            return addOperation.operate(firstNumber: firstNumber, secondNumber: secondNumber)
        }
        if `operator` == "-" {
            return subtractOperation.operate(firstNumber: firstNumber, secondNumber: secondNumber)
        }
        if `operator` == "*" {
            return multiplyOperation.operate(firstNumber: firstNumber, secondNumber: secondNumber)
        }
        if `operator` == "/" {
            return divideOperation.operate(firstNumber: firstNumber, secondNumber: secondNumber)
        }
        return 0
    }
}

클래스 Calculator 에서의 기능이 너무 많고 새로운 기능을 추가하고 메서드를 추가할 때 마다 직접 수정을 해줘야한다. 예를 들어, 200개 넘는 operate 함수를 만들게 되면, Calculator 클래스에는 각각 200개씩 3번이나 추가해야 하고 너무나도 길어지게 된다.

class AbstractOperation {
    func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return 0
    }
}

class AddOperation: AbstractOperation {
    override func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber + secondNumber)
    }
}

class SubtractOperation: AbstractOperation {
    override func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber - secondNumber)
    }
}

class MultiplyOperation: AbstractOperation {
    override func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber * secondNumber)
    }
}

class DivideOperation: AbstractOperation {
    override func operate(firstNumber: Int, secondNumber: Int) -> Double {
        return Double(firstNumber / secondNumber)
    }
}

class Calculator {
    private var abstractOperation: AbstractOperation
    
    init(operation: AbstractOperation) {
        self.abstractOperation = operation
    }
    
    func setOperation(operation: AbstractOperation) {
        self.abstractOperation = operation
    }
    
    func calculate(firstNumber: Int, secondNumber: Int) -> Double {
        abstractOperation.operate(firstNumber: firstNumber, secondNumber: secondNumber)
    }
}

부모 클래스를 두고 그 아래 자식 클래스들의 operate 함수를 재정의(override)하여 쓰게 되면, Calculator 클래스에서는 단 한 줄의 코드 만으로 Operation 클래스들의 함수를 전부 쓸 수 있게 된다.


계산기를 만드는 과정을 무시하고 숫자야구게임을 만들었다. 너무나도 어려웠고 큰 틀은 짤 수 있었지만, 세부적인 함수를 사용하는 디테일, 에러가 생기는 이유 등이 날 너무 어렵게 했다. 강의 반복해서 듣고 체화를 시켜 내 것으로 만드는 시간이 필요하다. 힘들어도 좀만 더 파이팅하자

profile
I am 개발해요

0개의 댓글