Skip to content

Commit e01c852

Browse files
committed
Merge pull request iluwatar#30 from ruslanpa/master
Makes a few improvements, basically unnecessary declarations, formatting
2 parents c7a4a85 + 33cd28f commit e01c852

File tree

21 files changed

+300
-275
lines changed

21 files changed

+300
-275
lines changed

builder/src/main/java/com/iluwatar/HairType.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public enum HairType {
44

5-
BALD, SHORT, CURLY, LONG_STRAIGHT, LONG_CURLY;
5+
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
66

7-
@Override
7+
private String title;
8+
9+
HairType(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
814
public String toString() {
9-
return name().toLowerCase().replaceAll("_", " ");
15+
return title;
1016
}
11-
1217
}

command/src/main/java/com/iluwatar/Goblin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public class Goblin extends Target {
44

55
public Goblin() {
6-
this.setSize(Size.NORMAL);
7-
this.setVisibility(Visibility.VISIBLE);
6+
setSize(Size.NORMAL);
7+
setVisibility(Visibility.VISIBLE);
88
}
99

1010
@Override

command/src/main/java/com/iluwatar/InvisibilitySpell.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ public class InvisibilitySpell extends Command {
44

55
private Target target;
66

7-
public InvisibilitySpell() {
8-
target = null;
9-
}
10-
117
@Override
128
public void execute(Target target) {
139
target.setVisibility(Visibility.INVISIBLE);

command/src/main/java/com/iluwatar/ShrinkSpell.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
public class ShrinkSpell extends Command {
44

55
private Size oldSize;
6-
76
private Target target;
87

9-
public ShrinkSpell() {
10-
oldSize = null;
11-
target = null;
12-
}
13-
148
@Override
159
public void execute(Target target) {
1610
oldSize = target.getSize();

command/src/main/java/com/iluwatar/Wizard.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ public class Wizard extends Target {
55
private Command previousSpell;
66

77
public Wizard() {
8-
this.setSize(Size.NORMAL);
9-
this.setVisibility(Visibility.VISIBLE);
10-
previousSpell = null;
8+
setSize(Size.NORMAL);
9+
setVisibility(Visibility.VISIBLE);
1110
}
1211

1312
public void castSpell(Command command, Target target) {
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.iluwatar;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -15,7 +16,7 @@
1516
*/
1617
public class DwarvenGoldmineFacade {
1718

18-
List<DwarvenMineWorker> workers;
19+
private final List<DwarvenMineWorker> workers;
1920

2021
public DwarvenGoldmineFacade() {
2122
workers = new ArrayList<>();
@@ -25,23 +26,20 @@ public DwarvenGoldmineFacade() {
2526
}
2627

2728
public void startNewDay() {
28-
for (DwarvenMineWorker worker : workers) {
29-
worker.wakeUp();
30-
worker.goToMine();
31-
}
29+
makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
3230
}
3331

3432
public void digOutGold() {
35-
for (DwarvenMineWorker worker : workers) {
36-
worker.work();
37-
}
33+
makeActions(workers, DwarvenMineWorker.Action.WORK);
3834
}
3935

4036
public void endDay() {
41-
for (DwarvenMineWorker worker : workers) {
42-
worker.goHome();
43-
worker.goToSleep();
44-
}
37+
makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
4538
}
4639

40+
private void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
41+
for (DwarvenMineWorker worker : workers) {
42+
worker.action(actions);
43+
}
44+
}
4745
}

facade/src/main/java/com/iluwatar/DwarvenMineWorker.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,40 @@ public void goToMine() {
2323
System.out.println(name() + " goes to the mine.");
2424
}
2525

26+
private void action(Action action) {
27+
switch (action) {
28+
case GO_TO_SLEEP:
29+
goToSleep();
30+
break;
31+
case WAKE_UP:
32+
wakeUp();
33+
break;
34+
case GO_HOME:
35+
goHome();
36+
break;
37+
case GO_TO_MINE:
38+
goToMine();
39+
break;
40+
case WORK:
41+
work();
42+
break;
43+
default:
44+
System.out.println("Undefined action");
45+
break;
46+
}
47+
}
48+
49+
public void action(Action... actions) {
50+
for (Action action : actions) {
51+
action(action);
52+
}
53+
}
54+
2655
public abstract void work();
2756

2857
public abstract String name();
2958

59+
static enum Action {
60+
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
61+
}
3062
}

mediator/src/main/java/com/iluwatar/Action.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77
*/
88
public enum Action {
99

10-
HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE("");
10+
HUNT("hunted a rabbit", "arrives for dinner"),
11+
TALE("tells a tale", "comes to listen"),
12+
GOLD("found gold", "takes his share of the gold"),
13+
ENEMY("spotted enemies", "runs for cover"),
14+
NONE("", "");
1115

1216
private String title;
17+
private String description;
1318

14-
Action(String title) {
19+
Action(String title, String description) {
1520
this.title = title;
21+
this.description = description;
22+
}
23+
24+
public String getDescription() {
25+
return description;
1626
}
1727

1828
public String toString() {

mediator/src/main/java/com/iluwatar/PartyImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
public class PartyImpl implements Party {
1212

13-
private List<PartyMember> members;
13+
private final List<PartyMember> members;
1414

1515
public PartyImpl() {
1616
members = new ArrayList<>();

mediator/src/main/java/com/iluwatar/PartyMemberBase.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,7 @@ public void joinedParty(Party party) {
1717

1818
@Override
1919
public void partyAction(Action action) {
20-
String s = this + " ";
21-
switch (action) {
22-
case ENEMY:
23-
s = s + "runs for cover";
24-
break;
25-
case GOLD:
26-
s = s + "takes his share of the gold";
27-
break;
28-
case HUNT:
29-
s = s + "arrives for dinner";
30-
break;
31-
case TALE:
32-
s = s + "comes to listen";
33-
break;
34-
default:
35-
break;
36-
}
37-
System.out.println(s);
20+
System.out.println(this + " " + action.getDescription());
3821
}
3922

4023
@Override
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.iluwatar.generic;
22

33
public interface Observer<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
4-
public void update(S subject, A argument);
4+
5+
void update(S subject, A argument);
56
}

servant/src/main/java/com/iluwatar/App.java

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,48 @@
44

55

66
/**
7-
* Servant offers some functionality to a group of classes without defining that functionality in each of them.
7+
* Servant offers some functionality to a group of classes without defining that functionality in each of them.
88
* A Servant is a class whose instance provides methods that take care of a desired service,
99
* while objects for which the servant does something, are taken as parameters.
1010
*
1111
*/
1212
public class App {
13-
static Servant jenkins = new Servant("Jenkins");
14-
static Servant travis = new Servant("Travis");
15-
16-
public static void main( String[] args ){
17-
scenario(jenkins, 1);
18-
scenario(travis, 0);
19-
}
20-
21-
/*
22-
* Can add a List with enum Actions for variable scenarios
23-
* */
24-
public static void scenario(Servant servant, int compliment){
25-
King k = new King();
26-
Queen q = new Queen();
27-
28-
ArrayList<Royalty> guests = new ArrayList<>();
29-
guests.add(k);
30-
guests.add(q);
31-
32-
//feed
33-
servant.feed(k);
34-
servant.feed(q);
35-
//serve drinks
36-
servant.giveWine(k);
37-
servant.giveWine(q);
38-
//compliment
39-
servant.GiveCompliments( guests.get(compliment) );
40-
41-
//outcome of the night
42-
for(Royalty r : guests)
43-
r.changeMood();
44-
45-
//check your luck
46-
if( servant.checkIfYouWillBeHanged(guests) )
47-
System.out.println(servant.name + " will live another day");
48-
else
49-
System.out.println("Poor " + servant.name + ". His days are numbered");
50-
}
51-
52-
13+
static Servant jenkins = new Servant("Jenkins");
14+
static Servant travis = new Servant("Travis");
15+
16+
public static void main(String[] args) {
17+
scenario(jenkins, 1);
18+
scenario(travis, 0);
19+
}
20+
21+
/*
22+
* Can add a List with enum Actions for variable scenarios
23+
* */
24+
public static void scenario(Servant servant, int compliment) {
25+
King k = new King();
26+
Queen q = new Queen();
27+
28+
ArrayList<Royalty> guests = new ArrayList<>();
29+
guests.add(k);
30+
guests.add(q);
31+
32+
//feed
33+
servant.feed(k);
34+
servant.feed(q);
35+
//serve drinks
36+
servant.giveWine(k);
37+
servant.giveWine(q);
38+
//compliment
39+
servant.GiveCompliments(guests.get(compliment));
40+
41+
//outcome of the night
42+
for (Royalty r : guests)
43+
r.changeMood();
44+
45+
//check your luck
46+
if (servant.checkIfYouWillBeHanged(guests))
47+
System.out.println(servant.name + " will live another day");
48+
else
49+
System.out.println("Poor " + servant.name + ". His days are numbered");
50+
}
5351
}
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package com.iluwatar;
22

3-
public class King implements Royalty{
4-
private boolean isDrunk = false, isHungry = true, isHappy = false;
5-
private boolean complimentReceived = false;
6-
7-
@Override
8-
public void getFed() {
9-
isHungry = false;
10-
}
3+
public class King implements Royalty {
4+
private boolean isDrunk;
5+
private boolean isHungry = true;
6+
private boolean isHappy;
7+
private boolean complimentReceived;
118

12-
@Override
13-
public void getDrink() {
14-
isDrunk = true;
15-
}
16-
17-
public void receiveCompliments(){
18-
complimentReceived = true;
19-
}
9+
@Override
10+
public void getFed() {
11+
isHungry = false;
12+
}
2013

21-
@Override
22-
public void changeMood() {
23-
if(!isHungry && isDrunk) isHappy = true;
24-
if( complimentReceived ) isHappy = false;
25-
}
14+
@Override
15+
public void getDrink() {
16+
isDrunk = true;
17+
}
2618

27-
@Override
28-
public boolean getMood() {
29-
return isHappy;
30-
}
19+
public void receiveCompliments() {
20+
complimentReceived = true;
21+
}
22+
23+
@Override
24+
public void changeMood() {
25+
if (!isHungry && isDrunk) isHappy = true;
26+
if (complimentReceived) isHappy = false;
27+
}
28+
29+
@Override
30+
public boolean getMood() {
31+
return isHappy;
32+
}
3133
}

0 commit comments

Comments
 (0)