From 3fd2ac48de851d9898d8fea78d04322983a615fa Mon Sep 17 00:00:00 2001 From: im-Lily Date: Tue, 18 Jul 2023 01:31:10 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(prototype):=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=86=A0=ED=83=80=EC=9E=85=20=ED=8C=A8=ED=84=B4=20=EC=9E=90?= =?UTF-8?q?=EB=B0=94=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ria/java/gof/build.gradle | 1 + .../com/gof/pattern/prototype/after/App.java | 25 +++++++ .../pattern/prototype/after/GithubIssue.java | 65 +++++++++++++++++++ .../prototype/after/GithubRepository.java | 12 ++++ .../com/gof/pattern/prototype/before/App.java | 18 +++++ .../pattern/prototype/before/GithubIssue.java | 22 +++++++ .../prototype/before/GithubRepository.java | 12 ++++ .../prototype/java/GithubIssueData.java | 14 ++++ .../prototype/java/JavaCollectionExample.java | 18 +++++ .../prototype/java/ModelMapperExample.java | 23 +++++++ .../gof/pattern/prototype/java/Student.java | 17 +++++ 11 files changed, 227 insertions(+) create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/after/App.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubIssue.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubRepository.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/before/App.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubIssue.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubRepository.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/java/GithubIssueData.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/java/JavaCollectionExample.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/java/ModelMapperExample.java create mode 100644 ria/java/gof/src/main/java/com/gof/pattern/prototype/java/Student.java diff --git a/ria/java/gof/build.gradle b/ria/java/gof/build.gradle index f7447e9..fb9ec88 100644 --- a/ria/java/gof/build.gradle +++ b/ria/java/gof/build.gradle @@ -15,6 +15,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.projectlombok:lombok' + implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/App.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/App.java new file mode 100644 index 0000000..a6b5666 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/App.java @@ -0,0 +1,25 @@ +package com.gof.pattern.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 = " + url); + + GithubIssue clone = (GithubIssue) githubIssue.clone(); + System.out.println("clone.getUrl() = " + clone.getUrl()); + + System.out.println(clone != githubIssue); + System.out.println(clone.equals(githubIssue)); + System.out.println(clone.getClass() == githubIssue.getClass()); + System.out.println(clone.getRepository() == githubIssue.getRepository()); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubIssue.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubIssue.java new file mode 100644 index 0000000..bd31d64 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubIssue.java @@ -0,0 +1,65 @@ +package com.gof.pattern.prototype.after; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Objects; + +@Getter +@Setter +public class GithubIssue implements Cloneable { + + private int id; + + private String title; + + private GithubRepository repository; + + public GithubIssue(GithubRepository repository) { + this.repository = repository; + } + + public String getUrl() { + return String.format("https://github.com/%s/%s/issues/%d", + repository.getUser(), + repository.getName(), + this.getId()); + } + + /* Object 가 제공해주는 메서드 - 얕은 복사 + * 얕은 복사의 경우, 값이 수정됨 + */ +// @Override +// protected Object clone() throws CloneNotSupportedException { +// return super.clone(); +// } + + /* + 깊은 복사 + */ + @Override + protected Object clone() throws CloneNotSupportedException { + 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; + } + + @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); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubRepository.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubRepository.java new file mode 100644 index 0000000..361efad --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/after/GithubRepository.java @@ -0,0 +1,12 @@ +package com.gof.pattern.prototype.after; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GithubRepository { + + private String user; + private String name; +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/App.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/App.java new file mode 100644 index 0000000..dda1ad5 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/App.java @@ -0,0 +1,18 @@ +package com.gof.pattern.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(); + githubIssue.setId(1); + githubIssue.setTitle("1주차 과졔: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가"); + + String url = githubIssue.getUrl(); + System.out.println("url = " + url); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubIssue.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubIssue.java new file mode 100644 index 0000000..0aaf559 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubIssue.java @@ -0,0 +1,22 @@ +package com.gof.pattern.prototype.before; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GithubIssue { + + private int id; + + private String title; + + private GithubRepository repository; + + public String getUrl() { + return String.format("https://github.com/%s/%s/issues/%d", + repository.getUser(), + repository.getName(), + this.getId()); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubRepository.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubRepository.java new file mode 100644 index 0000000..90c2650 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/before/GithubRepository.java @@ -0,0 +1,12 @@ +package com.gof.pattern.prototype.before; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GithubRepository { + + private String user; + private String name; +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/GithubIssueData.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/GithubIssueData.java new file mode 100644 index 0000000..003dc17 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/GithubIssueData.java @@ -0,0 +1,14 @@ +package com.gof.pattern.prototype.java; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GithubIssueData { + + private int id; + private String title; + private String repositoryUser; + private String repositoryName; +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/JavaCollectionExample.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/JavaCollectionExample.java new file mode 100644 index 0000000..31ff721 --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/JavaCollectionExample.java @@ -0,0 +1,18 @@ +package com.gof.pattern.prototype.java; + +import java.util.ArrayList; +import java.util.List; + +public class JavaCollectionExample { + + public static void main(String[] args) { + Student keesun = new Student("keesun"); + Student whiteship = new Student("whiteship"); + List students = new ArrayList<>(); // List는 Cloneable을 상속받지 않음 + students.add(keesun); + students.add(whiteship); + + List clone = new ArrayList<>(students); + System.out.println(clone); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/ModelMapperExample.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/ModelMapperExample.java new file mode 100644 index 0000000..35d7e5d --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/ModelMapperExample.java @@ -0,0 +1,23 @@ +package com.gof.pattern.prototype.java; + +import com.gof.pattern.prototype.after.GithubIssue; +import com.gof.pattern.prototype.after.GithubRepository; +import org.modelmapper.ModelMapper; + +public class ModelMapperExample { + + 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은 무엇이며 자바 코드는 어떻게 실행하는 것인가"); + + // 리플렉션 사용 + ModelMapper modelMapper = new ModelMapper(); + GithubIssueData githubIssueData = modelMapper.map(githubIssue, GithubIssueData.class); + System.out.println(githubIssueData); + } +} diff --git a/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/Student.java b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/Student.java new file mode 100644 index 0000000..853beba --- /dev/null +++ b/ria/java/gof/src/main/java/com/gof/pattern/prototype/java/Student.java @@ -0,0 +1,17 @@ +package com.gof.pattern.prototype.java; + +public class Student { + + String name; + + public Student(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + "}"; + } +} From 4339d4a8b1ebee8502f2aa6cf64193f188e761bf Mon Sep 17 00:00:00 2001 From: im-Lily Date: Tue, 18 Jul 2023 02:05:40 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(prototype):=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=86=A0=ED=83=80=EC=9E=85=20=ED=8C=A8=ED=84=B4=20=EC=BD=94?= =?UTF-8?q?=ED=8B=80=EB=A6=B0=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gof/pattern/prototype/after/App.kt | 24 +++++++++++++++++++ .../pattern/prototype/after/GithubIssue.kt | 24 +++++++++++++++++++ .../prototype/after/GithubRepository.kt | 6 +++++ .../com/gof/pattern/prototype/before/App.kt | 16 +++++++++++++ .../pattern/prototype/before/GithubIssue.kt | 16 +++++++++++++ .../prototype/before/GithubRepository.kt | 6 +++++ 6 files changed, 92 insertions(+) create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/after/App.kt create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubIssue.kt create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubRepository.kt create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/before/App.kt create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubIssue.kt create mode 100644 gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubRepository.kt diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/after/App.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/after/App.kt new file mode 100644 index 0000000..37882d3 --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/after/App.kt @@ -0,0 +1,24 @@ +package com.gof.pattern.prototype.after + +class App + +fun main() { + var repository: GithubRepository = GithubRepository() + repository.user = "whiteship" + repository.name = "live-study" + + var githubIssue: GithubIssue = GithubIssue(repository) + githubIssue.id = 1 + githubIssue.title = "1주차 과제: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가" + + val url = githubIssue.getUrl() + println(url) + + val clone = githubIssue.clone() + println(clone.getUrl()) + + println(clone != githubIssue) + println(clone == githubIssue) + println(clone.javaClass === githubIssue.javaClass) + println(clone.repository === githubIssue.repository) +} diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubIssue.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubIssue.kt new file mode 100644 index 0000000..bc7de91 --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubIssue.kt @@ -0,0 +1,24 @@ +package com.gof.pattern.prototype.after + +class GithubIssue : Cloneable { + var id: Int = 0 + var title: String = "" + var repository: GithubRepository = GithubRepository() + + constructor(repository: GithubRepository) { + this.repository = repository + } + + fun getUrl(): String { + return String.format( + "https://github.com/%s/%s/issues/%d", + repository.user, + repository.name, + this.id + ) + } + + public override fun clone(): GithubIssue { + return super.clone() as GithubIssue + } +} diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubRepository.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubRepository.kt new file mode 100644 index 0000000..2a35879 --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubRepository.kt @@ -0,0 +1,6 @@ +package com.gof.pattern.prototype.after + +class GithubRepository { + var user: String = "" + var name: String = "" +} diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/before/App.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/before/App.kt new file mode 100644 index 0000000..186e356 --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/before/App.kt @@ -0,0 +1,16 @@ +package com.gof.pattern.prototype.before + +class App + +fun main() { + var repository: GithubRepository = GithubRepository() + repository.user = "whiteship" + repository.name = "live-study" + + var githubIssue: GithubIssue = GithubIssue() + githubIssue.id = 1 + githubIssue.title = "1주차 과제: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가" + + val url = githubIssue.getUrl() + println(url) +} diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubIssue.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubIssue.kt new file mode 100644 index 0000000..a3050bc --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubIssue.kt @@ -0,0 +1,16 @@ +package com.gof.pattern.prototype.before + +class GithubIssue { + var id: Int = 0 + var title: String = "" + private var repository: GithubRepository = GithubRepository() + + fun getUrl(): String { + return String.format( + "https://github.com/%s/%s/issues/%d", + repository.user, + repository.name, + this.id + ) + } +} diff --git a/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubRepository.kt b/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubRepository.kt new file mode 100644 index 0000000..b2c79fa --- /dev/null +++ b/gof/src/main/kotlin/com/gof/pattern/prototype/before/GithubRepository.kt @@ -0,0 +1,6 @@ +package com.gof.pattern.prototype.before + +class GithubRepository { + var user: String = "" + var name: String = "" +}