[3주차] 객체지향 코드 연습(박진현) #2
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
이펙티브 자바 내용을 적용하려 노력 했습니다 해당 내용들은 리드미에 작성해뒀습니다!
이펙티브 스터디
아이템 17. 변경 가능성을 최소화하라
불변 클래스란?
불변 인스턴스에 간직된 정보는 고정되어 객체가 파괴되 전까지 절대객체릐 달라지지 않음
클래스를 불변으로 만드려면
정적 팩터리를 사용한 불변 클래스
코드에서의 활용
TryCount를 불변 클래스로 사용
Cars는 일급 컬렉션으로 컬렉션 자체는 불변이지만 내부 요소는 가변
생성자가 하나뿐이기에 정적 팩터리메서드 사용 X
아이템18. 상속보다는 컴포지션을 사용하라
컴포지션 이란?
어떤 객체가 다른 객체를 포함해서 사용하는것(has-a 관계)
어떤 객체가 다른 객체의 기능 을 사용하고 싶을때 그 객체를 자신의 필드로 가지고 있다가 필요한 시점에 위임 하는 방식
상속(inheritance)은 강력하지만, 잘못 사용하면 결합도(coupling)가 높아지고, 유연성 떨어지며, 오류를 유발가능
부모클래스의 내부 구현에 강하게 의존하므로, 부모클래스가 바뀌면 자식클래스도 영향을 크게 받음
컴포지션(composition)은 필드로 다른 객체를 가지고 있으며, 그 객체에 기능을 위임함으로 더 유연하고 안정적 설계가 가능
코드에서의 활용
아이템 10, 11 equals 와 hashcode
equals(Object obj)
hashCode()
equals 재정의 → 반드시 hashCode도 재정의
코드에서의 활용
equals(Object obj) → 객체가 같다는 기준 정의 (역기서는 자동차의 이름)
hashCode() →
Set<Car> uniqueCars = new HashSet<>(carList);
으로 중복검사 위해서하면서 특히 궁금했던점은
전략 객체를 필드로 직접 가지고 있지 않아도 컴포지션이라고 볼 수 있을까? 입니다!