2021.12.04_프로토타입패턴02.패턴적용하기
본문 바로가기
CS Study/디자인패턴

2021.12.04_프로토타입패턴02.패턴적용하기

by KyeongMin 2021. 12. 4.
728x90
반응형
  • 이전과는 다르게 , 자바에서 사용하는 기능으로 사용함
  • 다음에 커스텀하는 방법까지 배울 예정
  • clone은 public에 없어서 바로는 안됨
    • object에 있음
    • 쓸 수 없는것이 protected 이기 때문에
  • clone을 지원하고 싶은 클래스가 있으면 implement Cloneable 선언해준다.
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
  • super.clone();으로 해서 보내줘도됨

Clone(); 사용전 준비

    public static void main(String[] args) throws  CloneNotSupportedException{
  • 위와 같이 throws CloneNotSupportedException을 선언해줘야 에러없이 아래와 같이 쓸수 있음
     GithubIssue clone =(GithubIssue) githubIssue.clone();
     System.out.println(clone.getUrl());
  • 이렇게 해주면 같은 것을 확인 할 수 있음
System.out.println(clone!=githubIssue);// 인스턴스는 다르지만
System.out.println(clone.equals(githubIssue));//clone은 githubIssue와 같아야함

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 super.equals(o);
    }
  • 이부분을 추가해줌
System.out.println(clone.getClass()==githubIssue.getClass());
  • 무튼 아래 세가지가 같아야함
    • true가 나와야함
  • System.out.println(clone!=githubIssue);// 인스턴스는 다르지만
    System.out.println(clone.equals(githubIssue));//clone은 githubIssue와 같아야함
    System.out.println(clone.getClass()==githubIssue.getClass());	

결론

        GithubRepository repository = new GithubRepository();
        repository.setUser("whiteship");
        repository.setName("live-study");

        GithubIssue githubIssue = new GithubIssue(repository);
        githubIssue.setId(1);
        githubIssue.setTitle("1주차 과제: JVM은 무엇이며 자바 코드는 어떻게 실행하는 것인가.");
  • 같은것을 써야하는 경우 저렇게 많이 선언할 필요없다.
  • 리소스 낭비와 시간을 줄일 수 있음

얇은 복사

  • githubIssue를 카피해서 clone했는데
  • 이곳의 repository라는 부분을 생성자에 넣고 그자체를 참조하는지 유무인데
    • 대게 기본적으로 얇은 복사이고
System.out.println(clone.getRepository()==githubIssue.getRepository());
  • ture의 결과를 얻음
  • 여기서 문제는 완벽한 새로운 클론을 만들고 싶은 경우는 얇은 복사 사용하면 안됨
  • 같은 레퍼런스를 똑같이 가르키고 있는 경우라서
    • 클론을 만들고 repository.setUser("kkk");해서 바꾸면 클론도 영향을 받음
System.out.println(clone.getUrl());
  • 이렇게 하면 클론의 url에 바뀐것이 보이게됨

딥 복사 구현하는 법

  • before (얇은 복사)
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
  • after (딥 복사)
    @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;
    }

https://github.com/3DPIT/study/blob/master/05.%EC%BD%94%EB%94%A9%EC%9C%BC%EB%A1%9C%ED%95%99%EC%8A%B5%ED%95%98%EB%8A%94GoF%EC%9D%98%EB%94%94%EC%9E%90%EC%9D%B8%ED%8C%A8%ED%84%B4/01.%EA%B0%9D%EC%B2%B4%EC%83%9D%EC%84%B1%EA%B4%80%EB%A0%A8%EB%94%94%EC%9E%90%EC%9D%B8%ED%8C%A8%ED%84%B4/2021/12/1204/%ED%94%84%EB%A1%9C%ED%86%A0%ED%83%80%EC%9E%85%ED%8C%A8%ED%84%B4/02.%ED%8C%A8%ED%84%B4%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0/2021.12.04_%ED%94%84%EB%A1%9C%ED%86%A0%ED%83%80%EC%9E%85%ED%8C%A8%ED%84%B402.%ED%8C%A8%ED%84%B4%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0.md

 

GitHub - 3DPIT/study

Contribute to 3DPIT/study development by creating an account on GitHub.

github.com

 

728x90
반응형

댓글