design patterns : prototype
This commit is contained in:
28
design-pattern/gof/src/prototype/after/App.java
Normal file
28
design-pattern/gof/src/prototype/after/App.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package prototype.after;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) throws CloneNotSupportedException {
|
||||
GithubRepository repository = new GithubRepository();
|
||||
repository.setUser("whiteship");
|
||||
repository.setName("live-study");
|
||||
|
||||
GithubIssue githubIssue = new GithubIssue(repository);
|
||||
githubIssue.setId(1);
|
||||
githubIssue.setTitle("1주차 과제: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가.");
|
||||
|
||||
String url = githubIssue.getUrl();
|
||||
System.out.println(url);
|
||||
|
||||
GithubIssue clone = (GithubIssue) githubIssue.clone();
|
||||
System.out.println(clone.getUrl());
|
||||
|
||||
System.out.println(clone != githubIssue);
|
||||
System.out.println(clone.equals(githubIssue));
|
||||
System.out.println(clone.getClass() == githubIssue.getClass());
|
||||
|
||||
// shallow copy (default)
|
||||
System.out.println(clone.getRepository() == githubIssue.getRepository());
|
||||
}
|
||||
|
||||
}
|
||||
71
design-pattern/gof/src/prototype/after/GithubIssue.java
Normal file
71
design-pattern/gof/src/prototype/after/GithubIssue.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package prototype.after;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class GithubIssue implements Cloneable {
|
||||
|
||||
private int id;
|
||||
|
||||
private String title;
|
||||
|
||||
private GithubRepository repository;
|
||||
|
||||
public GithubIssue(GithubRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public GithubRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return String.format("https://github.com/%s/%s/issues/%d",
|
||||
repository.getUser(),
|
||||
repository.getName(),
|
||||
this.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
|
||||
// deep copy
|
||||
// GithubRepository repository = new GithubRepository();
|
||||
// repository.setUser(this.repository.getUser());
|
||||
// repository.setName(this.repository.getName());
|
||||
//
|
||||
// GithubIssue githubIssue = new GithubIssue(repository);
|
||||
// githubIssue.setId(this.id);
|
||||
// githubIssue.setTitle(this.title);
|
||||
// return githubIssue;
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
GithubIssue that = (GithubIssue) o;
|
||||
return id == that.id && Objects.equals(title, that.title) && Objects.equals(repository, that.repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, title, repository);
|
||||
}
|
||||
}
|
||||
24
design-pattern/gof/src/prototype/after/GithubRepository.java
Normal file
24
design-pattern/gof/src/prototype/after/GithubRepository.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package prototype.after;
|
||||
|
||||
public class GithubRepository {
|
||||
|
||||
private String user;
|
||||
|
||||
private String name;
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
18
design-pattern/gof/src/prototype/before/App.java
Normal file
18
design-pattern/gof/src/prototype/before/App.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package prototype.before;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GithubRepository repository = new GithubRepository();
|
||||
repository.setUser("whiteship");
|
||||
repository.setName("live-study");
|
||||
|
||||
GithubIssue githubIssue = new GithubIssue(repository);
|
||||
githubIssue.setId(1);
|
||||
githubIssue.setTitle("1주차 과제: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가.");
|
||||
|
||||
String url = githubIssue.getUrl();
|
||||
System.out.println(url);
|
||||
}
|
||||
|
||||
}
|
||||
41
design-pattern/gof/src/prototype/before/GithubIssue.java
Normal file
41
design-pattern/gof/src/prototype/before/GithubIssue.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package prototype.before;
|
||||
|
||||
public class GithubIssue {
|
||||
|
||||
private int id;
|
||||
|
||||
private String title;
|
||||
|
||||
private GithubRepository repository;
|
||||
|
||||
public GithubIssue(GithubRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public GithubRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return String.format("https://github.com/%s/%s/issues/%d",
|
||||
repository.getUser(),
|
||||
repository.getName(),
|
||||
this.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package prototype.before;
|
||||
|
||||
public class GithubRepository {
|
||||
|
||||
private String user;
|
||||
|
||||
private String name;
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user