Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8841314

Browse files
committed
Added the code for Abstract factory pattern implementation and its unit test
1 parent4456658 commit8841314

File tree

11 files changed

+175
-0
lines changed

11 files changed

+175
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagesrc.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+
publicabstractclassAbstractShapeFactory {
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+
publicabstractShapegetShape(ShapeTypename);
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassCircleimplementsShape {
4+
@Override
5+
publicdoublesurfaceArea(floatradius) {
6+
returnMath.PI *radius *radius;
7+
}
8+
9+
@Override
10+
publicShapeTypegetShapeType() {
11+
returnShapeType.CIRCLE;
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassFactoryProvider {
4+
publicstaticAbstractShapeFactorygetShapeFactory(FactoryTypefactoryType) {
5+
if (FactoryType.TWO_D_FACTORY ==factoryType) {
6+
returnnewTwoDShapeFactory();
7+
}elseif (FactoryType.THREE_D_FACTORY ==factoryType) {
8+
returnnewThreeDShapeFactory();
9+
}
10+
returnnull;
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicenumFactoryType {
4+
TWO_D_FACTORY,
5+
THREE_D_FACTORY
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassLineimplementsShape {
4+
@Override
5+
publicdoublesurfaceArea(floatradius) {
6+
return0;
7+
}
8+
9+
@Override
10+
publicShapeTypegetShapeType() {
11+
returnShapeType.LINE;
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicinterfaceShape {
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+
doublesurfaceArea(floatradius);
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+
ShapeTypegetShapeType();
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicenumShapeType {
4+
LINE,
5+
CIRCLE,
6+
SPHERE
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassSphereimplementsShape {
4+
@Override
5+
publicdoublesurfaceArea(floatradius) {
6+
return4 *Math.PI *radius *radius;
7+
}
8+
9+
@Override
10+
publicShapeTypegetShapeType() {
11+
returnShapeType.SPHERE;
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassThreeDShapeFactoryextendsAbstractShapeFactory {
4+
@Override
5+
publicShapegetShape(ShapeTypename) {
6+
if (ShapeType.SPHERE ==name) {
7+
returnnewSphere();
8+
}
9+
returnnull;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.creational.abstractfactory;
2+
3+
publicclassTwoDShapeFactoryextendsAbstractShapeFactory {
4+
@Override
5+
publicShapegetShape(ShapeTypename) {
6+
if (ShapeType.LINE ==name) {
7+
returnnewLine();
8+
}elseif (ShapeType.CIRCLE ==name) {
9+
returnnewCircle();
10+
}
11+
returnnull;
12+
}
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagesrc.test.java.com.designpatterns.creational.abstractfactory;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
importsrc.main.java.com.designpatterns.creational.abstractfactory.*;
6+
7+
publicclassAbstractShapeFactoryTest {
8+
@Test
9+
publicvoidtestAbstractShapeFactory() {
10+
StringfailReason ="";
11+
// Tests for 2-D shape factory
12+
// Test for Line
13+
AbstractShapeFactoryshapeFactory =FactoryProvider.getShapeFactory(FactoryType.TWO_D_FACTORY);
14+
Shapeshape =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)

[8]ページ先頭

©2009-2025 Movatter.jp