From 0fbb4f400309b97faeb8e864e5923afe89bf7b16 Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 09:23:59 +0200 Subject: [PATCH 1/7] [refactor] Remove unnecessary declarations in command pattern. --- command/src/main/java/com/iluwatar/Goblin.java | 4 ++-- command/src/main/java/com/iluwatar/InvisibilitySpell.java | 4 ---- command/src/main/java/com/iluwatar/ShrinkSpell.java | 6 ------ command/src/main/java/com/iluwatar/Wizard.java | 5 ++--- 4 files changed, 4 insertions(+), 15 deletions(-) diff --git a/command/src/main/java/com/iluwatar/Goblin.java b/command/src/main/java/com/iluwatar/Goblin.java index 4e50915f9..6bb614554 100644 --- a/command/src/main/java/com/iluwatar/Goblin.java +++ b/command/src/main/java/com/iluwatar/Goblin.java @@ -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 diff --git a/command/src/main/java/com/iluwatar/InvisibilitySpell.java b/command/src/main/java/com/iluwatar/InvisibilitySpell.java index b95a24f7e..9a35a9df3 100644 --- a/command/src/main/java/com/iluwatar/InvisibilitySpell.java +++ b/command/src/main/java/com/iluwatar/InvisibilitySpell.java @@ -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); diff --git a/command/src/main/java/com/iluwatar/ShrinkSpell.java b/command/src/main/java/com/iluwatar/ShrinkSpell.java index fe444a972..44a5b7144 100644 --- a/command/src/main/java/com/iluwatar/ShrinkSpell.java +++ b/command/src/main/java/com/iluwatar/ShrinkSpell.java @@ -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(); diff --git a/command/src/main/java/com/iluwatar/Wizard.java b/command/src/main/java/com/iluwatar/Wizard.java index 63aa560c7..e3d19be1f 100644 --- a/command/src/main/java/com/iluwatar/Wizard.java +++ b/command/src/main/java/com/iluwatar/Wizard.java @@ -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) { From 04992483a135effa12de50ce2ece5bea75c12468 Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 09:33:03 +0200 Subject: [PATCH 2/7] [refactor] Separate out one method to call actions for workers. --- .../com/iluwatar/DwarvenGoldmineFacade.java | 22 ++++++------- .../java/com/iluwatar/DwarvenMineWorker.java | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/facade/src/main/java/com/iluwatar/DwarvenGoldmineFacade.java b/facade/src/main/java/com/iluwatar/DwarvenGoldmineFacade.java index fc5af7338..b3a01a3d9 100644 --- a/facade/src/main/java/com/iluwatar/DwarvenGoldmineFacade.java +++ b/facade/src/main/java/com/iluwatar/DwarvenGoldmineFacade.java @@ -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 workers; + private final List 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 workers, DwarvenMineWorker.Action... actions) { + for (DwarvenMineWorker worker : workers) { + worker.action(actions); + } + } } diff --git a/facade/src/main/java/com/iluwatar/DwarvenMineWorker.java b/facade/src/main/java/com/iluwatar/DwarvenMineWorker.java index 27ec4e87b..74ff80984 100644 --- a/facade/src/main/java/com/iluwatar/DwarvenMineWorker.java +++ b/facade/src/main/java/com/iluwatar/DwarvenMineWorker.java @@ -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 + } } From 8980b3958c93f2daf38c858a60668f837dcdf21e Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 09:48:22 +0200 Subject: [PATCH 3/7] [refactor] Extends Action enum in mediator pattern. --- .../src/main/java/com/iluwatar/Action.java | 14 ++++++++++++-- .../src/main/java/com/iluwatar/PartyImpl.java | 2 +- .../java/com/iluwatar/PartyMemberBase.java | 19 +------------------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/mediator/src/main/java/com/iluwatar/Action.java b/mediator/src/main/java/com/iluwatar/Action.java index 7e6fc30f7..eb237269f 100644 --- a/mediator/src/main/java/com/iluwatar/Action.java +++ b/mediator/src/main/java/com/iluwatar/Action.java @@ -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() { diff --git a/mediator/src/main/java/com/iluwatar/PartyImpl.java b/mediator/src/main/java/com/iluwatar/PartyImpl.java index 70fe24d71..634c3444b 100644 --- a/mediator/src/main/java/com/iluwatar/PartyImpl.java +++ b/mediator/src/main/java/com/iluwatar/PartyImpl.java @@ -10,7 +10,7 @@ import java.util.List; */ public class PartyImpl implements Party { - private List members; + private final List members; public PartyImpl() { members = new ArrayList<>(); diff --git a/mediator/src/main/java/com/iluwatar/PartyMemberBase.java b/mediator/src/main/java/com/iluwatar/PartyMemberBase.java index 133792db6..d352b7b33 100644 --- a/mediator/src/main/java/com/iluwatar/PartyMemberBase.java +++ b/mediator/src/main/java/com/iluwatar/PartyMemberBase.java @@ -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 From 300c8dfad4c8dc982f663c8ea8b9d13a2b5746cf Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 09:49:33 +0200 Subject: [PATCH 4/7] [refactor] Update HairType enum in builder pattern. --- builder/src/main/java/com/iluwatar/HairType.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/builder/src/main/java/com/iluwatar/HairType.java b/builder/src/main/java/com/iluwatar/HairType.java index 3348b86d1..ec5508712 100644 --- a/builder/src/main/java/com/iluwatar/HairType.java +++ b/builder/src/main/java/com/iluwatar/HairType.java @@ -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"); - @Override + private String title; + + HairType(String title) { + this.title = title; + } + + @Override public String toString() { - return name().toLowerCase().replaceAll("_", " "); + return title; } - } From 4b432a79d95c0c6b8f279cb4657f95e8227c877b Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 09:57:22 +0200 Subject: [PATCH 5/7] [refactor] Remove unnecessary declarations in servant pattern. --- servant/src/main/java/com/iluwatar/App.java | 80 +++++++++---------- servant/src/main/java/com/iluwatar/King.java | 52 ++++++------ servant/src/main/java/com/iluwatar/Queen.java | 59 +++++++------- .../src/main/java/com/iluwatar/Royalty.java | 15 ++-- 4 files changed, 107 insertions(+), 99 deletions(-) diff --git a/servant/src/main/java/com/iluwatar/App.java b/servant/src/main/java/com/iluwatar/App.java index 4571e2974..34688ea1a 100644 --- a/servant/src/main/java/com/iluwatar/App.java +++ b/servant/src/main/java/com/iluwatar/App.java @@ -4,50 +4,48 @@ import java.util.ArrayList; /** - * Servant offers some functionality to a group of classes without defining that functionality in each of them. + * Servant offers some functionality to a group of classes without defining that functionality in each of them. * A Servant is a class whose instance provides methods that take care of a desired service, * while objects for which the servant does something, are taken as parameters. * */ public class App { - static Servant jenkins = new Servant("Jenkins"); - static Servant travis = new Servant("Travis"); - - public static void main( String[] args ){ - scenario(jenkins, 1); - scenario(travis, 0); - } - - /* - * Can add a List with enum Actions for variable scenarios - * */ - public static void scenario(Servant servant, int compliment){ - King k = new King(); - Queen q = new Queen(); - - ArrayList guests = new ArrayList<>(); - guests.add(k); - guests.add(q); - - //feed - servant.feed(k); - servant.feed(q); - //serve drinks - servant.giveWine(k); - servant.giveWine(q); - //compliment - servant.GiveCompliments( guests.get(compliment) ); - - //outcome of the night - for(Royalty r : guests) - r.changeMood(); - - //check your luck - if( servant.checkIfYouWillBeHanged(guests) ) - System.out.println(servant.name + " will live another day"); - else - System.out.println("Poor " + servant.name + ". His days are numbered"); - } - - + static Servant jenkins = new Servant("Jenkins"); + static Servant travis = new Servant("Travis"); + + public static void main(String[] args) { + scenario(jenkins, 1); + scenario(travis, 0); + } + + /* + * Can add a List with enum Actions for variable scenarios + * */ + public static void scenario(Servant servant, int compliment) { + King k = new King(); + Queen q = new Queen(); + + ArrayList guests = new ArrayList<>(); + guests.add(k); + guests.add(q); + + //feed + servant.feed(k); + servant.feed(q); + //serve drinks + servant.giveWine(k); + servant.giveWine(q); + //compliment + servant.GiveCompliments(guests.get(compliment)); + + //outcome of the night + for (Royalty r : guests) + r.changeMood(); + + //check your luck + if (servant.checkIfYouWillBeHanged(guests)) + System.out.println(servant.name + " will live another day"); + else + System.out.println("Poor " + servant.name + ". His days are numbered"); + } } diff --git a/servant/src/main/java/com/iluwatar/King.java b/servant/src/main/java/com/iluwatar/King.java index 7b38fd77f..61ccc27cc 100644 --- a/servant/src/main/java/com/iluwatar/King.java +++ b/servant/src/main/java/com/iluwatar/King.java @@ -1,31 +1,33 @@ package com.iluwatar; -public class King implements Royalty{ - private boolean isDrunk = false, isHungry = true, isHappy = false; - private boolean complimentReceived = false; - - @Override - public void getFed() { - isHungry = false; - } +public class King implements Royalty { + private boolean isDrunk; + private boolean isHungry = true; + private boolean isHappy; + private boolean complimentReceived; - @Override - public void getDrink() { - isDrunk = true; - } - - public void receiveCompliments(){ - complimentReceived = true; - } + @Override + public void getFed() { + isHungry = false; + } - @Override - public void changeMood() { - if(!isHungry && isDrunk) isHappy = true; - if( complimentReceived ) isHappy = false; - } + @Override + public void getDrink() { + isDrunk = true; + } - @Override - public boolean getMood() { - return isHappy; - } + public void receiveCompliments() { + complimentReceived = true; + } + + @Override + public void changeMood() { + if (!isHungry && isDrunk) isHappy = true; + if (complimentReceived) isHappy = false; + } + + @Override + public boolean getMood() { + return isHappy; + } } diff --git a/servant/src/main/java/com/iluwatar/Queen.java b/servant/src/main/java/com/iluwatar/Queen.java index baf6fd239..71b3472cd 100644 --- a/servant/src/main/java/com/iluwatar/Queen.java +++ b/servant/src/main/java/com/iluwatar/Queen.java @@ -1,35 +1,38 @@ package com.iluwatar; -public class Queen implements Royalty{ - private boolean isDrunk = true, isHungry = false, isHappy = false; - private boolean isFlirty = true, complimentReceived = false; - - @Override - public void getFed() { - isHungry = false; - } +public class Queen implements Royalty { + private boolean isDrunk = true; + private boolean isHungry; + private boolean isHappy; + private boolean isFlirty = true; + private boolean complimentReceived; - @Override - public void getDrink() { - isDrunk = true; - } - - public void receiveCompliments(){ - complimentReceived = true; - } + @Override + public void getFed() { + isHungry = false; + } - @Override - public void changeMood() { - if( complimentReceived && isFlirty && isDrunk ) isHappy = true; - } + @Override + public void getDrink() { + isDrunk = true; + } - @Override - public boolean getMood() { - return isHappy; - } - - public void setFlirtiness(boolean f){ - this.isFlirty = f; - } + public void receiveCompliments() { + complimentReceived = true; + } + + @Override + public void changeMood() { + if (complimentReceived && isFlirty && isDrunk) isHappy = true; + } + + @Override + public boolean getMood() { + return isHappy; + } + + public void setFlirtiness(boolean f) { + this.isFlirty = f; + } } diff --git a/servant/src/main/java/com/iluwatar/Royalty.java b/servant/src/main/java/com/iluwatar/Royalty.java index 2c8bef32d..bcfc8b748 100644 --- a/servant/src/main/java/com/iluwatar/Royalty.java +++ b/servant/src/main/java/com/iluwatar/Royalty.java @@ -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(); } From 14058082bbc4639a0a91c7f1f19bb4370d0635d3 Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 10:02:44 +0200 Subject: [PATCH 6/7] [refactor] Remove unnecessary declarations in service-locator pattern. --- .../src/main/java/com/iluwatar/App.java | 28 ++++----- .../main/java/com/iluwatar/InitContext.java | 38 +++++------ .../src/main/java/com/iluwatar/Service.java | 7 +-- .../main/java/com/iluwatar/ServiceCache.java | 63 ++++++++++--------- .../main/java/com/iluwatar/ServiceImpl.java | 52 +++++++-------- .../java/com/iluwatar/ServiceLocator.java | 51 ++++++++------- 6 files changed, 119 insertions(+), 120 deletions(-) diff --git a/service-locator/src/main/java/com/iluwatar/App.java b/service-locator/src/main/java/com/iluwatar/App.java index 54635e907..25ac36d6b 100644 --- a/service-locator/src/main/java/com/iluwatar/App.java +++ b/service-locator/src/main/java/com/iluwatar/App.java @@ -1,20 +1,20 @@ package com.iluwatar; + /** - * Service locator pattern, used to lookup jndi services - * and cache them for subsequent requests. - * @author saifasif + * Service locator pattern, used to lookup jndi services + * and cache them for subsequent requests. * + * @author saifasif */ public class App { - public static void main(String[] args) { - Service service = ServiceLocator.getService("jndi/serviceA"); - service.execute(); - service = ServiceLocator.getService("jndi/serviceB"); - service.execute(); - service = ServiceLocator.getService("jndi/serviceA"); - service.execute(); - service = ServiceLocator.getService("jndi/serviceA"); - service.execute(); - } - + public static void main(String[] args) { + Service service = ServiceLocator.getService("jndi/serviceA"); + service.execute(); + service = ServiceLocator.getService("jndi/serviceB"); + service.execute(); + service = ServiceLocator.getService("jndi/serviceA"); + service.execute(); + service = ServiceLocator.getService("jndi/serviceA"); + service.execute(); + } } diff --git a/service-locator/src/main/java/com/iluwatar/InitContext.java b/service-locator/src/main/java/com/iluwatar/InitContext.java index 8b217bc28..3c03b717a 100644 --- a/service-locator/src/main/java/com/iluwatar/InitContext.java +++ b/service-locator/src/main/java/com/iluwatar/InitContext.java @@ -3,27 +3,27 @@ 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 - */ - public Object lookup(String serviceName){ - if( serviceName.equals("jndi/serviceA") ){ - System.out.println("Looking up service A and creating new service for A"); - return new ServiceImpl("jndi/serviceA"); - } else if( serviceName.equals("jndi/serviceB") ){ - System.out.println("Looking up service B and creating new service for B"); - return new ServiceImpl("jndi/serviceB"); - } else { - return null; - } - } - + /** + * Perform the lookup based on the service name. The returned object will need to be + * casted into a {@link Service} + * + * @param serviceName a string + * @return an {@link Object} + */ + public Object lookup(String serviceName) { + if (serviceName.equals("jndi/serviceA")) { + System.out.println("Looking up service A and creating new service for A"); + return new ServiceImpl("jndi/serviceA"); + } else if (serviceName.equals("jndi/serviceB")) { + System.out.println("Looking up service B and creating new service for B"); + return new ServiceImpl("jndi/serviceB"); + } else { + return null; + } + } } diff --git a/service-locator/src/main/java/com/iluwatar/Service.java b/service-locator/src/main/java/com/iluwatar/Service.java index 6a6af8156..e9642e3d0 100644 --- a/service-locator/src/main/java/com/iluwatar/Service.java +++ b/service-locator/src/main/java/com/iluwatar/Service.java @@ -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(); } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceCache.java b/service-locator/src/main/java/com/iluwatar/ServiceCache.java index f175583ba..4d5a388c3 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceCache.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceCache.java @@ -5,42 +5,43 @@ import java.util.Map; /** * The service cache implementation which will cache services that are being created. - * On first hit, the cache will be empty and thus any service that is being requested, will be + * 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 + * be requested, it will be returned from the cache * + * @author saifasif */ public class ServiceCache { - - private Map serviceCache; - public ServiceCache() { - serviceCache = new HashMap(); - } + private final Map serviceCache; - /** - * Get the service from the cache. null if no service is found matching the - * name - * @param serviceName - * @return {@link Service} - */ - public Service getService(String serviceName){ - Service cachedService = null; - for (String serviceJndiName : serviceCache.keySet()){ - if( serviceJndiName.equals( serviceName ) ){ - cachedService = serviceCache.get(serviceJndiName); - System.out.println("(cache call) Fetched service " + cachedService.getName() + "("+cachedService.getId()+") from cache... !"); - } - } - return cachedService; - } + public ServiceCache() { + serviceCache = new HashMap(); + } - /** - * Adds the service into the cache map - * @param newService - */ - public void addService(Service newService){ - serviceCache.put(newService.getName(), newService); - } + /** + * 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) { + Service cachedService = null; + for (String serviceJndiName : serviceCache.keySet()) { + if (serviceJndiName.equals(serviceName)) { + cachedService = serviceCache.get(serviceJndiName); + System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !"); + } + } + return cachedService; + } + + /** + * Adds the service into the cache map + * + * @param newService a {@link Service} + */ + public void addService(Service newService) { + serviceCache.put(newService.getName(), newService); + } } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceImpl.java b/service-locator/src/main/java/com/iluwatar/ServiceImpl.java index 425c60a24..d0f865248 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceImpl.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceImpl.java @@ -1,37 +1,37 @@ 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 + * 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; - - public ServiceImpl(String serviceName) { - // set the service name - this.serviceName = serviceName; - - // Generate a random id to this service object - this.id = (int)Math.floor(Math.random()*1000)+1; - } - @Override - public String getName() { - return serviceName; - } + private final String serviceName; + private final int id; - @Override - public int getId() { - return id; - } + public ServiceImpl(String serviceName) { + // set the service name + this.serviceName = serviceName; - @Override - public void execute() { - System.out.println("Service " + getName() + " is now executing with id " + getId()); - } + // Generate a random id to this service object + this.id = (int) Math.floor(Math.random() * 1000) + 1; + } + + @Override + public String getName() { + return serviceName; + } + + @Override + public int getId() { + return id; + } + + @Override + public void execute() { + System.out.println("Service " + getName() + " is now executing with id " + getId()); + } } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceLocator.java b/service-locator/src/main/java/com/iluwatar/ServiceLocator.java index b8de9e3c2..7b6701f5f 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceLocator.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceLocator.java @@ -3,35 +3,34 @@ package com.iluwatar; /** * The service locator module. * Will fetch service from cache, otherwise creates a fresh service and update cache - * - * @author saifasif * + * @author saifasif */ public class ServiceLocator { - - private static ServiceCache serviceCache = new ServiceCache(); - - /** - * 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 - * @return {@link Service} - */ - public static Service getService(String serviceJndiName){ - Service serviceObj = serviceCache.getService(serviceJndiName); - if ( serviceObj != null ){ - return serviceObj; - } else { - /* - * If we are unable to retrive anything from cache, then + + private static ServiceCache serviceCache = new ServiceCache(); + + /** + * 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 a string + * @return {@link Service} + */ + public static Service getService(String serviceJndiName) { + Service serviceObj = serviceCache.getService(serviceJndiName); + if (serviceObj != null) { + return serviceObj; + } else { + /* + * If we are unable to retrive anything from cache, then * lookup the service and add it in the cache map */ - InitContext ctx = new InitContext(); - serviceObj = (Service) ctx.lookup(serviceJndiName); - serviceCache.addService(serviceObj); - return serviceObj; - } - } - + InitContext ctx = new InitContext(); + serviceObj = (Service) ctx.lookup(serviceJndiName); + serviceCache.addService(serviceObj); + return serviceObj; + } + } } From 33cd28f7cd1518c54b9307ce668f6d25e612f1b3 Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 10:06:22 +0200 Subject: [PATCH 7/7] [refactor] Remove unnecessary declarations in observer pattern. --- observer/src/main/java/com/iluwatar/generic/Observer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/observer/src/main/java/com/iluwatar/generic/Observer.java b/observer/src/main/java/com/iluwatar/generic/Observer.java index 49bb54a7d..33614b79d 100644 --- a/observer/src/main/java/com/iluwatar/generic/Observer.java +++ b/observer/src/main/java/com/iluwatar/generic/Observer.java @@ -1,5 +1,6 @@ package com.iluwatar.generic; public interface Observer, O extends Observer, A> { - public void update(S subject, A argument); + + void update(S subject, A argument); }