Created factory that has multiple ifs, which i really don't like. Is there a way to express the same some other way (polymorphism or smth else) so it looks prettier?
abstract class S { abstract void Draw(); }
class BO : S { ... }
class CR : S { ... }
class Cn : S { ... }
class R : S { ... }
class CIB : S { ... }
static class SFactory
{
static S SFactory Get(double cC, double cO, double pC, double pO,
double pL, double pH, bool isA = true, bool isB = false)
{
// conditions are mutually exclusive
if (cC >= cO && cO > pC && pC >= pO) return new BO(true);
if (cC <= cO && cO < pC && pC <= pO) return new BO(false);
if (pC < pO && cO < pC && cC > cO) return new CR(true);
if (pC <= pO && cO < pC && cC > cO && cC >= pC && isA) return new CR(true);
if (pC > pO && cO > pC && cC < cO) return new CR(false);
if (pC >= pO && cO > pC && cC < cO && cC <= pC && isA) return new CR(false);
if (cC > cO && cO < pC && cC > pH) return new Cn(true);
if (cC < cO && cO > pC && cC < pH) return new Cn(false);
if (pC < pO && cO > pC && cC > cO) return new R(true);
if (pC > pO && cO < pC && cC < cO) return new R(false);
if (cC > cO && pC > pO && cO < pC && cC <= pH) return new CIB(true);
if (cC < cO && pC < pO && cO > pC && cC >= pH) return new CIB(false);
return null;
}
Once constructed, those inheriting from S classes will draw themselves on the board, instead of having another long if-else chain for drawing. Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire