오브젝트
-
오브젝트 6 - 다형성, 서브타이핑, 일관된 협력책책책 책을 읽읍시다/프로그래밍 2023. 2. 14. 23:29
다형성 다형성의 의미와 종류 다형성(polymorphism)이라는 단어는 그리스어에서 '많은'을 의미하는 'poly'와 '형태'를 의미하는 'morph'의 합성어로 '많은 형태를 가질 수 있는 능력'을 의미한다. 컴퓨터 과학에서는 다형성을 하나의 추상 인터페이스에 대해 코드를 작성하고 이 추상 인터페이스에 대해 서로 다른 구현을 연결할 수 있는 능력으로 정의한다[Czanecki00]. 간단하게 말해서 다형성은 여러 타입을 대상으로 동작할 수 있는 코드를 작성할 수 있는 방법이라고 할 수 있다. 객체지향 프로그래밍에서 사용되는 다형성은 아래와 같이 4가지로 분류할 수 있다. 유니버설(Universal 매개변수(Parametric) : 제네릭 프로그래밍과 관련이 높은데 클래스의 인스턴스 변수나 메서드의 메개..
-
오브젝트 5 - 상속 vs. 합성책책책 책을 읽읍시다/프로그래밍 2023. 2. 14. 22:15
중복과 상속 중복 코드는 변경을 방해한다. 이것이 중복 코드를 제거해야 하는 가장 큰 이유다. 중복 코드가 가지는 가장 큰 문제는 코드를 수정하는 데 필요한 노력을 몇 배로 증가시킨다는 것이다. 우선 어떤 코드가 중복인지를 찾아야 한다. 일단 중복 코드의 묶음을 찾았다면 찾아낸 모든 코드를 일관되게 수정해야 한다. 모든 중복 코드를 개별적으로 테스트해서 동일한 결과를 내놓는지 확인애야만한다. 중복 코드는 수정과 테스트에 드는 비용을 증가시킬뿐만 아니라 시스템과 우리를 공황상태로 몰아넣을 수도 있다. 이번 예제는 한 달에 한 번씩 가입자별로 전화 요금을 계산하는 애플리케이션이다. 전화 요금을 계산하는 규칙은 통화 시간을 단위 시간당 요금으로 나눠주면 된다. 10초당 5원의 통화료를 부과하는 요금제에 가입돼..
-
오브젝트 4 - 메세지, 인터페이스, 의존성책책책 책을 읽읍시다/프로그래밍 2023. 2. 13. 23:21
퍼블릭 인터페이스와 오퍼레이션 객체가 의사소통을 위해 외부에 공개하는 메세지의 집합을 퍼블릭 인터페이스라고 부른다. 프록래밍 언어의 관점에서 퍼블릭 인터페이스에 포함된 메세지를 오퍼레이션(operation)이라고 부른다. 오퍼레이션은 수행 가능한 어떤 행동에 대한 추상화다. 흔히 오퍼레이션이라고 부를 때는 내부의 구현 코드는 제외하고 단순히 메세지와 관련된 시그니처를 가리키는 경우가 대부분이다. 영화 예매 시스템의 예로 DiscountCondition 인터페이스에 정의된 isSatisfiedBy가 오퍼레이션에 해당한다. public interface DiscountCondition { boolean isSatisfiedBy(Screening screening); } 그에 비해 메세지를 수신했을 때 실제로..
-
오브젝트 3 - 데이터 중심 설계 vs. 책임 주도 설계책책책 책을 읽읍시다/프로그래밍 2023. 2. 13. 14:31
데이터 중심 설계 예제로 보는 설계 품질과 트레이드 오프 객체지향 설계에서는 두 가지 방법을 이용해 시스템을 객체로 분할할 수 있다. 첫 번째 방법은 상태(데이터)를 분할의 중심축으로 삼는 방법이고, 두 번째 방법은 책임을 분할의 중심축으로 삼는 방법이다. 데이터 중심의 관점에서는 객체는 자신이 포함하고 있는 데이터를 조작하는 데 필요한 오퍼레이션을 정의한다. 책임 중심의 관점에서 객체는 다른 객체가 요청할 수 있는 오퍼레이션을 위해 필요한 상태를 보관한다. 데이터 중심의 관점은 객체의 상태에 초점을 맞추고 책임 중심의 관점은 객체의 행동에 초점을 맞춘다. 전자는 객체를 독립된 데이터 덩어리로 바라보고 후자는 객체를 협력하는 공동체의 일원으로 바라본다. 훌륭한 객체지향 설계는 책임에 초점을 맞춰야 하는데..
-
오브젝트 2 - 유연한 객체 협력 모델 만들기책책책 책을 읽읍시다/프로그래밍 2023. 2. 8. 22:58
이번 예제는 영화 예매 시스템이다. 영화를 상영하는 스크린이 있고, 영화 상영 요금에 대한 할인 정책과 할인 조건이 있다. 할인 정책은 금액(예: 1000원)과 비율(예: 5%)이 있고, 한 영화에 하나만 적용 가능하다. 할인 조건은 순번(예: 조조 첫타임 할인)과 기간(예: 월요일 10:00~12:00 할인)이 있는데 정책과 달리 복수개가 적용 가능하다. 아래는 각 객체들에 대한 설명이다. Screening : 영화 상영 정보에 따라 상영 회차별 예매 금액을 표시한다. Reservation : 영화 상영 회차별 예약을 담당한다. Movie : Screen의 요청을 받아 영화별 할인 정책에 따른 예매 금액을 계산한다. 영화별로 할인 정책을 정할 수 있다. DiscountPolicy : 할인 정책으로 아래..
-
오브젝트 1 - 맛보기책책책 책을 읽읍시다/프로그래밍 2023. 2. 7. 22:38
저자 : 조영호 책 이모저모 객체지향 개발의 교과서이다. Java, C# 등 객체지향 언어를 다루는 개발자라면 필독해야 하는 도서이다. '토비의 스프링'을 읽었을 때와 같은 감동을 받았다. 이 책의 핵심은 "퍼블릭 인터페이스를 통해 캡슐화된 자율적인 객체들이 메세지를 주고 받으며 협력하는 코드를 설계하라"이다. 이를 다양한 코드 리팩터링 사례를 보여주면서 유연한 시스템이 어떻게 만들어지는지 설명해주는데 읽으면서 참 잘 와닿았다. 쉽게 전달이 되면서도 그렇다고 가볍지는 않고 고도의 전문 지식을 바탕으로 근거를 제시하니 읽으면서 남는게 많은 느낌이 든다. 저자의 전작인 '객체지향의 사실과 오해'는 말 그대로 객체지향의 해묵은 오해를 깨는 도끼였다면, 이번 저서는 깨진 자리에 새살을 돋게하는 영양제 같은 책이..