未验证 提交 fc421591 编写于 作者: L Libin Yang 提交者: GitHub

Merge pull request #767 from abhijay94/Development

Added the code for Abstract factory pattern implementation and JUnit
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);
}
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;
}
}
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;
}
}
package src.main.java.com.designpatterns.creational.abstractfactory;
public enum FactoryType {
TWO_D_FACTORY,
THREE_D_FACTORY
}
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;
}
}
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();
}
package src.main.java.com.designpatterns.creational.abstractfactory;
public enum ShapeType {
LINE,
CIRCLE,
SPHERE
}
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;
}
}
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;
}
}
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;
}
}
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);
}
}
......@@ -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));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册