Prototype pattern #1

This commit is contained in:
kim
2021-01-11 18:05:42 +09:00
parent 9c2547019e
commit 12d96fec9e
4 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package Prototype;
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 Circle copy() throws CloneNotSupportedException {
Circle circle = (Circle) clone();
circle.x ++;
circle.y ++;
return circle;
}
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;
}
}

View File

@@ -0,0 +1,11 @@
package Prototype;
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());
}
}

View File

@@ -0,0 +1,10 @@
Prototype Pattern
- 프로토타입 패턴을 통해서 복잡한 인스턴스를 복사할 수 있다.
- 생산 비용이 높은 인스턴스를 복사를 통해서 쉽게 생성 할 수 있도록 하는 패턴
- 종류가 너무 많아서 클래스로 정리되지 않는 경우
- 클래스로부터 인스턴스를 생성이 어려운 경우
- cloneable 인터페이스를 상속받으면 object 클래스의 clone 메소드를 오버라이딩 하여 사용할 수 있다.
요구사항
- 그림그리기 툴에서 어떤 모양을 그릴 수 있도록 하고 복사 붙여너힉 기능 구현
- 복사 후 두 도형이 겹치지 않도록 이동

View File

@@ -0,0 +1,14 @@
package Prototype;
public class Shape implements Cloneable{
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}