Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions gof/src/main/kotlin/com/gof/pattern/prototype/after/App.kt
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions gof/src/main/kotlin/com/gof/pattern/prototype/after/GithubIssue.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.gof.pattern.prototype.after

class GithubRepository {
var user: String = ""
var name: String = ""
}
16 changes: 16 additions & 0 deletions gof/src/main/kotlin/com/gof/pattern/prototype/before/App.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.gof.pattern.prototype.before

class GithubRepository {
var user: String = ""
var name: String = ""
}
1 change: 1 addition & 0 deletions ria/java/gof/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Student> students = new ArrayList<>(); // List는 Cloneable을 상속받지 않음
students.add(keesun);
students.add(whiteship);

List<Student> clone = new ArrayList<>(students);
System.out.println(clone);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
"}";
}
}