Currently I am trying to implement some design structures and the factory seems most appropriate and as far as dependency injection goes I much prefer constructor injection. However the issue arises that not all my products require the same dependancies, which kind of messes with the pattern...
My abstract Factory .get() method will have to look like so
abstract class AbstractSpellFactory
{
public abstract WCSpell get(SpellSubType sSubType,SpellCard,int a,int b,int c,int d);
}
and for completion sake here is the spell class in the context of what i am doing/using
public abstract class WCSpell
{
public abstract void CastSpell();
}
and then i can use it like
AbstractSpellFactory aSpellFactory = SpellFactory.createSpellFactory(SpellType.buff);
WCSpell spell = aSpellFactory.get(SpellSubType.Positive,sCard,1,2,3,4);//OK
spell.CastSpell();
aSpellFactory = SpellFactory.createSpellFactory(SpellType.rotate);
spell = aSpellFactory.get(SpellSubType.clockwise,sCard,0,0,0,0);//Non-used/Needed values...
spell.CastSpell();
So this works, but first and foremost the fact that the rotate spell does not need the integers is a little inelegant, but the biggest problem of all is if i add any new spells that have different dependancies the AbstractSpellFactory.get(...) method argument parameters are just going to get bigger and based on the spell type it might not even need/have the values being passed in.
So I am a tad stuck, does anyone have any suggestions?
Psuedo Implementation of code above
Spell Factory class
static class SpellFactory
{
public static AbstractSpellFactory createSpellFactory( SpellType sType )
{
AbstractSpellFactory sFactory = null;
switch(sType)
{
case SpellType.kBuff:
{
sFactory = new SpellBuffFactory();
}
break;
case SpellType.kRotateClockWise:
{
sFactory = new SpellRotateFactory();
}
break;
}
return sFactory;
}
}
Buff Spell Factory
public class SpellBuffFactory : AbstractFactory
{
public override Spell get( SpellSubType sSubType,SpellCard sCard,int a,int b,int c,int d)
{
Spell spell = null;
switch(sSubType)
{
case Positive:
{
spell = new BuffSpell(a,b,c,d,sCard);
}
break;
case Negative:
{
spell = new BuffSpell(-a,-b,-c,-d,sCard);//some check to make sure all values are negative
}
}
return spell;
}
}
Rotate Spell Factory
public class SpellRotateFactory : AbstractFactory
{
public override Spell get( SpellSubType sSubType,SpellCard sCard,int a,int b,int c, int d)
{
Spell spell = null;
switch(sSubType)
{
case Clockwise:
{
spell = new WCRotateSpell(WCRotateSpell.RotationDirection.Clockwise,sCard);
}
break;
case CounterClockwise:
{
spell = new WCRotateSpell(WCRotateSpell.RotationDirection.CounterClockwise,sCard);
}
}
return spell;
}
}