[java] 6강 프로토 타입 패턴(Prototype Pattern)
https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EB%94%94%EC%9E%90%EC%9D%B8-%ED%8C%A8%ED%84%B4#
자바 디자인 패턴의 이해 - Gof Design Pattern - 인프런
자바 디자인 패턴 이해하기 강좌 입니다. 여러가지 디자인 패턴들을 알아보며 디자인 패턴에 대한 이해도를 높이도록 도와줍니다. Gof Design Pattern을 자바 언어로 설명한 강의. 의미 있고 쉬운 예제를 준비하려고 노력했습니다. 중급 프로그래밍 언어 Java MVC 온라인 강의 자바 디자인 패턴
www.inflearn.com
사전적 의미의 프로토타입(Prototype)이란?
원형, 원작을 의미한다. 사본이 있다면 그것에 대한 원본이 있기 마련이다.
프로토타입 패턴이란?
생산 비용이 높은 인스턴스를 복사를 통해 쉽게 생성할 수 있도록 하는 패턴을 말한다.
즉, 미리 만들어진 객체(Object)를 복사하여 객체를 생성하는 패턴이다.
이를 통해 객체의 생성 비용을 효과적으로 줄일 수 있다.
복사는 깊은 복사와 얕은 복사로 이루어져 있는데
자세한 사항은 여기를 참조해 주세요
프로토타입을 사용하는 경우
1. 종류가 너무 많아서 클래스로 정리되지 않는 경우
2. 클래스로부터 인스턴스 생성이 어려운 경우
요구사항
일러스트레이터와 같은 그림 그리기 툴을 개발 중입니다.
어떤 모양(Shape)을 그릴 수 있도록 하고 복사 붙여 넣기 기능을 구현해 주세요.
추가 요구사항
복사 후 붙여 넣기를 하면 두 도형이 겹치는데 안 겹치도록 살짝 옆으로 이동해 주세요
Prototype.Circle.java
public class Circle extends Shape{
private int x,y,r;
public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public Circle copy() throws CloneNotSupportedException {
Circle circle = (Circle) clone();
// 더 공부하기 ----
circle.x = x+1;
circle.y = y+1;
return circle;
}
}
Prototype.Shape
public class Shape implements Cloneable { // Cloneable은 이미 정의되어있는 인터페이스
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
ProtoType.Main
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Circle circle1 = new Circle(1, 1, 3);
Circle circle2 = circle1.copy();
System.out.println
(circle1.getX()+
", " +
circle1.getY() + ", "
+ circle1.getR());
System.out.println
(circle2.getX()+
", " +
circle2.getY() + ", "
+ circle2.getR());
}
}
참고
https://copynull.tistory.com/127
원형 패턴 (Prototype Pattern)
Prototype Pattern - 원형 패턴 미리 만들어진 개체(object)를 복사하여 개체를 생성하는 패턴이다. 다수의 객체 생성시에 발생되는 객체의 생성 비용을 효과적으로 줄일 수 있다. 샘플 코드 Colored By Color Sc..
copynull.tistory.com
http://knowledgebase.oasissofttech.com/index.php?title=Prototype_Design_Pattern
Prototype Design Pattern - OasisSoftTech.com - Knowledge Base/Java/Springframework/Microservices/Cloud-AWS/AI
From OasisSoftTech.com - Knowledge Base/Java/Springframework/Microservices/Cloud-AWS/AI Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Pr
knowledgebase.oasissofttech.com
소스코드
https://github.com/garam-park/java-designpattern
garam-park/java-designpattern
Java Design Patter Example for Coders in korean. Contribute to garam-park/java-designpattern development by creating an account on GitHub.
github.com