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

Commit43d4722

Browse files
committed
Added Factory Design pattern implementation in Java and its test
1 parent4a3f871 commit43d4722

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.factorypattern;
2+
3+
publicclassPentagonimplementsPolygon {
4+
@Override
5+
publicStringgetType() {
6+
return"Pentagon";
7+
}
8+
9+
@Override
10+
publicdoublearea(doubleside) {
11+
return3.847104 *side *side;
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagesrc.main.java.com.designpatterns.factorypattern;
2+
3+
publicinterfacePolygon {
4+
/**
5+
* Should be overriden to describe the type of each polygon
6+
*
7+
* @return a String value describing the name of the polygon
8+
*/
9+
StringgetType();
10+
11+
/**
12+
* Calculates the area of the regular polygon
13+
*
14+
* @param side The length of the side of regular polygon
15+
* @return area of the polygon
16+
*/
17+
doublearea(doubleside);
18+
}
19+
20+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagesrc.main.java.com.designpatterns.factorypattern;
2+
3+
/**
4+
* In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal
5+
* with the problem of creating objects without having to specify the exact class of the object that will be created.
6+
* This is done by creating objects by calling a factory method—either specified in an interface and implemented by
7+
* child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling
8+
* a constructor.
9+
*
10+
* @see <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Pattern</a>
11+
*/
12+
publicclassPolygonFactory {
13+
/**
14+
* Factory pattern implementation for the Polygon Interface to return the correct regular polygon object
15+
* depending on the number of sides it has.
16+
*
17+
* @param numberOfSides in the polygon to initialize.
18+
* @return the object having the respective number of sides
19+
*/
20+
publicPolygongetPolygon(intnumberOfSides) {
21+
switch (numberOfSides) {
22+
case3:
23+
returnnewTriangle();
24+
case4:
25+
returnnewSquare();
26+
case5:
27+
returnnewPentagon();
28+
default:
29+
returnnull;
30+
}
31+
}
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
packagesrc.main.java.com.designpatterns.factorypattern;
2+
3+
publicclassSquareimplementsPolygon {
4+
5+
@Override
6+
publicStringgetType() {
7+
return"Square";
8+
}
9+
10+
@Override
11+
publicdoublearea(doubleside) {
12+
returnside *side;
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagesrc.main.java.com.designpatterns.factorypattern;
2+
3+
publicclassTriangleimplementsPolygon {
4+
@Override
5+
publicStringgetType() {
6+
return"Triangle";
7+
}
8+
9+
@Override
10+
publicdoublearea(doubleside) {
11+
return0.433013 *side *side;
12+
}
13+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagesrc.test.java.com.designpatterns.factorypattern;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
importsrc.main.java.com.designpatterns.factorypattern.Polygon;
6+
importsrc.main.java.com.designpatterns.factorypattern.PolygonFactory;
7+
8+
publicclassPolygonFactoryTest {
9+
@Test
10+
publicvoidtestPolygonFactory() {
11+
StringfailReason ="";
12+
PolygonFactorypolFactory =newPolygonFactory();
13+
14+
//Test for triangle
15+
Polygontriangle =polFactory.getPolygon(3);
16+
if (!triangle.getType().equals("Triangle")) {
17+
failReason +="Polygon Factory failed for Triangle.";
18+
}
19+
if (triangle.area(4) !=6.928208) {
20+
failReason +="Triangle area is incorrect!";
21+
}
22+
23+
24+
//Test for square
25+
Polygonsquare =polFactory.getPolygon(4);
26+
if (!square.getType().equals("Square")) {
27+
failReason +="Polygon Factory failed for Square.";
28+
}
29+
if (square.area(5) !=25) {
30+
failReason +="Square area is incorrect!";
31+
}
32+
33+
34+
//Test for pentagon
35+
Polygonpentagon =polFactory.getPolygon(5);
36+
if (!pentagon.getType().equals("Pentagon")) {
37+
failReason +="Polygon Factory failed for Pentagon.";
38+
}
39+
if (pentagon.area(9) !=311.615424) {
40+
failReason +="Pentagon area is incorrect!";
41+
}
42+
43+
Assert.assertEquals(failReason,failReason,"");
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp