First Class Collection (일급 컬렉션): Collection을 Wrapping 하면서, 그 외 다른 멤버 변수가 없는 상태
일급 컬렉션을 적용하지 않은 경우: Study 클래스의 멤버 변수로 스터디원을 의미하는 member에 대한 리스트를 가지고 있는 경우
public class Study {
private String name;
List<Member> members;
// ...
}
public class Study {
private Members members;
public Study(Members members) {
this.members = members;
}
public Members getMembers() {
return members;
}
}
class Members {
private List<Member> members;
public Members(List<Member> members) {
this.members = members;
}
}
class Member {
String name;
String sex;
int age;
}
public class Study {
private String name;
private boolean isFree;
private List<Member> members;
public Study(String name, boolean isFree, List<Member> members) {
validate(members);
this.name = name;
this.isFree = isFree;
this.members = members;
}
private void validate(List<Member> members) {
if (members.size() > 10) {
throw new IllegalArgumentException("멤버는 최대 10명입니다.");
}
}
}
public class Study {
private String name;
private boolean isFree;
private Members members;
public Study(String name, boolean isFree, Members members) {
this.name = name;
this.isFree = isFree;
this.members = members;
}
}
//일급 컬렉션
class Members {
private List<Member> members;
public Members(List<Member> members) {
validate(members);
this.members = members;
}
private void validate(List<Member> members) {
if (members.size() > 10) {
throw new IllegalArgumentException("스터디원은 최대 10명입니다.");
}
}
}
public class Study {
private String name;
private boolean isFree;
private List<Member> members;
// ...
public List<Member> findAdults() {
return members.stream()
.filter(m -> m.getAge() >= 20)
.collect(Collectors.toList());
}
}
public class Study {
private List<Member> members;
// ...
public List<Member> findAdults() {
return members.stream() // 중복 코드
.filter(m -> m.getAge() >= 20)
.collect(Collectors.toList());
}
}
public class Club {
private List<Member> members;
// ...
public List<Member> findAdults() {
return members.stream() // 중복 코드
.filter(m -> m.getAge() >= 20)
.collect(Collectors.toList());
}
}
public class Study {
private Members members;
// ...
public List<Member> findAdults() {
return members.findAdults();
}
}
public class Club {
private Members members;
// ...
public List<Member> findAdults() {
return members.findAdults();
}
}
//일급 컬렉션
class Members {
private List<Member> members;
// ...
public List<Member> findAdults() {
return members.stream()
.filter(m -> m.getAge() >= 20)
.collect(Collectors.toList());
}
}
class Members {
private List<Member> members;
public Members(List<Member> members) {
this.members = members;
}
public Member findMember(int index) {
return members.get(index);
}
}
class Members {
private List<Member> members;
public Members(List<Member> members) {
this.members = members;
}
// ...
public List<Member> getMembers() {
return Collections.unmodifiableList(members);
}
}
https://developer-hm.tistory.com/227
https://jojoldu.tistory.com/412
https://tecoble.techcourse.co.kr/post/2020-05-08-First-Class-Collection/
단점은 어떤 게 있을까요 태곤님