Created
January 2, 2014 22:32
-
-
Save JayBazuzi/8228363 to your computer and use it in GitHub Desktop.
Replaced all conditionals with polymorphism
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 I | |
{ | |
string Get(int i); | |
} | |
private class Fizz : I | |
{ | |
public string Get(int i) | |
{ | |
return "Fizz"; | |
} | |
} | |
class Buzz : I | |
{ | |
public string Get(int i) | |
{ | |
return "Buzz"; | |
} | |
} | |
class FizzBuzz : I | |
{ | |
public string Get(int i) | |
{ | |
return "FizzBuzz"; | |
} | |
} | |
class Number : I | |
{ | |
public string Get(int i) | |
{ | |
return i.ToString(); | |
} | |
} | |
public string GetFizzBuzz(int i) | |
{ | |
return new I[] { | |
new FizzBuzz(), //15 | |
new Number(), // 1 | |
new Number(), // 2 | |
new Fizz(), // 3 | |
new Number(), // 4 | |
new Buzz(), // 5 | |
new Fizz(), // 6 | |
new Number(), // 7 | |
new Number(), // 8 | |
new Fizz(), // 9 | |
new Buzz(), //10 | |
new Number(), //11 | |
new Fizz(), //12 | |
new Number(), //13 | |
new Number(), //14 | |
}[i % 15].Get(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh, I really like this. I didn't think I would but I do!
I don't think this would be universally applicable to all conditionals (thought I wish it were) considering the
new I[] {…}[i % 15].Get(i)
is akin to hard coding instead of algorithmicly determining the outcome.Then again the counter argument is that this is more configuration over computation and allows for more customization. I need to meditate on this 💭