C# gripe |
[Feb. 7th, 2008|09:30 pm]
“Nightwatch”
|
Hey C# programmers! FACTORY CLASSES ARE EVIL.
Do not use factory classes. Every single use of factory classes can be done much more cleanly using private classes. Just replace the is-a relationship with a has-a relationship.
Here's an example of the wrong way to do it, which I see all the time:
public abstract class Pokemon {
public void Attack();
}
public class Growlithe {
public void Attack() { ... }
}
public class Pikachu {
public void Attack() { ... }
}
public static class PokemonFactory {
public static Pokemon Create(string kind)
{
if (kind == "Growlithe")
return new Growlithe();
else
return new Pikachu();
}
}
This is Java-ish syntactic noise, and it leaks implementation details. Why should the caller have to care whether the object is internally a Growlithe or a Pikachu, when you meant to present the opaque type Pokemon to them?
Here's a better way to do it:
public class Pokemon {
private abstract class InternalPokemon {
public abstract void Attack();
}
private class Growlithe : InternalPokemon {
public void Attack();
}
private class Pikachu : InternalPokemon {
public void Attack();
}
private InternalPokemon ip;
public Pokemon(string kind)
{
if (kind == "Growlithe")
ip = new Growlithe();
else
ip = new Pikachu();
}
public void Attack()
{
ip.Attack();
}
}
This is less code, and it hides implementation details. It's also more flexible, because it allows you to use any object you want as the "internal" object. |
|