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

Commite7b119c

Browse files
authored
Merge pull requestiluwatar#709 from mookkiah/issue_508_prototype
issue 508 - using copy constructor to implement prototype.
2 parentsec28b12 +0805716 commite7b119c

File tree

14 files changed

+96
-47
lines changed

14 files changed

+96
-47
lines changed

‎prototype/src/main/java/com/iluwatar/prototype/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public static void main(String[] args) {
5252
Warlordwarlord;
5353
Beastbeast;
5454

55-
factory =newHeroFactoryImpl(newElfMage(),newElfWarlord(),newElfBeast());
55+
factory =newHeroFactoryImpl(newElfMage("cooking"),newElfWarlord("cleaning"),newElfBeast("protecting"));
5656
mage =factory.createMage();
5757
warlord =factory.createWarlord();
5858
beast =factory.createBeast();
5959
LOGGER.info(mage.toString());
6060
LOGGER.info(warlord.toString());
6161
LOGGER.info(beast.toString());
6262

63-
factory =newHeroFactoryImpl(newOrcMage(),newOrcWarlord(),newOrcBeast());
63+
factory =newHeroFactoryImpl(newOrcMage("axe"),newOrcWarlord("sword"),newOrcBeast("laser"));
6464
mage =factory.createMage();
6565
warlord =factory.createWarlord();
6666
beast =factory.createBeast();

‎prototype/src/main/java/com/iluwatar/prototype/Beast.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
publicabstractclassBeastextendsPrototype {
3131

3232
@Override
33-
publicabstractBeastclone()throwsCloneNotSupportedException;
33+
publicabstractBeastcopy()throwsCloneNotSupportedException;
3434

3535
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,25 @@
2828
*
2929
*/
3030
publicclassElfBeastextendsBeast {
31+
32+
privateStringhelpType;
3133

32-
publicElfBeast() {}
34+
publicElfBeast(StringhelpType) {
35+
this.helpType =helpType;
36+
}
37+
38+
publicElfBeast(ElfBeastelfBeast) {
39+
this.helpType =elfBeast.helpType;
40+
}
3341

3442
@Override
35-
publicBeastclone()throwsCloneNotSupportedException {
36-
returnnewElfBeast();
43+
publicBeastcopy()throwsCloneNotSupportedException {
44+
returnnewElfBeast(this);
3745
}
3846

3947
@Override
4048
publicStringtoString() {
41-
return"Elven eagle";
49+
return"Elven eagle helps in " +helpType;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfMage.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@
2929
*/
3030
publicclassElfMageextendsMage {
3131

32-
publicElfMage() {}
32+
33+
privateStringhelpType;
34+
35+
publicElfMage(StringhelpType) {
36+
this.helpType =helpType;
37+
}
38+
39+
publicElfMage(ElfMageelfMage) {
40+
this.helpType =elfMage.helpType;
41+
}
3342

3443
@Override
35-
publicMageclone()throwsCloneNotSupportedException {
36-
returnnewElfMage();
44+
publicElfMagecopy()throwsCloneNotSupportedException {
45+
returnnewElfMage(this);
3746
}
3847

3948
@Override
4049
publicStringtoString() {
41-
return"Elven mage";
50+
return"Elven mage helps in " +helpType;
4251
}
4352

4453
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
publicclassElfWarlordextendsWarlord {
3131

32-
publicElfWarlord() {}
32+
privateStringhelpType;
33+
34+
publicElfWarlord(StringhelpType) {
35+
this.helpType =helpType;
36+
}
37+
38+
publicElfWarlord(ElfWarlordelfWarlord) {
39+
this.helpType =elfWarlord.helpType;
40+
}
3341

3442
@Override
35-
publicWarlordclone()throwsCloneNotSupportedException {
36-
returnnewElfWarlord();
43+
publicElfWarlordcopy()throwsCloneNotSupportedException {
44+
returnnewElfWarlord(this);
3745
}
3846

3947
@Override
4048
publicStringtoString() {
41-
return"Elven warlord";
49+
return"Elven warlord helps in " +helpType;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
4747
*/
4848
publicMagecreateMage() {
4949
try {
50-
returnmage.clone();
50+
returnmage.copy();
5151
}catch (CloneNotSupportedExceptione) {
5252
returnnull;
5353
}
@@ -58,7 +58,7 @@ public Mage createMage() {
5858
*/
5959
publicWarlordcreateWarlord() {
6060
try {
61-
returnwarlord.clone();
61+
returnwarlord.copy();
6262
}catch (CloneNotSupportedExceptione) {
6363
returnnull;
6464
}
@@ -69,7 +69,7 @@ public Warlord createWarlord() {
6969
*/
7070
publicBeastcreateBeast() {
7171
try {
72-
returnbeast.clone();
72+
returnbeast.copy();
7373
}catch (CloneNotSupportedExceptione) {
7474
returnnull;
7575
}

‎prototype/src/main/java/com/iluwatar/prototype/Mage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
publicabstractclassMageextendsPrototype {
3131

3232
@Override
33-
publicabstractMageclone()throwsCloneNotSupportedException;
33+
publicabstractMagecopy()throwsCloneNotSupportedException;
3434

3535
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,26 @@
2828
*
2929
*/
3030
publicclassOrcBeastextendsBeast {
31+
32+
privateStringweapon;
3133

32-
publicOrcBeast() {}
34+
publicOrcBeast(Stringweapon) {
35+
this.weapon =weapon;
36+
}
37+
38+
publicOrcBeast(OrcBeastorcBeast) {
39+
this.weapon =orcBeast.weapon;
40+
}
3341

3442
@Override
35-
publicBeastclone()throwsCloneNotSupportedException {
36-
returnnewOrcBeast();
43+
publicBeastcopy()throwsCloneNotSupportedException {
44+
returnnewOrcBeast(this);
3745
}
3846

3947
@Override
4048
publicStringtoString() {
41-
return"Orcish wolf";
49+
return"Orcish wolf attacks with " +weapon;
4250
}
51+
4352

4453
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcMage.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
publicclassOrcMageextendsMage {
3131

32-
publicOrcMage() {}
32+
privateStringweapon;
33+
34+
publicOrcMage(Stringweapon) {
35+
this.weapon =weapon;
36+
}
37+
38+
publicOrcMage(OrcMageorcMage) {
39+
this.weapon =orcMage.weapon;
40+
}
3341

3442
@Override
35-
publicMageclone()throwsCloneNotSupportedException {
36-
returnnewOrcMage();
43+
publicOrcMagecopy()throwsCloneNotSupportedException {
44+
returnnewOrcMage(this);
3745
}
3846

3947
@Override
4048
publicStringtoString() {
41-
return"Orcish mage";
49+
return"Orcish mage attacks with " +weapon;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
publicclassOrcWarlordextendsWarlord {
3131

32-
publicOrcWarlord() {}
32+
privateStringweapon;
33+
34+
publicOrcWarlord(Stringweapon) {
35+
this.weapon =weapon;
36+
}
37+
38+
publicOrcWarlord(OrcWarlordorcWarlord) {
39+
this.weapon =orcWarlord.weapon;
40+
}
3341

3442
@Override
35-
publicWarlordclone()throwsCloneNotSupportedException {
36-
returnnewOrcWarlord();
43+
publicOrcWarlordcopy()throwsCloneNotSupportedException {
44+
returnnewOrcWarlord(this);
3745
}
3846

3947
@Override
4048
publicStringtoString() {
41-
return"Orcish warlord";
49+
return"Orcish warlord attacks with " +weapon;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/Prototype.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
publicabstractclassPrototypeimplementsCloneable {
3131

32-
@Override
33-
publicabstractObjectclone()throwsCloneNotSupportedException;
32+
publicabstractObjectcopy()throwsCloneNotSupportedException;
3433

3534
}

‎prototype/src/main/java/com/iluwatar/prototype/Warlord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
publicabstractclassWarlordextendsPrototype {
3131

3232
@Override
33-
publicabstractWarlordclone()throwsCloneNotSupportedException;
33+
publicabstractWarlordcopy()throwsCloneNotSupportedException;
3434

3535
}

‎prototype/src/test/java/com/iluwatar/prototype/HeroFactoryImplTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ public void testFactory() throws Exception {
4343
finalWarlordwarlord =mock(Warlord.class);
4444
finalBeastbeast =mock(Beast.class);
4545

46-
when(mage.clone()).thenThrow(CloneNotSupportedException.class);
47-
when(warlord.clone()).thenThrow(CloneNotSupportedException.class);
48-
when(beast.clone()).thenThrow(CloneNotSupportedException.class);
46+
when(mage.copy()).thenThrow(CloneNotSupportedException.class);
47+
when(warlord.copy()).thenThrow(CloneNotSupportedException.class);
48+
when(beast.copy()).thenThrow(CloneNotSupportedException.class);
4949

5050
finalHeroFactoryImplfactory =newHeroFactoryImpl(mage,warlord,beast);
5151
assertNull(factory.createMage());
5252
assertNull(factory.createWarlord());
5353
assertNull(factory.createBeast());
5454

55-
verify(mage).clone();
56-
verify(warlord).clone();
57-
verify(beast).clone();
55+
verify(mage).copy();
56+
verify(warlord).copy();
57+
verify(beast).copy();
5858
verifyNoMoreInteractions(mage,warlord,beast);
5959
}
6060

‎prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
publicclassPrototypeTest<PextendsPrototype> {
4242
staticCollection<Object[]>dataProvider() {
4343
returnArrays.asList(
44-
newObject[]{newOrcBeast(),"Orcish wolf"},
45-
newObject[]{newOrcMage(),"Orcish mage"},
46-
newObject[]{newOrcWarlord(),"Orcish warlord"},
47-
newObject[]{newElfBeast(),"Elven eagle"},
48-
newObject[]{newElfMage(),"Elven mage"},
49-
newObject[]{newElfWarlord(),"Elven warlord"}
44+
newObject[]{newOrcBeast("axe"),"Orcish wolf attacks with axe"},
45+
newObject[]{newOrcMage("sword"),"Orcish mage attacks with sword"},
46+
newObject[]{newOrcWarlord("laser"),"Orcish warlord attacks with laser"},
47+
newObject[]{newElfBeast("cooking"),"Elven eagle helps in cooking"},
48+
newObject[]{newElfMage("cleaning"),"Elven mage helps in cleaning"},
49+
newObject[]{newElfWarlord("protecting"),"Elven warlord helps in protecting"}
5050
);
5151
}
5252

@@ -55,7 +55,7 @@ static Collection<Object[]> dataProvider() {
5555
publicvoidtestPrototype(PtestedPrototype,StringexpectedToString)throwsException {
5656
assertEquals(expectedToString,testedPrototype.toString());
5757

58-
finalObjectclone =testedPrototype.clone();
58+
finalObjectclone =testedPrototype.copy();
5959
assertNotNull(clone);
6060
assertNotSame(clone,testedPrototype);
6161
assertSame(testedPrototype.getClass(),clone.getClass());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp