package me.hsnam.prototype;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Objects;
@Getter
@Setter
@ToString
public class GithubIssue implements Cloneable{
private int id;
private String title;
private GithubRepository githubRepository;
public String getUrl() {
return String.format("https://github.com/%s/%s/issues/%d", githubRepository.getUser(), githubRepository.getName(), this.getId());
}
//얕은 복사의 Clone
// @Override
// public Object clone() throws CloneNotSupportedException {
// return super.clone();
// }
// 깊은 복사
@Override
public Object clone() throws CloneNotSupportedException {
GithubRepository repository = new GithubRepository();
repository.setUser(this.githubRepository.getUser());
repository.setName(this.githubRepository.getName());
GithubIssue issue = new GithubIssue();
issue.setTitle(this.title);
issue.setId(this.id);
return issue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GithubIssue issue = (GithubIssue) o;
return id == issue.id && Objects.equals(title, issue.title) && Objects.equals(githubRepository, issue.githubRepository);
}
@Override
public int hashCode() {
return Objects.hash(id, title, githubRepository);
}
}
package me.hsnam.prototype;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class GithubRepository {
private String user;
private String name;
}
package prototype;
import me.hsnam.prototype.GithubIssue;
import me.hsnam.prototype.GithubRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;
import java.util.ArrayList;
public class PrototypeTest {
@Test
@DisplayName("prototype 패턴 테스트")
public void prototypeTest() throws CloneNotSupportedException{
GithubRepository githubRepository = new GithubRepository();
githubRepository.setName("hong-brother");
githubRepository.setUser("hsnam");
GithubIssue issue = new GithubIssue();
issue.setTitle("디자인 패턴 이슈");
issue.setId(1);
issue.setGithubRepository(githubRepository);
// System.out.println(issue.getUrl());
GithubIssue anotherIssue = (GithubIssue) issue.clone();
Assertions.assertNotEquals(issue, anotherIssue);
Assertions.assertEquals(false, issue.equals(anotherIssue));
Assertions.assertEquals(issue.getClass(), anotherIssue.getClass());
Assertions.assertEquals(false, issue.getGithubRepository() == anotherIssue.getGithubRepository());
}
}