예고르 부가옌코
-
엘레강트 오브젝트 : 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로 끝..