Created
March 25, 2014 10:12
-
-
Save halfdan/9758585 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 FactorialComputator { | |
int computeFactorial(int n); | |
} | |
class RecursiveFactorial implements FactorialComputator { | |
public int computeFactorial(int n) { | |
if (n == 1) { | |
return 1; | |
} | |
return this.computeFactorial(n-1) * n; | |
} | |
} | |
class InterativeFactorial implements FactorialComputator { | |
public int computeFactorial(int n) { | |
int fac = 1; | |
for(int i = 1; i<=n; i++) { | |
fac *= i; | |
} | |
return fac; | |
} | |
} | |
class SomeOtherClass { | |
FactorialComputator f; | |
public SomeOtherClass(FactorialComputator f) { | |
this.f = f; | |
} | |
public int getFactorial(int n) { | |
return f.computeFactorial(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment