ジェネレータアレイジェネレータアレイ:プロトタイプアレイプロトタイプアレイ


紹介する

  • の新しいインスタンスを作成するコストが非常に高い場合、既存のインスタンスをコピーして新しいインスタンスを作成できます.
  • JAVAでは、クローンインタフェースを受け入れることによってクローン方法を実現することができる.
  • クローンインタフェースは浅いコピーをサポートし、参照型資料についてはクローン方法の内部で直接コピーすることができる.
  • @Getter
    @Setter
    public class GithubIssue implements Cloneable {
    	private int id;
        private String title;
       	private GihubRepository 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());
        }
        
        @Override
        protected Override clone() throw CloneNotSupportedException {
        	//return super(this); -> 얕은 복사: GithubRepository는 주소만 가져온다.
            
            //deep copy
            GithubRepository repository = new GithubRepository();
            repository.setUser(this.repository.getUser());
            repository.setName(this.repository.getName());
            
            GithubRepository githubIssue = new GithubIssue(repository);
            githubIssue.setId(this.id);
            githubIssue.setTitle(this.title);
            
            return githubIssue;
        }
    }