GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 아이템 64. 객체는 인터페이스를 사용해 참조하라 · Issue #152 · Study-2-Effective-Java/Effective-Java Open Labels 9장 일반적인 프로그래밍 원칙이펙티브 자바 9장 (일반적인 프로그래밍 원칙)이펙티브 자바 9장 (일반적인 프로그래밍 원칙) DescriptionDiscussed in https://github.com/orgs/Study-2-Effective-Java/discussions/151Originally posted by JoisFe March 13, 2023 아이템 64. 객체는 인터페이스를 사용해 참조하라어디서 많이 본 주제인데?
적합한 인터페이스만 있다면 매개변수 뿐 아니라 반환값, 변수, 필드를 전부 인터페이스 타입으로 선언하라!!!!!!!!
// 좋은 예 - 인터페이스를 타입으로 사용 Set<Son> sonSet = new LinkedHashSet<>(); // 나쁜 예 - 클래스를 타입으로 사용 LinkedHashSet<Son> sonSet = new LinkedHashSet<>(); 인터페이스를 타입으로 사용하는 습관을 길러두면 프로그램이 훨씬 유연해짐
주의할 점
Ex)
구현 타입을 바꾸려 하는 동기?
오해
적합한 인터페이스가 없다면 당연히 클래스로 참조해야 함!1. String, BigInteger가 대표적인 예
2. 클래스 기반으로 작성된 프레임워크가 제공하는 객체들
3.인터페이스에는 없는 특별한 메서드를 제공하는 클래스들
참고 (PriorityQueue)
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
클래스 타입을 직접 사용하는 경우는 이러한 추가 메서드를 꼭 사용해야 하는 위 3가지 정도의 경우로 최소화 해야함
정리
ActivityMetadataMetadataAssigneesLabels9장 일반적인 프로그래밍 원칙이펙티브 자바 9장 (일반적인 프로그래밍 원칙)이펙티브 자바 9장 (일반적인 프로그래밍 원칙) TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |