Created
July 17, 2013 17:50
-
-
Save v6ak/6022800 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| interface ICalculatingStrategy{ | |
| public int calculate(int a, int b); | |
| } | |
| class CalculatingStrategyFactory{ | |
| public static ICalculatingStrategy create(String strategy){ | |
| switch(strategy) { | |
| case "add": return new ICalculatingStrategy(){ | |
| public int calculate(int a, int b) { | |
| return a + b; | |
| } | |
| }; | |
| case "multiply": return new ICalculatingStrategy(){ | |
| public int calculate(int a, int b) { | |
| return a * b; | |
| } | |
| }; | |
| default: throw new IllegalArgumentException("Unsupported strategy"); | |
| } | |
| } | |
| } | |
| public class Main{ | |
| public static void main(String...args){ | |
| System.out.println( | |
| CalculatingStrategyFactory.create("multiply").calculate(3, 2) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment