Skip to content

Commit 8841314

Browse files
committed
Added the code for Abstract factory pattern implementation and its unit test
1 parent 4456658 commit 8841314

File tree

11 files changed

+175
-0
lines changed

11 files changed

+175
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
/**
4+
* The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme
5+
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
6+
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
7+
* of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories,
8+
* since it uses only the generic interfaces of their products.
9+
* <p>
10+
* This pattern separates the details of implementation of a set of objects from their general usage and relies on
11+
* object composition, as object creation is implemented in methods exposed in the factory interface.
12+
*
13+
* @see <a href="https://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory Pattern</a>
14+
*/
15+
public abstract class AbstractShapeFactory {
16+
/**
17+
* Creates the appropriate shape object depending on the type of the shape
18+
*
19+
* @param name enum defining the name of the shape
20+
* @return shape object
21+
*/
22+
public abstract Shape getShape(ShapeType name);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class Circle implements Shape {
4+
@Override
5+
public double surfaceArea(float radius) {
6+
return Math.PI * radius * radius;
7+
}
8+
9+
@Override
10+
public ShapeType getShapeType() {
11+
return ShapeType.CIRCLE;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class FactoryProvider {
4+
public static AbstractShapeFactory getShapeFactory(FactoryType factoryType) {
5+
if (FactoryType.TWO_D_FACTORY == factoryType) {
6+
return new TwoDShapeFactory();
7+
} else if (FactoryType.THREE_D_FACTORY == factoryType) {
8+
return new ThreeDShapeFactory();
9+
}
10+
return null;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public enum FactoryType {
4+
TWO_D_FACTORY,
5+
THREE_D_FACTORY
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class Line implements Shape {
4+
@Override
5+
public double surfaceArea(float radius) {
6+
return 0;
7+
}
8+
9+
@Override
10+
public ShapeType getShapeType() {
11+
return ShapeType.LINE;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public interface Shape {
4+
/**
5+
* calculates the surface area for the shape object
6+
*
7+
* @param radius the radius or length of shape whose area is to be calculated
8+
* @return total surface area for the shape
9+
*/
10+
double surfaceArea(float radius);
11+
12+
/**
13+
* A property to identity the type of the shape for testing the pattern
14+
*
15+
* @return an enum describing the shape type
16+
*/
17+
ShapeType getShapeType();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public enum ShapeType {
4+
LINE,
5+
CIRCLE,
6+
SPHERE
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class Sphere implements Shape {
4+
@Override
5+
public double surfaceArea(float radius) {
6+
return 4 * Math.PI * radius * radius;
7+
}
8+
9+
@Override
10+
public ShapeType getShapeType() {
11+
return ShapeType.SPHERE;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class ThreeDShapeFactory extends AbstractShapeFactory {
4+
@Override
5+
public Shape getShape(ShapeType name) {
6+
if (ShapeType.SPHERE == name) {
7+
return new Sphere();
8+
}
9+
return null;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
public class TwoDShapeFactory extends AbstractShapeFactory {
4+
@Override
5+
public Shape getShape(ShapeType name) {
6+
if (ShapeType.LINE == name) {
7+
return new Line();
8+
} else if (ShapeType.CIRCLE == name) {
9+
return new Circle();
10+
}
11+
return null;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package src.test.java.com.designpatterns.creational.abstractfactory;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.designpatterns.creational.abstractfactory.*;
6+
7+
public class AbstractShapeFactoryTest {
8+
@Test
9+
public void testAbstractShapeFactory() {
10+
String failReason = "";
11+
// Tests for 2-D shape factory
12+
// Test for Line
13+
AbstractShapeFactory shapeFactory = FactoryProvider.getShapeFactory(FactoryType.TWO_D_FACTORY);
14+
Shape shape = shapeFactory.getShape(ShapeType.LINE);
15+
if (shape.getShapeType() != ShapeType.LINE) {
16+
failReason += "Could not create an object for LINE.\n";
17+
}
18+
if (shape.surfaceArea(5) != 0) {
19+
failReason += "Surface area of Line is incorrect!.\n";
20+
}
21+
22+
// Test for circle
23+
shape = shapeFactory.getShape(ShapeType.CIRCLE);
24+
if (shape.getShapeType() != ShapeType.CIRCLE) {
25+
failReason += "Could not create an object for CIRCLE.\n";
26+
}
27+
if (shape.surfaceArea(9) != 254.46900494077323) {
28+
failReason += "Surface area of Circle is incorrect!.\n";
29+
}
30+
31+
// Test for 3-D shape factory
32+
// Test for Sphere
33+
shapeFactory = FactoryProvider.getShapeFactory(FactoryType.THREE_D_FACTORY);
34+
shape = shapeFactory.getShape(ShapeType.SPHERE);
35+
36+
if (shape.getShapeType() != ShapeType.SPHERE) {
37+
failReason += "Could not create and object for SPHERE.\n";
38+
}
39+
if (shape.surfaceArea(6) != 452.3893421169302) {
40+
failReason += "Surface area of Sphere is incorrect!.\n";
41+
}
42+
43+
Assert.assertEquals(failReason, "", failReason);
44+
45+
}
46+
}

0 commit comments

Comments
 (0)