Added the code for Abstract factory pattern implementation and its unit test
This commit is contained in:
parent
4456658f82
commit
8841314c87
@ -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);
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package src.main.java.com.designpatterns.creational.abstractfactory;
|
||||
|
||||
public enum FactoryType {
|
||||
TWO_D_FACTORY,
|
||||
THREE_D_FACTORY
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package src.main.java.com.designpatterns.creational.abstractfactory;
|
||||
|
||||
public enum ShapeType {
|
||||
LINE,
|
||||
CIRCLE,
|
||||
SPHERE
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user