Skip to content

Added Factory pattern implementation in Java and its test #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2019
13 changes: 13 additions & 0 deletions src/main/java/com/designpatterns/factorypattern/Pentagon.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/designpatterns/factorypattern/Polygon.java
Original file line number Diff line number Diff line change
@@ -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);
}


Original file line number Diff line number Diff line change
@@ -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 <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Pattern</a>
*/
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;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/designpatterns/factorypattern/Square.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/designpatterns/factorypattern/Triangle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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, "");
}
}