diff --git a/src/main/java/com/designpatterns/factorypattern/Pentagon.java b/src/main/java/com/designpatterns/factorypattern/Pentagon.java new file mode 100644 index 00000000..a7ad7c3d --- /dev/null +++ b/src/main/java/com/designpatterns/factorypattern/Pentagon.java @@ -0,0 +1,13 @@ +package src.main.java.com.designpatterns.factorypattern; + +public class Pentagon implements Polygon { + @Override + public String getType() { + return "Pentagon"; + } + + @Override + public double area(double side) { + return 3.847104 * side * side; + } +} diff --git a/src/main/java/com/designpatterns/factorypattern/Polygon.java b/src/main/java/com/designpatterns/factorypattern/Polygon.java new file mode 100644 index 00000000..415d6734 --- /dev/null +++ b/src/main/java/com/designpatterns/factorypattern/Polygon.java @@ -0,0 +1,20 @@ +package src.main.java.com.designpatterns.factorypattern; + +public interface Polygon { + /** + * Should be overriden to describe the type of each polygon + * + * @return a String value describing the name of the polygon + */ + String getType(); + + /** + * Calculates the area of the regular polygon + * + * @param side The length of the side of regular polygon + * @return area of the polygon + */ + double area(double side); +} + + diff --git a/src/main/java/com/designpatterns/factorypattern/PolygonFactory.java b/src/main/java/com/designpatterns/factorypattern/PolygonFactory.java new file mode 100644 index 00000000..08dc9e42 --- /dev/null +++ b/src/main/java/com/designpatterns/factorypattern/PolygonFactory.java @@ -0,0 +1,32 @@ +package src.main.java.com.designpatterns.factorypattern; + +/** + * In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal + * with the problem of creating objects without having to specify the exact class of the object that will be created. + * This is done by creating objects by calling a factory method—either specified in an interface and implemented by + * child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling + * a constructor. + * + * @see Factory Pattern + */ +public class PolygonFactory { + /** + * Factory pattern implementation for the Polygon Interface to return the correct regular polygon object + * depending on the number of sides it has. + * + * @param numberOfSides in the polygon to initialize. + * @return the object having the respective number of sides + */ + public Polygon getPolygon(int numberOfSides) { + switch (numberOfSides) { + case 3: + return new Triangle(); + case 4: + return new Square(); + case 5: + return new Pentagon(); + default: + return null; + } + } +} diff --git a/src/main/java/com/designpatterns/factorypattern/Square.java b/src/main/java/com/designpatterns/factorypattern/Square.java new file mode 100644 index 00000000..30f9d234 --- /dev/null +++ b/src/main/java/com/designpatterns/factorypattern/Square.java @@ -0,0 +1,14 @@ +package src.main.java.com.designpatterns.factorypattern; + +public class Square implements Polygon { + + @Override + public String getType() { + return "Square"; + } + + @Override + public double area(double side) { + return side * side; + } +} diff --git a/src/main/java/com/designpatterns/factorypattern/Triangle.java b/src/main/java/com/designpatterns/factorypattern/Triangle.java new file mode 100644 index 00000000..69beb14e --- /dev/null +++ b/src/main/java/com/designpatterns/factorypattern/Triangle.java @@ -0,0 +1,13 @@ +package src.main.java.com.designpatterns.factorypattern; + +public class Triangle implements Polygon { + @Override + public String getType() { + return "Triangle"; + } + + @Override + public double area(double side) { + return 0.433013 * side * side; + } +} diff --git a/src/test/java/com/designpatterns/factorypattern/PolygonFactoryTest.java b/src/test/java/com/designpatterns/factorypattern/PolygonFactoryTest.java new file mode 100644 index 00000000..c175911b --- /dev/null +++ b/src/test/java/com/designpatterns/factorypattern/PolygonFactoryTest.java @@ -0,0 +1,45 @@ +package src.test.java.com.designpatterns.factorypattern; + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.designpatterns.factorypattern.Polygon; +import src.main.java.com.designpatterns.factorypattern.PolygonFactory; + +public class PolygonFactoryTest { + @Test + public void testPolygonFactory() { + String failReason = ""; + PolygonFactory polFactory = new PolygonFactory(); + + // Test for triangle + Polygon triangle = polFactory.getPolygon(3); + if (!"Triangle".equals(triangle.getType())) { + failReason += "Polygon Factory failed for Triangle."; + } + if (triangle.area(4) != 6.928208) { + failReason += "Triangle area is incorrect!"; + } + + + // Test for square + Polygon square = polFactory.getPolygon(4); + if (!"Square".equals(square.getType())) { + failReason += "Polygon Factory failed for Square."; + } + if (square.area(5) != 25) { + failReason += "Square area is incorrect!"; + } + + + // Test for pentagon + Polygon pentagon = polFactory.getPolygon(5); + if (!"Pentagon".equals(pentagon.getType())) { + failReason += "Polygon Factory failed for Pentagon."; + } + if (pentagon.area(9) != 311.615424) { + failReason += "Pentagon area is incorrect!"; + } + + Assert.assertEquals(failReason, failReason, ""); + } +}