Created
October 27, 2013 21:37
-
-
Save hlegius/7188168 to your computer and use it in GitHub Desktop.
Chain of Responsibility - GoF Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Authenticator { | |
private Authenticator next; | |
public static Authenticator chain() { | |
Authenticator defaultMethod = new EmailAuthenticator(); | |
Authenticator networkMethod = new NetworkAuthenticator(); | |
defaultMethod.setNext(networkMethod); | |
Authenticator partnerMethod = new PartnerAuthenticator(); | |
networkMethod.setNext(partnerMethod); | |
return defaultMethod; | |
} | |
public void setNext(Authenticator auth) { this.next = auth; } | |
public boolean login(String username, String password) { | |
if (canAuthenticate(username, password)) { | |
return authenticate(username, password); | |
} | |
if (next == null) { | |
return false; | |
} | |
return next.login(username, password); | |
} | |
abstract boolean canAuthenticate(String username, String password); | |
abstract boolean authenticate(String username, String password); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailAuthenticator extends Authenticator { | |
public boolean canAuthenticate(String username, String password) { | |
return username.indexOf("@") > 0; | |
} | |
public boolean authenticate(String username, String password) { | |
// do email authentication | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HellYeahController { | |
public void loginAction(Request request, Response response) { | |
boolean userLoggedIn = UserAuthenticator.login( | |
request.getParameter("[email protected]"), | |
request.getParameter("password1") | |
); | |
if (userLoggedIn) { | |
System.out.println("WOW!"); | |
} else { | |
System.out.println("OH NO!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkAuthenticator extends Authenticator { | |
public boolean canAuthenticate(String username, String password) { | |
return username.indexOf("/") > 0; | |
} | |
public boolean authenticate(String username, String password) { | |
// do network authentication | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PartnerAuthenticator extends Authenticator { | |
public boolean canAuthenticate(String username, String password) { | |
return username.startsWith("/") && username.length() > 12 && username.endsWith("@"); | |
} | |
public boolean authenticate(String username, String password) { | |
// do far away partner authentication | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Request { | |
public String getParameter(String key) { return key; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAuthenticator { | |
public static boolean login(String username, String password) { | |
return Authenticator.chain().login(username, password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is more LOC than POG version, but this one is more testable; extensible; readable and will keep you employed. :)