Skip to content

Added the code for Abstract factory pattern implementation and JUnit #767

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 2 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

/**
* The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
* of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories,
* since it uses only the generic interfaces of their products.
* <p>
* This pattern separates the details of implementation of a set of objects from their general usage and relies on
* object composition, as object creation is implemented in methods exposed in the factory interface.
*
* @see <a href="https://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory Pattern</a>
*/
public abstract class AbstractShapeFactory {
/**
* Creates the appropriate shape object depending on the type of the shape
*
* @param name enum defining the name of the shape
* @return shape object
*/
public abstract Shape getShape(ShapeType name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class Circle implements Shape {
@Override
public double surfaceArea(float radius) {
return Math.PI * radius * radius;
}

@Override
public ShapeType getShapeType() {
return ShapeType.CIRCLE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class FactoryProvider {
public static AbstractShapeFactory getShapeFactory(FactoryType factoryType) {
if (FactoryType.TWO_D_FACTORY == factoryType) {
return new TwoDShapeFactory();
} else if (FactoryType.THREE_D_FACTORY == factoryType) {
return new ThreeDShapeFactory();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public enum FactoryType {
TWO_D_FACTORY,
THREE_D_FACTORY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class Line implements Shape {
@Override
public double surfaceArea(float radius) {
return 0;
}

@Override
public ShapeType getShapeType() {
return ShapeType.LINE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public interface Shape {
/**
* calculates the surface area for the shape object
*
* @param radius the radius or length of shape whose area is to be calculated
* @return total surface area for the shape
*/
double surfaceArea(float radius);

/**
* A property to identity the type of the shape for testing the pattern
*
* @return an enum describing the shape type
*/
ShapeType getShapeType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public enum ShapeType {
LINE,
CIRCLE,
SPHERE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class Sphere implements Shape {
@Override
public double surfaceArea(float radius) {
return 4 * Math.PI * radius * radius;
}

@Override
public ShapeType getShapeType() {
return ShapeType.SPHERE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class ThreeDShapeFactory extends AbstractShapeFactory {
@Override
public Shape getShape(ShapeType name) {
if (ShapeType.SPHERE == name) {
return new Sphere();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package src.main.java.com.designpatterns.creational.abstractfactory;

public class TwoDShapeFactory extends AbstractShapeFactory {
@Override
public Shape getShape(ShapeType name) {
if (ShapeType.LINE == name) {
return new Line();
} else if (ShapeType.CIRCLE == name) {
return new Circle();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package src.test.java.com.designpatterns.creational.abstractfactory;

import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.designpatterns.creational.abstractfactory.*;

public class AbstractShapeFactoryTest {
@Test
public void testAbstractShapeFactory() {
String failReason = "";
// Tests for 2-D shape factory
// Test for Line
AbstractShapeFactory shapeFactory = FactoryProvider.getShapeFactory(FactoryType.TWO_D_FACTORY);
Shape shape = shapeFactory.getShape(ShapeType.LINE);
if (shape.getShapeType() != ShapeType.LINE) {
failReason += "Could not create an object for LINE.\n";
}
if (shape.surfaceArea(5) != 0) {
failReason += "Surface area of Line is incorrect!.\n";
}

// Test for circle
shape = shapeFactory.getShape(ShapeType.CIRCLE);
if (shape.getShapeType() != ShapeType.CIRCLE) {
failReason += "Could not create an object for CIRCLE.\n";
}
if (shape.surfaceArea(9) != 254.46900494077323) {
failReason += "Surface area of Circle is incorrect!.\n";
}

// Test for 3-D shape factory
// Test for Sphere
shapeFactory = FactoryProvider.getShapeFactory(FactoryType.THREE_D_FACTORY);
shape = shapeFactory.getShape(ShapeType.SPHERE);

if (shape.getShapeType() != ShapeType.SPHERE) {
failReason += "Could not create and object for SPHERE.\n";
}
if (shape.surfaceArea(6) != 452.3893421169302) {
failReason += "Surface area of Sphere is incorrect!.\n";
}

Assert.assertEquals(failReason, "", failReason);

}
}
6 changes: 2 additions & 4 deletions src/test/java/com/others/FastPowerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@

public class FastPowerTest {

@Test
void testLong(long n, long k, long m) {
private void testLong(long n, long k, long m) {
long result = FastPower.calculate(n, k, m);
assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue());
}

@Test
void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
private void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
BigInteger result = FastPower.calculate(n, k, m);
assertEquals(result, n.modPow(k, m));
}
Expand Down