final class CalculateShape {
func calculateSquareArea(length: Int) -> Int {
return calculateArea(length: length, width: length)
}
func calculateRectangleArea(length: Int, width: Int) -> Int {
return calculateArea(length: length, width: width)
}
// 만일 따로 뺀 메소드가 클래스의 단일 책임 원칙을 위반하는 메소드라면
// 다른 클래스로 옮겨서 사용하면 된다.
func calculateArea(length: Int, width: Int) -> Int {
return length * width
}
}
좋은 이름을 선택한다.
함수와 클래스 크기를 가능한 줄인다.
표준 명칭을 사용한다.
// 1. Command 패턴을 사용하는 경우
protocol Command {
func execute()
}
// 클래스에도 Command라는 이름을 붙여주면
// 다른 개발자가 해당 클래스는 Command 패턴을 사용하고 있다고 쉽게 알 수 있다.
class LightOnCommand: Command {
var light: Light
init(light: Light) {
self.light = light
}
func execute() {
light.on()
}
}
==============================================================================
// 2. Visitor 패턴을 사용하는 경우
protocol Visitor {
func visit(element: Element)
}
// 클래스에도 Visitor라는 이름을 붙여주면
// 다른 개발자가 해당 클래스는 Visitor 패턴을 사용하고 있다고 쉽게 알 수 있다.
class ConcreteVisitor: Visitor {
func visit(element: Element) {
print("Visiting \(element)")
}
}