diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java new file mode 100644 index 00000000..f48f80c1 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java @@ -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. + *

+ * 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 Abstract Factory Pattern + */ +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); +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java b/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java new file mode 100644 index 00000000..47f277a6 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java @@ -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; + } +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java new file mode 100644 index 00000000..40716aa7 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java @@ -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; + } +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java new file mode 100644 index 00000000..8ea56aa8 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java @@ -0,0 +1,6 @@ +package src.main.java.com.designpatterns.creational.abstractfactory; + +public enum FactoryType { + TWO_D_FACTORY, + THREE_D_FACTORY +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Line.java b/src/main/java/com/designpatterns/creational/abstractfactory/Line.java new file mode 100644 index 00000000..620037c3 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Line.java @@ -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; + } +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java b/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java new file mode 100644 index 00000000..5e741319 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java @@ -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(); +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java b/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java new file mode 100644 index 00000000..8fc6ac9b --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java @@ -0,0 +1,7 @@ +package src.main.java.com.designpatterns.creational.abstractfactory; + +public enum ShapeType { + LINE, + CIRCLE, + SPHERE +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java b/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java new file mode 100644 index 00000000..01b530c3 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java @@ -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; + } +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java new file mode 100644 index 00000000..2a23234f --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java @@ -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; + } +} diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java new file mode 100644 index 00000000..5d553ef8 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java @@ -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; + } +} diff --git a/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java b/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java new file mode 100644 index 00000000..96e4e34b --- /dev/null +++ b/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java @@ -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); + + } +} diff --git a/src/test/java/com/others/FastPowerTest.java b/src/test/java/com/others/FastPowerTest.java index c3f504ed..5824dae2 100644 --- a/src/test/java/com/others/FastPowerTest.java +++ b/src/test/java/com/others/FastPowerTest.java @@ -9,14 +9,12 @@ import static org.junit.Assert.*; 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)); }