Here\'s what I have so far:
namespace Strategy
{
interface IWeaponBehavior
{
void UseWeapon();
}
}
namespace Strategy
{
class Knife
Take a look at dependency injection & then also look into Inversion of Control.
TekPub has a great free episode on these two ideas using a sample very similar to yours. This should help you with a concrete sample. :-)
http://tekpub.com/view/concepts/1
Sample code from video:
class Samurai {
IWeapon _weapon;
public Samurai() {
_weapon = new Sword();
}
public Samurai(IWeapon weapon) {
_weapon = weapon;
}
public void Attack(string target) {
_weapon.Hit(target);
}
}
The above is DI (with IWeapon constructor). Now adding Inversion of Control you gain even more control.
Using an IOC container you will be able to control the IWeapon via injection instead of in the class itself.
The video uses NInject as it's IOC and shows how you can eliminate the IWeapon Constructor and gain control over the class and all classes needing to use IWeapon to use what you want in one location/configuration area.
Sample from video:
class WarriorModule: NinjectModule {
public override void Load() {
Bind().To();
Bind().To();
}
The above configuration tells NInject that whenever it encounters an IWarrior to use Samurai and whenever it encourters an IWeapon to use Sword.
The video goes on to explain how this little change increases your flexibility. It adds another Module for a Samuria who uses a ranged weapon, etc....
You can change these as needed/wanted.