From c907e511230387db1792e6465c82d2435a1a6c5b Mon Sep 17 00:00:00 2001 From: Heesoo Date: Mon, 10 Feb 2025 16:31:43 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat]=20exercise-polymorphism=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20pull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle/wrapper/gradle-wrapper.properties | 2 +- src/main/java/coordinate/AbstractFigure.java | 23 +++++++ src/main/java/coordinate/Figure.java | 10 +++ src/main/java/coordinate/FigureCreator.java | 7 ++ src/main/java/coordinate/FigureFactory.java | 21 ++++++ src/main/java/coordinate/Line.java | 26 +++++++ src/main/java/coordinate/Point.java | 68 +++++++++++++++++++ src/main/java/coordinate/Rectangle.java | 26 +++++++ src/main/java/coordinate/Triangle.java | 26 +++++++ .../java/coordinate/FigureFactoryTest.java | 46 +++++++++++++ 10 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 src/main/java/coordinate/AbstractFigure.java create mode 100644 src/main/java/coordinate/Figure.java create mode 100644 src/main/java/coordinate/FigureCreator.java create mode 100644 src/main/java/coordinate/FigureFactory.java create mode 100644 src/main/java/coordinate/Line.java create mode 100644 src/main/java/coordinate/Point.java create mode 100644 src/main/java/coordinate/Rectangle.java create mode 100644 src/main/java/coordinate/Triangle.java create mode 100644 src/test/java/coordinate/FigureFactoryTest.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0f80bbf..d2880ba 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/coordinate/AbstractFigure.java b/src/main/java/coordinate/AbstractFigure.java new file mode 100644 index 0000000..5ae244a --- /dev/null +++ b/src/main/java/coordinate/AbstractFigure.java @@ -0,0 +1,23 @@ +package coordinate; + +import java.util.List; + +public abstract class AbstractFigure implements Figure { + private final List points; + + public AbstractFigure(List points){ + if(points.size() != size()){ + throw new IllegalArgumentException(getName() + "의 길이는 " + size() + "이어야 합니다."); + } + this.points = points; + } + + protected Point getPoint(int index){ + return points.get(index); + } + + @Override + public List getPoints(){ + return points; + } +} diff --git a/src/main/java/coordinate/Figure.java b/src/main/java/coordinate/Figure.java new file mode 100644 index 0000000..9f76fee --- /dev/null +++ b/src/main/java/coordinate/Figure.java @@ -0,0 +1,10 @@ +package coordinate; + +import java.util.List; + +public interface Figure { + List getPoints(); + int size(); + String getName(); + double area(); +} diff --git a/src/main/java/coordinate/FigureCreator.java b/src/main/java/coordinate/FigureCreator.java new file mode 100644 index 0000000..6091046 --- /dev/null +++ b/src/main/java/coordinate/FigureCreator.java @@ -0,0 +1,7 @@ +package coordinate; + +import java.util.List; + +public interface FigureCreator { + Figure create(List points); +} diff --git a/src/main/java/coordinate/FigureFactory.java b/src/main/java/coordinate/FigureFactory.java new file mode 100644 index 0000000..25594c2 --- /dev/null +++ b/src/main/java/coordinate/FigureFactory.java @@ -0,0 +1,21 @@ +package coordinate; + +import java.util.List; + +public class FigureFactory { + static Figure getInstance(List points) { + if (points.size() == Line.LINE_POINT_SIZE) { + return new Line(points); + } + + if (points.size() == Triangle.TRIANGLE_POINT_SIZE) { + return new Triangle(points); + } + + if (points.size() == Rectangle.RECTANGLE_POINT_SIZE) { + return new Rectangle(points); + } + + throw new IllegalArgumentException("유효하지 않은 도형입니다."); + } +} \ No newline at end of file diff --git a/src/main/java/coordinate/Line.java b/src/main/java/coordinate/Line.java new file mode 100644 index 0000000..1b8d90a --- /dev/null +++ b/src/main/java/coordinate/Line.java @@ -0,0 +1,26 @@ +package coordinate; + +import java.util.List; + +public class Line extends AbstractFigure{ + public static final int LINE_POINT_SIZE = 2; + + public Line(List points) { + super(points); + } + + @Override + public int size() { + return LINE_POINT_SIZE; + } + + @Override + public String getName() { + return "선"; + } + + @Override + public double area() { + return 0; + } +} diff --git a/src/main/java/coordinate/Point.java b/src/main/java/coordinate/Point.java new file mode 100644 index 0000000..3439784 --- /dev/null +++ b/src/main/java/coordinate/Point.java @@ -0,0 +1,68 @@ +package coordinate; + +import java.util.Objects; + +public class Point { + private final int x; + private final int y; + + public Point(int x, int y) { + this.x = x; + if (x < 0 || x > 24){ + throw new IllegalArgumentException(); + } + + this.y = y; + if(y < 0 || y > 24){ + throw new IllegalArgumentException(); + } + } + + public double getDistance(Point other){ + int xDifference = other.minusX(x); + int yDifference = other.minusY(y); + return Math.sqrt(square(xDifference) + square(yDifference)); + } + + private int minusX(int number){ + return this.x - number; + } + + private int minusY(int number){ + return this.y - number; + } + + private static int square(int number){ + return number * number; + } + + public static Point of(int x, int y){ + return new Point(x, y); + } + + public static Point ofCommaSeparator(String text){ + String[] values = text.split(","); + return new Point(Integer.parseInt(values[0]), Integer.parseInt(values[1])); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Point point = (Point) o; + return x == point.x && y == point.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } + + @Override + public String toString() { + return "Point{" + + "x=" + x + + ", y=" + y + + '}'; + } +} diff --git a/src/main/java/coordinate/Rectangle.java b/src/main/java/coordinate/Rectangle.java new file mode 100644 index 0000000..debb650 --- /dev/null +++ b/src/main/java/coordinate/Rectangle.java @@ -0,0 +1,26 @@ +package coordinate; + +import java.util.List; + +public class Rectangle extends AbstractFigure{ + public static final int RECTANGLE_POINT_SIZE = 4; + + public Rectangle(List points) { + super(points); + } + + @Override + public int size() { + return RECTANGLE_POINT_SIZE; + } + + @Override + public String getName() { + return "사각형"; + } + + @Override + public double area() { + return 0; + } +} diff --git a/src/main/java/coordinate/Triangle.java b/src/main/java/coordinate/Triangle.java new file mode 100644 index 0000000..2ae5518 --- /dev/null +++ b/src/main/java/coordinate/Triangle.java @@ -0,0 +1,26 @@ +package coordinate; + +import java.util.List; + +public class Triangle extends AbstractFigure{ + public static final int TRIANGLE_POINT_SIZE = 3; + + public Triangle(List points) { + super(points); + } + + @Override + public int size() { + return TRIANGLE_POINT_SIZE; + } + + @Override + public String getName() { + return "삼각형"; + } + + @Override + public double area() { + return 0; + } +} diff --git a/src/test/java/coordinate/FigureFactoryTest.java b/src/test/java/coordinate/FigureFactoryTest.java new file mode 100644 index 0000000..a82b42a --- /dev/null +++ b/src/test/java/coordinate/FigureFactoryTest.java @@ -0,0 +1,46 @@ +package coordinate; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FigureFactoryTest { + @Test + public void line() { + List points = Arrays.asList( + Point.of(1, 2), + Point.of(2, 3)); + + Figure figure = FigureFactory.getInstance(points); + assertThat(figure).isInstanceOfAny(Line.class); + assertThat(figure.getName()).isEqualTo("선"); + } + + @Test + public void triangle() { + List points = Arrays.asList( + Point.of(1, 1), + Point.of(4, 1), + Point.of(1, 4)); + + Figure figure = FigureFactory.getInstance(points); + assertThat(figure).isInstanceOfAny(Triangle.class); + assertThat(figure.getName()).isEqualTo("삼각형"); + } + + @Test + public void rectangle() { + List points = Arrays.asList( + Point.of(1, 1), + Point.of(4, 1), + Point.of(1, 4), + Point.of(4, 4)); + + Figure figure = FigureFactory.getInstance(points); + assertThat(figure).isInstanceOfAny(Rectangle.class); + assertThat(figure.getName()).isEqualTo("사각형"); + } +} \ No newline at end of file From d635806130da6bd3a8d9e635fffd4a2db6521ff8 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Mon, 10 Feb 2025 17:17:39 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[update]=20FigureFactory=20=EB=8B=A4?= =?UTF-8?q?=ED=98=95=EC=84=B1=20=EC=A0=9C=EA=B1=B0=20=EC=8B=A4=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/FigureFactory.java | 30 ++++++++++------- .../java/coordinate/FigureFactoryTest.java | 32 +++++++++++++++---- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/java/coordinate/FigureFactory.java b/src/main/java/coordinate/FigureFactory.java index 25594c2..a802e5e 100644 --- a/src/main/java/coordinate/FigureFactory.java +++ b/src/main/java/coordinate/FigureFactory.java @@ -1,21 +1,29 @@ package coordinate; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.function.Function; -public class FigureFactory { - static Figure getInstance(List points) { - if (points.size() == Line.LINE_POINT_SIZE) { - return new Line(points); - } +public class FigureFactory implements FigureCreator{ + private static final Map, Figure>> creator; - if (points.size() == Triangle.TRIANGLE_POINT_SIZE) { - return new Triangle(points); - } + static{ + creator = new HashMap<>(); + creator.put(Line.LINE_POINT_SIZE, Line::new); + creator.put(Triangle.TRIANGLE_POINT_SIZE, Triangle::new); + creator.put(Rectangle.RECTANGLE_POINT_SIZE, Rectangle::new); + } - if (points.size() == Rectangle.RECTANGLE_POINT_SIZE) { - return new Rectangle(points); + @Override + public Figure create(List points) { + Function, Figure> figure = creator.get(points.size()); + + if(figure == null){ + throw new IllegalArgumentException("유효하지 않은 도형입니다."); } - throw new IllegalArgumentException("유효하지 않은 도형입니다."); + return figure.apply(points); } + } \ No newline at end of file diff --git a/src/test/java/coordinate/FigureFactoryTest.java b/src/test/java/coordinate/FigureFactoryTest.java index a82b42a..1a1a019 100644 --- a/src/test/java/coordinate/FigureFactoryTest.java +++ b/src/test/java/coordinate/FigureFactoryTest.java @@ -6,15 +6,18 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class FigureFactoryTest { + @Test public void line() { List points = Arrays.asList( Point.of(1, 2), - Point.of(2, 3)); + Point.of(2, 3) + ); - Figure figure = FigureFactory.getInstance(points); + Figure figure = new FigureFactory().create(points); assertThat(figure).isInstanceOfAny(Line.class); assertThat(figure.getName()).isEqualTo("선"); } @@ -24,9 +27,10 @@ public void triangle() { List points = Arrays.asList( Point.of(1, 1), Point.of(4, 1), - Point.of(1, 4)); + Point.of(1, 4) + ); - Figure figure = FigureFactory.getInstance(points); + Figure figure = new FigureFactory().create(points); assertThat(figure).isInstanceOfAny(Triangle.class); assertThat(figure.getName()).isEqualTo("삼각형"); } @@ -37,10 +41,26 @@ public void rectangle() { Point.of(1, 1), Point.of(4, 1), Point.of(1, 4), - Point.of(4, 4)); + Point.of(4, 4) + ); - Figure figure = FigureFactory.getInstance(points); + Figure figure = new FigureFactory().create(points); assertThat(figure).isInstanceOfAny(Rectangle.class); assertThat(figure.getName()).isEqualTo("사각형"); } + + @Test + public void illegalArgumentException(){ + List points = Arrays.asList( + Point.of(1, 1), + Point.of(4, 1), + Point.of(1, 4), + Point.of(4, 4), + Point.of(4, 22) + ); + + assertThatThrownBy(() -> new FigureFactory().create(points)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("유효하지 않은 도형입니다."); + } } \ No newline at end of file