public class Vehicle {
private Weight weight;
private Capacity capacity;
private Vehicle(Weight weight, Capacity capacity){
// 생략
}
// getter
// setter
}
public interface Vehicle { // <-- 자료구조가 드러난다.
Weight getWeight(); // <-- 자료가 그대로 드러난다.
Capacity getCapacity();
}
public interface Vehicle { // <-- 객체. 자료구조가 드러나지 않는다.
getVehicleWeightCapacityRemaining(); // <-- 함수만 드러낸다.
}
절차지향적인 코드 - 자료구조를 쓰는 절차적인 코드는 기존 자료구조 변경하지 않고 새 함수를 쓰기 쉽다. 새로운 자료구조를 추가하기 어렵다. 추가하려면 모든 함수를 고쳐야한다.
* A → B 상태에서 B의 구현 클래스를 추가하는 경우, A의 코드를 수정해야한다.
객체지향적인 코드 - 기존 함수를 변경하지 않으면서 새 클래스를 추가하기 쉽다. 그렇지만 새로운 함수를 추가하기는 어렵다. 그러려면 모든 클래스를 고쳐야한다.
절차지향적 | 객체지향적 |
---|---|
자료구조를 사용 | 추상화된 함수 사용 (자료구조는 숨긴다) |
함수 추가가 쉽다. | 클래스(자료구조)를 추가하기 쉽다 |
새로운 함수를 추가해야할 때 | 새로운 자료 타입이 필요할 때 |