Skip to content

Solution #1 - Using a DefaultMap as lookup table and a Strategy Pattern. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ repositories {
}

dependencies {
compile 'org.apache.commons:commons-collections4:4.0'

testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
}
Expand Down
10 changes: 10 additions & 0 deletions java/src/main/java/it/tug/Main/DoubleStringStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.tug.Main;

public class DoubleStringStrategy implements Strategy {

@Override
public String workOn(String theInput) {
return String.format("%s%s", theInput, theInput);
}

}
10 changes: 10 additions & 0 deletions java/src/main/java/it/tug/Main/ErrorStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.tug.Main;

public class ErrorStrategy implements Strategy {

@Override
public String workOn(String theInput) {
return "error";
}

}
17 changes: 10 additions & 7 deletions java/src/main/java/it/tug/Main/Formatter.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package it.tug.Main;

import org.apache.commons.collections4.map.DefaultedMap;


public class Formatter {

private Service service;
private DefaultedMap<String, Strategy> strategies;

public Formatter(Service service) {
this.service = service;

strategies = new DefaultedMap<String, Strategy>(new NullStrategy());
strategies.put("FAIL", new ErrorStrategy());
strategies.put("OK", new DoubleStringStrategy());
}

public String doTheJob(String theInput) {
String response = service.askForPermission();
if (response == "FAIL") {
return "error";
} else if (response == "OK") {
return String.format("%s%s", theInput, theInput);
} else {
return null;
}
Strategy strategy = strategies.get(response);
return strategy.workOn(theInput);
}

}
9 changes: 9 additions & 0 deletions java/src/main/java/it/tug/Main/NullStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.tug.Main;

public class NullStrategy implements Strategy {

@Override
public String workOn(String theInput) {
return null;
}
}
7 changes: 7 additions & 0 deletions java/src/main/java/it/tug/Main/Strategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.tug.Main;

public interface Strategy {

public String workOn(String theInput);

}