File tree Expand file tree Collapse file tree 6 files changed +137
-0
lines changed
main/java/com/designpatterns/factorypattern
test/java/com/designpatterns/factorypattern Expand file tree Collapse file tree 6 files changed +137
-0
lines changed Original file line number Diff line number Diff line change 1+ package src .main .java .com .designpatterns .factorypattern ;
2+
3+ public class Pentagon implements Polygon {
4+ @ Override
5+ public String getType () {
6+ return "Pentagon" ;
7+ }
8+
9+ @ Override
10+ public double area (double side ) {
11+ return 3.847104 * side * side ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package src .main .java .com .designpatterns .factorypattern ;
2+
3+ public interface Polygon {
4+ /**
5+ * Should be overriden to describe the type of each polygon
6+ *
7+ * @return a String value describing the name of the polygon
8+ */
9+ String getType ();
10+
11+ /**
12+ * Calculates the area of the regular polygon
13+ *
14+ * @param side The length of the side of regular polygon
15+ * @return area of the polygon
16+ */
17+ double area (double side );
18+ }
19+
20+
Original file line number Diff line number Diff line change 1+ package src .main .java .com .designpatterns .factorypattern ;
2+
3+ /**
4+ * In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal
5+ * with the problem of creating objects without having to specify the exact class of the object that will be created.
6+ * This is done by creating objects by calling a factory method—either specified in an interface and implemented by
7+ * child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling
8+ * a constructor.
9+ *
10+ * @see <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Pattern</a>
11+ */
12+ public class PolygonFactory {
13+ /**
14+ * Factory pattern implementation for the Polygon Interface to return the correct regular polygon object
15+ * depending on the number of sides it has.
16+ *
17+ * @param numberOfSides in the polygon to initialize.
18+ * @return the object having the respective number of sides
19+ */
20+ public Polygon getPolygon (int numberOfSides ) {
21+ switch (numberOfSides ) {
22+ case 3 :
23+ return new Triangle ();
24+ case 4 :
25+ return new Square ();
26+ case 5 :
27+ return new Pentagon ();
28+ default :
29+ return null ;
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package src .main .java .com .designpatterns .factorypattern ;
2+
3+ public class Square implements Polygon {
4+
5+ @ Override
6+ public String getType () {
7+ return "Square" ;
8+ }
9+
10+ @ Override
11+ public double area (double side ) {
12+ return side * side ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package src .main .java .com .designpatterns .factorypattern ;
2+
3+ public class Triangle implements Polygon {
4+ @ Override
5+ public String getType () {
6+ return "Triangle" ;
7+ }
8+
9+ @ Override
10+ public double area (double side ) {
11+ return 0.433013 * side * side ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package src .test .java .com .designpatterns .factorypattern ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Test ;
5+ import src .main .java .com .designpatterns .factorypattern .Polygon ;
6+ import src .main .java .com .designpatterns .factorypattern .PolygonFactory ;
7+
8+ public class PolygonFactoryTest {
9+ @ Test
10+ public void testPolygonFactory () {
11+ String failReason = "" ;
12+ PolygonFactory polFactory = new PolygonFactory ();
13+
14+ // Test for triangle
15+ Polygon triangle = polFactory .getPolygon (3 );
16+ if (!"Triangle" .equals (triangle .getType ())) {
17+ failReason += "Polygon Factory failed for Triangle." ;
18+ }
19+ if (triangle .area (4 ) != 6.928208 ) {
20+ failReason += "Triangle area is incorrect!" ;
21+ }
22+
23+
24+ // Test for square
25+ Polygon square = polFactory .getPolygon (4 );
26+ if (!"Square" .equals (square .getType ())) {
27+ failReason += "Polygon Factory failed for Square." ;
28+ }
29+ if (square .area (5 ) != 25 ) {
30+ failReason += "Square area is incorrect!" ;
31+ }
32+
33+
34+ // Test for pentagon
35+ Polygon pentagon = polFactory .getPolygon (5 );
36+ if (!"Pentagon" .equals (pentagon .getType ())) {
37+ failReason += "Polygon Factory failed for Pentagon." ;
38+ }
39+ if (pentagon .area (9 ) != 311.615424 ) {
40+ failReason += "Pentagon area is incorrect!" ;
41+ }
42+
43+ Assert .assertEquals (failReason , failReason , "" );
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments