Merge pull request #767 from abhijay94/Development

Added the code for Abstract factory pattern implementation and JUnit
This commit is contained in:
Libin Yang 2019-05-24 16:47:04 +08:00 committed by GitHub
commit fc421591fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 177 additions and 4 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,6 @@
package src.main.java.com.designpatterns.creational.abstractfactory;
public enum FactoryType {
TWO_D_FACTORY,
THREE_D_FACTORY
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -0,0 +1,7 @@
package src.main.java.com.designpatterns.creational.abstractfactory;
public enum ShapeType {
LINE,
CIRCLE,
SPHERE
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -9,14 +9,12 @@ import static org.junit.Assert.*;
public class FastPowerTest { public class FastPowerTest {
@Test private void testLong(long n, long k, long m) {
void testLong(long n, long k, long m) {
long result = FastPower.calculate(n, k, m); long result = FastPower.calculate(n, k, m);
assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue()); assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue());
} }
@Test private void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
BigInteger result = FastPower.calculate(n, k, m); BigInteger result = FastPower.calculate(n, k, m);
assertEquals(result, n.modPow(k, m)); assertEquals(result, n.modPow(k, m));
} }