본문 바로가기

개발 공부

커뮤니티 피드(1) - User 도메인 개발

* 밑줄: 여기서 처리할 부분

1. 유저 생성

  • 유저는 id 를 통해 구분이 되어야 함 
  • 이름, 프로필 이미지를 입력 받으면 저장되어야 함
    • 단, 이름은 빈 값이 입력되면 안됨
// org.communify.user.domain
// User.java
public class User {
	private final Long id;
    private final UserInfo userInfo;
   	
    public User(Long id, UserInfo userInfo) {
    	this.id = id;
        this.userInfo = userInfo;
    }
}

// UserInfo.java
public class UserInfo {
	private final String name;
    private final String profileImageUrl;
    
    public UserInfo(String name, String profileImageUrl) {
    	// 이름 빈 값 체크
        if (name == null || name.isEmpty()) {
        	throw new IllegalArgumentException();
        }
        this.name = name;
        this.profileImageUrl = profileImageUrl;
    }
}
  • User
    • 역할:
      • 도메인 객체로, 사용자와 관련된 정보를 나타냄
    • 책임:
      • 고유 식별자인 id로 사용자를 식별
      • UserInfo 객체를 통해 사용자의 정보를 포함
  • UserInfo
    • 역할:
      • VO
    • 책임:
      • 이름이 빈 값인지 유효성 체크
      • VO로 설계되어 한 번 생성되면 내부에서 값 변경 불가 - 불변성 유지

 

2. 팔로우 기능

  • 사용자는 다른 이용자를 팔로우 할 수 있음
  • 팔로우 취소 할 수 있음
  • 자기 자신을 팔로우 할 수 없음
// org.community.user.domain
// UserFollow.java
public class UserFollow {
	private int count;
    
    public UserFollow() {
    	this.count = 0;
    }
    
    public void increase() {
    	this.count++;
    }
    
    public void decrease() {
    	if (this.count <= 0) {
        	return;
        }
        this.count--;
    }
    
    public int getCount() {
    	return this.count;
    }
}

// User.java
public class User {
...
	private final UserFollow follower;
    private final UserFollow following;
    
    public User(...) {
    	...
        this.follower = new UserFollow();
        this.following = new UserFollow();
    }
	
    // 팔로우
    public void following(User targetUser) {
    	// 본인 확인
    	if (targetUser.equals(this)) {
        	throw new IllegalArgumentException();
        }
        this.following.increase();
        targetUser.follower.increase();
    }
    
    // 언팔로우
    public void unfollow(User targetUser) {
    	// 본인 확인
    	if (this.equals(targetUser)) {
        	throw new IllegalArgumentException();
        }
        this.following.decrease();
        targetUser.follower.decrease();
    }
    
    // equals & hash @Override 생성 
}
  • User
    • 역할:
      • 도메인 객체로, 사용자와 관련된 주요 비즈니스 로직(팔로우/언팔로우 동작)을 처리.
    • 책임:
      • 팔로우/언팔로우 동작의 전체 흐름을 제어.
      • 본인을 팔로우하거나 언팔로우하지 못하도록 **유효성 검사(본인 확인)**를 수행.
      • 팔로우/언팔로우에서 발생하는 팔로잉/팔로워 수의 증가·감소를 UserFollow 객체에 위임.
  • UserFollow
    • 역할:
      • 사용자 간의 관계에서 팔로잉/팔로워 수를 관리하는 상태 객체.
    • 책임:
      • increase()와 decrease() 메서드를 통해 팔로잉/팔로워 수를 변경.
      • User 객체로부터 팔로잉/팔로워 수 변경이라는 세부 작업을 위임받아 처리.

 

출처: https://fastcampus-community-feed.notion.site/883fa62553224723a74fa97654bc41e8