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

Commitba90059

Browse files
committed
Added the prototype pattern and its unit test
1 parentee985d5 commitba90059

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-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.creational.prototype;
2+
3+
classBlackColorextendsColor {
4+
5+
BlackColor() {
6+
this.colorName ="black";
7+
}
8+
9+
@Override
10+
publicStringaddColor() {
11+
return"Black color added";
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packagesrc.main.java.com.designpatterns.creational.prototype;
2+
3+
classBlueColorextendsColor {
4+
5+
BlueColor() {
6+
this.colorName ="blue";
7+
}
8+
9+
@Override
10+
publicStringaddColor() {
11+
return"Blue color added";
12+
}
13+
14+
}
15+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagesrc.main.java.com.designpatterns.creational.prototype;
2+
3+
/**
4+
* The prototype pattern is used when the type of objects to create is determined by a prototypical instance, which
5+
* is cloned to produce new objects. <p>
6+
* This pattern is used to:
7+
* 1. avoid subclasses of an object creator in the client application, like the factory method pattern does.
8+
* 2. avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is
9+
* prohibitively expensive for a given application.
10+
*
11+
* @see <a href="https://en.wikipedia.org/wiki/Prototype_pattern">Prototype Pattern</a>
12+
*/
13+
publicabstractclassColorimplementsCloneable {
14+
15+
StringcolorName;
16+
17+
publicabstractStringaddColor();
18+
19+
/**
20+
* This method should be called from the client instead of writing code that invokes the "new" operator on a
21+
* hard-coded class name.
22+
*
23+
* @return a clone for the object
24+
*/
25+
publicObjectclone() {
26+
Objectclone =null;
27+
try {
28+
clone =super.clone();
29+
}catch (CloneNotSupportedExceptione) {
30+
e.printStackTrace();
31+
}
32+
returnclone;
33+
}
34+
}
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.prototype;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclassColorStore {
7+
privatestaticMap<String,Color>colorMap =newHashMap<>();
8+
9+
static {
10+
colorMap.put("blue",newBlueColor());
11+
colorMap.put("black",newBlackColor());
12+
colorMap.put("red",newRedColor());
13+
}
14+
15+
publicstaticColorgetColor(StringcolorName) {
16+
return (Color)colorMap.get(colorName).clone();
17+
}
18+
}
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.prototype;
2+
3+
classRedColorextendsColor {
4+
5+
RedColor() {
6+
this.colorName ="red";
7+
}
8+
9+
@Override
10+
publicStringaddColor() {
11+
return"Red color added";
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagesrc.test.java.com.designpatterns.creational.prototype;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
importsrc.main.java.com.designpatterns.creational.prototype.ColorStore;
6+
7+
publicclassPrototypeTest {
8+
@Test
9+
publicvoidtestPrototype() {
10+
StringtestFailReason ="";
11+
StringtestOne =ColorStore.getColor("blue").addColor();
12+
if (!"Blue color added".equals(testOne)) {
13+
testFailReason +="TC 1 Failed: Blue couldn't be added\n";
14+
}
15+
StringtestTwo =ColorStore.getColor("black").addColor();
16+
if (!"Black color added".equals(testTwo)) {
17+
testFailReason +="TC 2 Failed: Black couldn't be added\n";
18+
}
19+
StringtestThree =ColorStore.getColor("red").addColor();
20+
if (!"Red color added".equals(testThree)) {
21+
testFailReason +="TC 3 Failed: Red couldn't be added\n";
22+
}
23+
StringtestFour =ColorStore.getColor("blue").addColor();
24+
if (!"Blue color added".equals(testFour)) {
25+
testFailReason +="TC 4 Failed: Blue couldn't be added\n";
26+
}
27+
Assert.assertEquals(testFailReason,"",testFailReason);
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp