๋ชฉ์ฐจ
์ด๋ค ์ํฉ์์ ์ฐ์ผ๊น?
DB์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ,
๋๋ ๋คํธ์ํฌ๋ฅผ ๊ฑฐ์ณ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ๋ ์๊ฐ๋ ์ค๋๊ฑธ๋ฆฌ๊ณ ๋ฆฌ์์ค๋ ๋ง์ด ์๋ชจํ๊ฒ๋๋ค.
๊ธฐ์กด์ ์ด๋ฏธ DB์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ ์ธ์คํด์ค๊ฐ ์๋ค๋ฉด
ํด๋น ์ธ์คํด์ค๋ฅผ ๋ณต์ฌํ๊ณ , ๊ฐ๋ง ์กฐ๊ธ ์์ ํด์ ์ฌ์ฉํด๋ ๋์ง ์์๊น?!
ํ๋กํ ํ์ ํจํด์ด๋?
๊ธฐ์กด ์ธ์คํด์ค๋ฅผ ๋ณต์ ํ์ฌ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ํจํด์ด๋ค.
๊ธฐ์กด์ ๊ฐ์ฒด๋ฅผ ์์ฉํด์ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ๋ง๋ค ๋ ํ์ฉํ๋ค
์ฅ์
- ๋ณต์กํ ๊ฐ์ฒด ๋ง๋๋ ๊ณผ์ ์ ์จ๊ธธ ์ ์๋ค.
- ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฒ๋ณด๋ค ํจ์จ์ ์ผ ์ ์๋ค.
๋จ์
- ์ํ์ฐธ์กฐ๊ฐ ์๋ ๊ฒฝ์ฐ, ๋ณต์ ๊ณผ์ ์์ฒด๊ฐ ๋ณต์กํ ์ ์๋ค.
๊ตฌํ๋ฐฉ๋ฒ
๊ธฐ์กด์ฝ๋
GithubRepository repository = new GithubRepository();
repository.setUser("lotu-us");
repository.setName("study");
GithubIssue issue = new GithubIssue(repository);
issue.setId(1);
issue.setTitle("๋์์ธํจํด : ์ฑ๊ธํค");
String url = issue.getUrl();
System.out.println(url); // http://github.com/lotu-us/study/issues/1
//์๋ก์ด URL์ ๊ฐ์ ธ์ค๊ณ ์ํ๋ฉด ์๋๋ฐฉ๋ฒ์ผ๋ก ์ด์ ์์ฑ..?
GithubIssue issue2 = new GithubIssue(repository);
issue2.setId(2);
issue2.setTitle("๋์์ธํจํด : ๋น๋");
๊นํ๋ธ ์ด์ URL์ ์์ฑํ ๋,
repository์ ์ ์ฅ๋ User์ Name์ ๋์ผํ๊ณ issue์ id, title๋ง ๋ฐ๋ํ ๋ฐ
์๋ก์ด ์ด์๋ฅผ ๋ง๋ค๋ ๋ GithubRepository๋ฅผ ์์กดํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผํ ๊น???
๋ณ๊ฒฝ์ฝ๋
์๋ฐ์ Object ๊ฐ์ฒด๋ clone๋ฉ์๋๋ฅผ ์ด๋ฏธ ์ง์ํ๊ณ ์๋ค!
Cloneable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ๋ฐ์ clone() ๋ฉ์๋๋ฅผ override๋ฐ์
public class GithubIssue implements Cloneable {
~~~~
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
GithubRepository repository = new GithubRepository();
repository.setUser("lotu-us");
repository.setName("study");
GithubIssue issue = new GithubIssue(repository);
issue.setId(1);
issue.setTitle("๋์์ธํจํด : ์ฑ๊ธํค");
String url = issue.getUrl();
System.out.println(url); // http://github.com/lotu-us/study/issues/1
GithubIssue cloneIssue = null;
try{
cloneIssue = (GithubIssue) issue.clone();
}catch(CloneNotSupportedException){
e.printStackTrace();
}
System.out.println(cloneIssue.getUrl()); // http://github.com/lotu-us/study/issues/1
์ฃผ์ํ ์ !
== ๊ณผ equals
GithubIssue issue = new GithubIssue(repository);
GithubIssue cloneIssue = issue.clone();
๐ฅ cloneIssue != issue => true
== ์ ์ฃผ์๊ฐ์ ๋น๊ตํ๋ค.
clone๋ฉ์๋๋ก ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ ๊ฒ์ด๊ธฐ์ ์๋ก์ ์ฃผ์๊ฐ ๋ค๋ฅด๋ค
๐ฅ cloneIssue.equals(issue) => true
equals ๋ ๊ฐ์ ๋น๊ตํ๋ค.
clone๋ฉ์๋๋ก ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ์ด๋ ์๋ก์ ๊ฐ์ด ๊ฐ๊ธฐ๋๋ฌธ์ true์ด๋ค.
shallow copy(์์ ๋ณต์ฌ) vs deep copy(๊น์ ๋ณต์ฌ)
GithubRepository repository = new GithubRepository();
repository.setUser("lotu-us");
GithubIssue issue = new GithubIssue(repository);
issue.setId(1);
System.out.println(issue.getUrl());
// http://github.com/lotu-us/study/issues/1
GithubIssue cloneIssue = (GithubIssue) issue.clone();
//cloneํ ํ ์๋ก ๋ฐ๋ผ๋ณด๊ณ ์๋ repository์ user๊ฐ ๋ณ๊ฒฝ๋๋ฉด?!
repository.setUser("GumiBear");
System.out.println( cloneIssue.getRepository() == issue.getRepository() );
//๐ฅtrue
//clone๊ณผ issue๊ฐ ๋ฐ๋ผ๋ณด๋ repository๋ ์๋ก ๊ฐ๋ค
System.out.println(cloneIssue.getUrl());
// http://github.com/GumiBear/study/issues/1
์๋ฐ Object๊ฐ์ฒด์ clone()์ ์์ ๋ณต์ฌ๋ฅผ ์ง์ํ๋ค.
๋ฐ๋ผ์ ์ค๊ฐ์ ์๋ก๊ฐ ๋ฐ๋ผ๋ณด๊ณ ์๋ repository์ ๊ฐ์ ๋ฐ๋๋ฉด ์๋ณธ๊ณผ ํด๋ก ๋ชจ๋์๊ฒ ์ํฅ์ ๋ผ์น๋ค!
deep copy๋ฅผ ํ๋ ค๋ฉด @Overrideํ clone๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ฉด๋๋ค.
public class GithubIssue implements Cloneable {
~~~~
@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;
}
}
์ฌ๊ธฐ์ ์ ์ฝ๋์ฒ๋ผ ์ค๊ฐ์ repository์ ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ์๋ณธ์๋ ์ ์ฉ๋๊ฒ ์ง๋ง ํด๋ก ์๋ ์ ์ฉ๋์ง ์์ ๊ฒ์ด๋ค.
repository.setUser("lotu-us");
System.out.println(issue.getUrl());
// http://github.com/lotu-us/study/issues/1
repository.setUser("GumiBear");
System.out.println( cloneIssue.getRepository() == issue.getRepository() );
//๐ฅfalse
//clone๊ณผ issue๊ฐ ๋ฐ๋ผ๋ณด๋ repository๊ฐ ๋ค๋ฅด๋ค
System.out.println(issue.getUrl());
//์๋ณธ : http://github.com/GumiBear/study/issues/1
System.out.println(cloneIssue.getUrl());
//ํด๋ก : http://github.com/lotu-us/study/issues/1
'๐ฐ Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Bridge Pattern | ๋ธ๋ฆฟ์ง ํจํด (0) | 2022.08.15 |
---|---|
Adapter Pattern | ์ด๋ํฐํจํด (0) | 2022.08.14 |
Abstract Factory | ์ถ์ ํฉํ ๋ฆฌ ํจํด (0) | 2022.08.14 |
Factory Method Pattern | ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด (0) | 2022.07.29 |
Singleton pattern | ์ฑ๊ธํคํจํด (3) | 2022.07.29 |