Merge pull request #756 from abhijay94/Development
Added Factory pattern implementation in Java and its test
This commit is contained in:
commit
21f82001dd
@ -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
src/main/java/com/designpatterns/factorypattern/Polygon.java
Normal file
20
src/main/java/com/designpatterns/factorypattern/Polygon.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
src/main/java/com/designpatterns/factorypattern/Square.java
Normal file
14
src/main/java/com/designpatterns/factorypattern/Square.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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, "");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user