조영호
-
엘레강트 오브젝트 : Mock 대신 Fake 사용책책책 책을 읽읍시다/프로그래밍 2023. 8. 4. 00:09
단위 테스트에서 Mock은 객체 의존 관계를 대신해주기 때문에 편리하다. 특히 repository에 의존하는 service나 domain을 테스트할 때 repository 의존성을 대신해주어 속도도 빠르고 복잡한 데이터를 조사할 필요도 없어진다. 하지만 과도하게 사용할 경우 테스트가 장황해지고 리팩터링 내성이 저하된다. Fake는 이런 문제를 해결해준다. 다음 자기 자신을 새로운 환율로 변환할 수 있는 Cash 클래스를 보자. public class Cash { private final Exchange exchange; private final int amount; public Cash(Exchange exchange, int amount) { this.exchange = exchange; this.am..
-
엘레강트 오브젝트 - 퍼플릭 상수(Public Constant)를 사용하지 말자책책책 책을 읽읍시다/프로그래밍 2023. 8. 1. 23:35
'상수(constant)'라고도 불리는 public static final 프로퍼티는 객체 사이에 데이터를 공유하기 위해 사용하는 매우 유명한 메커니즘이다. 글자 그대로 상수를 사용하는 이유는 데이터를 공유(또는 다른 객체를 공유)하기 위해서인데, 객체들은 어떤 것도 공유해서는 안된다. 대신 독립적이어야 하고 '닫혀 있어야(closed)'한다. 간단하게 말해서 상수를 이용한 공유 메커니즘은 캡슐화와 객체지향적인 사고 전체를 부정하는 일이다. 다음 예제는 구조화된 데이터를 Writer 객체에 기록하고, 각 줄을 '개행(new line) 문자'로 종료하는 메서드를 나타낸 것이다. public class Records { private static final String EOL = "\r\n"; void wr..
-
엘레강트 오브젝트 - 주 생성자 재활용책책책 책을 읽읍시다/프로그래밍 2023. 8. 1. 00:07
응집도가 높고 견고한 클래스에는 적은 수의 메서드와 상대적으로 더 많은 수의 생성자가 존재한다. 생성자의 개수가 더 많을수록 클래스는 더 개선되고, 사용자 입장에서 클래스를 더 편하게 사용할 수 있다. 아래와 같이 Cash란 클래스가 있고 다양한 방법으로 Cash 인스턴스를 생성하고 싶은 경우 다음과 같이 여러 종류의 생성자를 활용한다. public class Cash { private int dollars; public Cash(int dollars) { this.dollars = dollars; } public String usd() { return String.format("$ %d", this.dollars); } } new Cash(30); new Cash("$29.95"); new Cash(2..
-
엘레강트 오브젝트 - 접미사 er 사용을 자제하자책책책 책을 읽읍시다/프로그래밍 2023. 7. 30. 22:57
저자 : Yegor Bugayenko 엮자 : 조영호 들어가며 저자는 순수 OOP를 지향한다. 이러한 방식은 무조건 옳다라고 말하는 점에서 명쾌하기도 하고 너무 극단적이라 난해하기도 했던게 흥미로웠다. 이보다도 엮자 서문이 더 흥미로웠다. 보통은 책 내용에 동조하며 좋은 면을 위주로 소개하는데 반해 이 책을 읽을 때 주의할 점 먼저 설명한다. 엮자인 조영호 님의 저서인 '객체지향의 사실과 오해', '오브젝트'을 읽으며 객체지향에 대한 생각이 일관되게 잘 정리된 흔들리지 않을 느낌을 받았는데, 저자도 생각이 다르지만 결은 같은 사람 같았다. 좌파 정치인이 우파 정치인의 책을 출간한 느낌이랄까? 소개된 내용 중 실무에서 했던 내 과오와 연결되는 것이 많아서 반성문 쓴다라는 느낌으로 정리해보았다. -er로 끝..
-
오브젝트 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
데이터 중심 설계 예제로 보는 설계 품질과 트레이드 오프 객체지향 설계에서는 두 가지 방법을 이용해 시스템을 객체로 분할할 수 있다. 첫 번째 방법은 상태(데이터)를 분할의 중심축으로 삼는 방법이고, 두 번째 방법은 책임을 분할의 중심축으로 삼는 방법이다. 데이터 중심의 관점에서는 객체는 자신이 포함하고 있는 데이터를 조작하는 데 필요한 오퍼레이션을 정의한다. 책임 중심의 관점에서 객체는 다른 객체가 요청할 수 있는 오퍼레이션을 위해 필요한 상태를 보관한다. 데이터 중심의 관점은 객체의 상태에 초점을 맞추고 책임 중심의 관점은 객체의 행동에 초점을 맞춘다. 전자는 객체를 독립된 데이터 덩어리로 바라보고 후자는 객체를 협력하는 공동체의 일원으로 바라본다. 훌륭한 객체지향 설계는 책임에 초점을 맞춰야 하는데..