Merge pull request from ruslanpa/master

Makes a few improvements, basically unnecessary declarations, formatting
This commit is contained in:
Ilkka Seppälä 2015-02-10 19:09:47 +02:00
commit e01c852ee0
21 changed files with 297 additions and 272 deletions
builder/src/main/java/com/iluwatar
command/src/main/java/com/iluwatar
facade/src/main/java/com/iluwatar
mediator/src/main/java/com/iluwatar
observer/src/main/java/com/iluwatar/generic
servant/src/main/java/com/iluwatar
service-locator/src/main/java/com/iluwatar

@ -2,11 +2,16 @@ package com.iluwatar;
public enum HairType {
BALD, SHORT, CURLY, LONG_STRAIGHT, LONG_CURLY;
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
private String title;
HairType(String title) {
this.title = title;
}
@Override
public String toString() {
return name().toLowerCase().replaceAll("_", " ");
return title;
}
}

@ -3,8 +3,8 @@ package com.iluwatar;
public class Goblin extends Target {
public Goblin() {
this.setSize(Size.NORMAL);
this.setVisibility(Visibility.VISIBLE);
setSize(Size.NORMAL);
setVisibility(Visibility.VISIBLE);
}
@Override

@ -4,10 +4,6 @@ public class InvisibilitySpell extends Command {
private Target target;
public InvisibilitySpell() {
target = null;
}
@Override
public void execute(Target target) {
target.setVisibility(Visibility.INVISIBLE);

@ -3,14 +3,8 @@ package com.iluwatar;
public class ShrinkSpell extends Command {
private Size oldSize;
private Target target;
public ShrinkSpell() {
oldSize = null;
target = null;
}
@Override
public void execute(Target target) {
oldSize = target.getSize();

@ -5,9 +5,8 @@ public class Wizard extends Target {
private Command previousSpell;
public Wizard() {
this.setSize(Size.NORMAL);
this.setVisibility(Visibility.VISIBLE);
previousSpell = null;
setSize(Size.NORMAL);
setVisibility(Visibility.VISIBLE);
}
public void castSpell(Command command, Target target) {

@ -1,6 +1,7 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@ -15,7 +16,7 @@ import java.util.List;
*/
public class DwarvenGoldmineFacade {
List<DwarvenMineWorker> workers;
private final List<DwarvenMineWorker> workers;
public DwarvenGoldmineFacade() {
workers = new ArrayList<>();
@ -25,23 +26,20 @@ public class DwarvenGoldmineFacade {
}
public void startNewDay() {
for (DwarvenMineWorker worker : workers) {
worker.wakeUp();
worker.goToMine();
}
makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
}
public void digOutGold() {
for (DwarvenMineWorker worker : workers) {
worker.work();
}
makeActions(workers, DwarvenMineWorker.Action.WORK);
}
public void endDay() {
for (DwarvenMineWorker worker : workers) {
worker.goHome();
worker.goToSleep();
}
makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
}
private void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
for (DwarvenMineWorker worker : workers) {
worker.action(actions);
}
}
}

@ -23,8 +23,40 @@ public abstract class DwarvenMineWorker {
System.out.println(name() + " goes to the mine.");
}
private void action(Action action) {
switch (action) {
case GO_TO_SLEEP:
goToSleep();
break;
case WAKE_UP:
wakeUp();
break;
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
System.out.println("Undefined action");
break;
}
}
public void action(Action... actions) {
for (Action action : actions) {
action(action);
}
}
public abstract void work();
public abstract String name();
static enum Action {
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
}
}

@ -7,12 +7,22 @@ package com.iluwatar;
*/
public enum Action {
HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE("");
HUNT("hunted a rabbit", "arrives for dinner"),
TALE("tells a tale", "comes to listen"),
GOLD("found gold", "takes his share of the gold"),
ENEMY("spotted enemies", "runs for cover"),
NONE("", "");
private String title;
private String description;
Action(String title) {
Action(String title, String description) {
this.title = title;
this.description = description;
}
public String getDescription() {
return description;
}
public String toString() {

@ -10,7 +10,7 @@ import java.util.List;
*/
public class PartyImpl implements Party {
private List<PartyMember> members;
private final List<PartyMember> members;
public PartyImpl() {
members = new ArrayList<>();

@ -17,24 +17,7 @@ public abstract class PartyMemberBase implements PartyMember {
@Override
public void partyAction(Action action) {
String s = this + " ";
switch (action) {
case ENEMY:
s = s + "runs for cover";
break;
case GOLD:
s = s + "takes his share of the gold";
break;
case HUNT:
s = s + "arrives for dinner";
break;
case TALE:
s = s + "comes to listen";
break;
default:
break;
}
System.out.println(s);
System.out.println(this + " " + action.getDescription());
}
@Override

@ -1,5 +1,6 @@
package com.iluwatar.generic;
public interface Observer<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
public void update(S subject, A argument);
void update(S subject, A argument);
}

@ -48,6 +48,4 @@ public class App {
else
System.out.println("Poor " + servant.name + ". His days are numbered");
}
}

@ -1,8 +1,10 @@
package com.iluwatar;
public class King implements Royalty {
private boolean isDrunk = false, isHungry = true, isHappy = false;
private boolean complimentReceived = false;
private boolean isDrunk;
private boolean isHungry = true;
private boolean isHappy;
private boolean complimentReceived;
@Override
public void getFed() {

@ -1,8 +1,11 @@
package com.iluwatar;
public class Queen implements Royalty {
private boolean isDrunk = true, isHungry = false, isHappy = false;
private boolean isFlirty = true, complimentReceived = false;
private boolean isDrunk = true;
private boolean isHungry;
private boolean isHappy;
private boolean isFlirty = true;
private boolean complimentReceived;
@Override
public void getFed() {

@ -1,9 +1,14 @@
package com.iluwatar;
interface Royalty {
public void getFed();
public void getDrink();
public void changeMood();
public void receiveCompliments();
public boolean getMood();
void getFed();
void getDrink();
void changeMood();
void receiveCompliments();
boolean getMood();
}

@ -1,9 +1,10 @@
package com.iluwatar;
/**
* Service locator pattern, used to lookup jndi services
* and cache them for subsequent requests.
* @author saifasif
*
* @author saifasif
*/
public class App {
public static void main(String[] args) {
@ -16,5 +17,4 @@ public class App {
service = ServiceLocator.getService("jndi/serviceA");
service.execute();
}
}

@ -3,16 +3,17 @@ package com.iluwatar;
/**
* For JNDI lookup of services from the web.xml. Will match name of the service name that
* is being requested and return a newly created service object with the name
* @author saifasif
*
* @author saifasif
*/
public class InitContext {
/**
* Perform the lookup based on the service name. The returned object will need to be
* casted into a {@link Service}
* @param serviceName
* @return
*
* @param serviceName a string
* @return an {@link Object}
*/
public Object lookup(String serviceName) {
if (serviceName.equals("jndi/serviceA")) {
@ -25,5 +26,4 @@ public class InitContext {
return null;
}
}
}

@ -14,16 +14,15 @@ public interface Service {
/*
* The human readable name of the service
*/
public String getName();
String getName();
/*
* Unique ID of the particular service
*/
public int getId();
int getId();
/*
* The workflow method that defines what this service does
*/
public void execute();
void execute();
}

@ -8,21 +8,21 @@ import java.util.Map;
* On first hit, the cache will be empty and thus any service that is being requested, will be
* created fresh and then placed into the cache map. On next hit, if same service name will
* be requested, it will be returned from the cache
* @author saifasif
*
* @author saifasif
*/
public class ServiceCache {
private Map<String, Service> serviceCache;
private final Map<String, Service> serviceCache;
public ServiceCache() {
serviceCache = new HashMap<String, Service>();
}
/**
* Get the service from the cache. null if no service is found matching the
* name
* @param serviceName
* Get the service from the cache. null if no service is found matching the name
*
* @param serviceName a string
* @return {@link Service}
*/
public Service getService(String serviceName) {
@ -38,7 +38,8 @@ public class ServiceCache {
/**
* Adds the service into the cache map
* @param newService
*
* @param newService a {@link Service}
*/
public void addService(Service newService) {
serviceCache.put(newService.getName(), newService);

@ -4,13 +4,13 @@ package com.iluwatar;
* This is a single service implementation of a sample service. This is the actual
* service that will process the request. The reference for this service is to
* be looked upon in the JNDI server that can be set in the web.xml deployment descriptor
* @author saifasif
*
* @author saifasif
*/
public class ServiceImpl implements Service {
private String serviceName;
private int id;
private final String serviceName;
private final int id;
public ServiceImpl(String serviceName) {
// set the service name

@ -5,7 +5,6 @@ package com.iluwatar;
* Will fetch service from cache, otherwise creates a fresh service and update cache
*
* @author saifasif
*
*/
public class ServiceLocator {
@ -15,7 +14,8 @@ public class ServiceLocator {
* Fetch the service with the name param from the cache first,
* if no service is found, lookup the service from the {@link InitContext} and
* then add the newly created service into the cache map for future requests.
* @param serviceJndiName
*
* @param serviceJndiName a string
* @return {@link Service}
*/
public static Service getService(String serviceJndiName) {
@ -33,5 +33,4 @@ public class ServiceLocator {
return serviceObj;
}
}
}