Resolve instance which implement multiple interfaces from Unity Container - c#

Please see my current code below
public interface ICoach
{
void Test();
}
public class Player : IPlayer
{
void IPlayer.Run()
{
Console.WriteLine(this.Name + "Run");
}
void IPlayer.Jump()
{
Console.WriteLine(this.Name + ": Jump");
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Player(string name)
{
Name = name;
}
}
public class Coach : ICoach
{
void ICoach.Test()
{
Console.WriteLine("Test");
}
}
public class SuperPlayer : ICoach, IPlayer
{
private int count = 0;
public void Test()
{
Console.WriteLine("Super player test");
count++;
}
public void Run()
{
Console.WriteLine("Super player run");
count++;
}
public void Jump()
{
Console.WriteLine("Super player jump");
count++;
}
public void PrintRunTimes()
{
Console.WriteLine(string.Format("Super player runs {0} times", count));
}
}
container.RegisterType<IPlayer, Player>();
container.RegisterType<ICoach, Coach>();
container.RegisterType<ICoach, SuperPlayer>("super", new ContainerControlledLifetimeManager());
container.RegisterType<IPlayer, SuperPlayer>("super", new ContainerControlledLifetimeManager());
The SuperPlayer class implement two interfaces: IPlayer and ICoach; the Player class implement IPlayer interfaces and the Coach class implement ICoach interafce.
I can resolve a SuperPlayer instance with the super parameter. But it is singleton. If I don't register like this,resolving from IPlayer and ICoach with super parameter will return 2 difference instances. Is there a way to resolve an SuperPlayer instance, not a singleton instance?

You could create a new ISuperPlayer interface that combines ICoach & IPlayer. Then implement, register, and resolve that interface as required.

Related

Inheritance, need advices

Let's say I have an ai or player, I want him to be able to use different weapons.
My design with weapons:
public class Weapon()
{
public virtual void FireWeapon(){} // this is useless for melee weapons
public virtual void SwingMelee(){} // this is useless for guns
public virtual void Reload(){} // this is also useless for melee weapons
}
Then in the ai controller class I simply call the function I want him to do.
This is where the ugly part is (I think)...
Controller class have a list containing some different weapons of ai and a weapon which is being used.
public class WeaponController
{
private List<Weapon> someWeapons;
private Weapon aWeapon;
public void Main()
{
if(/*"Some action or a button click" &&*/ aWeapon.CanSwingMelee() )
aWeapon.SwingMelee();
if(/*"Some action or a button click" &&*/ aWeapon.CanReload() )
aWeapon.Reload();
}
}
What is the better way to implement this? do you have any advices?
Seems that for every different action in a new weapon, I need to implement a function in the most parent Weapon class and I don't think it's a good idea...
The capability of an in-game object can be represented by an interface; you can check if a capability is present by attempting to cast to the interface. What's more, these interfaces can overlap, e.g. both melee and ranged weapons might both have an Attack method.
So for example:
public interface IWeapon
{
void Attack();
}
public interface IRangedWeapon
{
bool IsInRange(ITargetable target);
}
public interface IRequiresAmmunition
{
void Reload();
int AmmoRemaining { get; set; }
}
public class Sword : IWeapon
{
public virtual void Attack() { //code }
}
public class Rifle : IWeapon, IRequiresAmmunition, IRangedWeapon
{
public virtual void Attack() { //code }
public virtual void Reload() { //code }
public virtual int AmmoRemaining { get { } set { } }
public virtual bool IsInrange (ITargetable target) { //code }
}
public class LaserGun: IWeapon, IRangedWeapon
{
public virtual void Attack() { //code }
public virtual bool IsInrange (ITargetable target) { //code }
}
public class WeaponController
{
private List<IWeapon> someWeapons;
private IWeapon aWeapon;
private ITargetable currentTarget;
public void Weapon_OnUse()
{
if (!currentTarget.IsHostile) return;
if (this.IsInMeleeRange(currentTarget))
{
aWeapon.Attack();
return;
}
var w = aWeapon as IRangedWeapon;
if (w != null && w.IsInRange(currentTarget)
{
aWeapon.Attack();
return;
}
context.HUD.Warn("Out of range");
}
public void Weapon_OnReload()
{
var w = aWeapon as IRequiresAmmunition;
if (w != null)
{
w.Reload();
context.HUD.DisplayAmmo(w.AmmoRemaining);
}
}
}
This seems like what abstract classes and inheritance is for:
public abstract class Weapon {
public abstract void Attack();
public abstract void Reload();
}
public class MeleeWeapon : Weapon {
public override void Attack() {
// swing sword
}
public override void Reload() {
// ignore reload
}
}
public class GunWeapon : Weapon {
public override void Attack() {
// fire gun
}
public override void Reload() {
// load weapon from inventory
}
}
public class WeaponController {
private List<Weapon> someWeapons;
private Weapon aWeapon;
public void Main() {
if (/*"Some action or a button click" */)
aWeapon.Attack();
else if (/* some other button click */)
aWeapon.Reload();
}
}
I don't recommend an approach that requires you to create new interfaces for every new behavior and check the type of the weapon. What about something like this:
(This is a very rough draft.)
public abstract class Weapon
{
protected Weapon(WeaponCommandStrategy[] commandStrategies)
{
CommandStrategies = commandStrategies;
}
protected IEnumerable<WeaponCommandStrategy> CommandStrategies { get; }
public void Use(WeaponCommand command)
{
var strategy = CommandStrategies.FirstOrDefault(c => c.Command == command);
strategy?.Execute();
}
}
public enum WeaponCommand
{
Fire,
Swing,
Reload
}
public abstract class WeaponCommandStrategy
{
public WeaponCommand Command { get; private set; }
protected WeaponCommandStrategy(WeaponCommand command)
{
Command = command;
}
public abstract void Execute();
}
Now you can give a weapon whatever behaviors you want it to have in the form of various instances of WeaponCommandStrategy. If a command is sent to a weapon, it executes it. If it doesn't support a command it ignores it. You could add a property to a weapon exposing the available commands so that you could display a list of available commands.
public class Sword : Weapon
{
// Perhaps use dependency injection here
public Sword()
: base(new WeaponCommandStrategy[] { new SwordSwingStrategy() })
{
}
}
public class SwordSwingStrategy : WeaponCommandStrategy
{
public SwordSwingStrategy() : base(WeaponCommand.Swing) { }
public override void Execute()
{
// Do whatever it does
}
}
This essentially makes a Weapon a composition of various things that a weapon can do. If several weapons behave similarly they can share strategies vs. having code duplicated between various weapons.

Method from interface takes one parameter but it will be used with 2 different objects

I have to do a simple rpg game and there are 2 types of entities: heroes and monsters. Attack method will be implemented in both classes and it is contained by a interface called IAttack. The problem is that this method take a Monster type as parameter for hero class and a Hero type as parameter for monster class.
The code looks something like this:
The interface:
interface IAttack
{
void Attack(Object oponnnent);
}
The Hero class(which implements IAttack):
public void Attack(Monster opponent)
{
//code goes here
}
The Monster class(which implements IAttack):
public void Attack(Hero opponent)
{
//code goes here
}
The problem is I can not pass different types of arguments.
You could make an abstract class that Monster and Hero dervice from.
public abstract class PlayerType
{
public abstract int Health();
}
Then in your interface use the new abstract type:
interface IAttack
{
void Attack(PlayerType oponnnent);
}
Monster class:
public class Monster : PlayerType, IAttack
{
public override int Health()
{
return 100;
}
public void Attack(PlayerType hero)
{
}
}
Hero class:
public class Hero : PlayerType, IAttack
{
public override int Health()
{
return 500; // He is a hero afterall ;)
}
public void Attack(PlayerType monster)
{
}
}
Why not have two interfaces? Something that can attack and something that can be attacked?
public interface IAttackable
{
void OnAttacked(IAttacker attacker);
}
public interface IAttacker
{
void OnAttack(IAttackable opponet);
}
public class Hero : IAttacker, IAttackable
{
public void OnAttack(IAttackable opponet)
{
}
public void OnAttacked(IAttacker attacker)
{
}
}
public class Monster : IAttacker, IAttackable
{
public void OnAttack(IAttackable opponet)
{
}
public void OnAttacked(IAttacker attacker)
{
}
}

Nested Strategy Design Pattern

Suppose we have a player which controls a character named Player1. To enable switching weapons at runtime, I have the following code:
public interface IWeapon
{
void Fire();
}
public class Player1Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Player1Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public class Player1
{
IWeapon weapon;
Player1Weapon1 w1;
Player1Weapon2 w2;
public Player1()
{
w1 = new Player1Weapon1(this);
w2 = new Player1Weapon2(this);
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon = w;
}
}
That works perfectly.
But now a player can select another character named Player2.
Note that Player2's Weapons are different from Player1. So we can add Classes like this:
public class Player2Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Player2Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public class Player2
{
IWeapon weapon;
Player2Weapon1 w1;
Player2Weapon2 w2;
public Player2()
{
w1 = new Player2Weapon1(this);
w2 =new Player2Weapon2(this);
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon=w;
}
}
It will work again but it is very tight, if a player wants to play with Player3, I should addmore classes to the project .
I am wondering how to make Strategy Pattern for flayers like below:
interface IPlayer()
{
}
I don't know which methods will be placed in IPlayer? How can I create nested Strategy Design Patterns?
Not sure if I get what you are asking or if you don't know how/what to ask. Strategy pattern is not what you need the most here. I try to give my two cents.
First of all, I don't think it's a good idea to have concrete classes in your players, like Player1Weapon. It should only contain IWeapons. This way you don't need to specifically define which weapons your players use nor create new player classes for every variations.
Consider the following. You have these IWeapons in SomeNamespace namespace.
public interface IWeapon
{
void Fire();
}
public class Shotgun : IWeapon
{
public void Fire()
{
Console.WriteLine("Shotgun goes boom");
}
}
public class Knife : IWeapon
{
public void Fire()
{
Console.WriteLine("Stabbed teh sucker");
}
}
public class NuclearBomb : IWeapon
{
public void Fire()
{
Console.WriteLine("Game over for everyone!!1");
}
}
Now your Player class could look like below. Just add any level of abstraction you might need, here I just assume you don't.
public class Player
{
private IWeapon _wielded;
public Player(string name)
:this(name, null, null)
{}
public Player(string name, IWeapon primary, IWeapon secondary)
{
Name = name;
Primary = _wielded = primary;
Secondary = secondary;
Console.WriteLine(string.Format("Player '{0}' spawned", Name));
}
public void Switch()
{
_wielded = _wielded != Primary ? Primary : Secondary;
}
public void Fire()
{
if (_wielded != null)
_wielded.Fire();
}
public string Name { get; set; }
public IWeapon Primary { get; set; }
public IWeapon Secondary { get; set; }
}
To create "any player" you could have a simple factory to "spawn" them taking required attributes as parameter.
public class PlayerFactory
{
// key = player name, value = weapons
public Player Create(KeyValuePair<string, string[]> args)
{
var primary = Activator.CreateInstance(Type.GetType(args.Value[0])) as IWeapon;
var secondary = Activator.CreateInstance(Type.GetType(args.Value[1])) as IWeapon;
var player = new Player(args.Key, primary, secondary);
return player;
}
}
And now if you run the below "initialization"...
// this would come from config file or similar
var config = new List<KeyValuePair<string, string[]>>
{
new KeyValuePair<string,string[]>(
"Player1", new[] { "SomeNamespace.Shotgun", "SomeNamespace.Knife" }),
new KeyValuePair<string,string[]>(
"Player2", new[] { "SomeNamespace.NuclearBomb", "SomeNamespace.Knife" })
};
var factory = new PlayerFactory();
foreach (var entry in config)
{
var player = factory.Create(entry);
player.Fire();
player.Switch();
player.Fire();
}
... you end up with following console log
Player 'Player1' spawned
Shotgun goes boom
Stabbed teh sucker
Player 'Player2' spawned
Game over for everyone!!1
Stabbed teh sucker
When using the Strategy pattern, your design might look like
the UML diagrams below.
Player1 delegates performing the Fire() operation to one of different weapon classes Weapon1, Weapon2,...
For further discussion please see the Strategy design pattern
at http://w3sdesign.com.
public interface IWeapon
{
void Fire();
}
public class Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public interface IPlayer
{
void Update();
}
public class Player1 : IPlayer
{
private IWeapon weapon;
private IWeapon w1;
private IWeapon w2;
public Player1()
{
w1 = new Weapon1();
w2 = new Weapon2();
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon = w;
}
}

C# storing generics state

public abstract class State<T>
{
public virtual Enter(T item)
{
// an empty method
}
}
public class ChaseState : State<FieldPlayer>
{
public override Enter(Player pl)
{
// ...
pl.Fsm.CurrentState = ChaseState.Instance;
//...
}
}
public class TendGoal : State<Goalkeeper>
{
public override Enter(Goalkeeper gk)
{
// ...implementation
gk.Fsm.CurrentState = TendGoal.Instance;
// ...implementation
}
}
public class DefendState : State<Team>
{
public override Enter(Team team)
{
// ....
team.Fsm.CurrentState = DefendState.Instance;
//.....
}
}
"Goalkeeper" and "FieldPlayer" inherit from an abstract class "Player", while "Team" inherits from another class.
public class FSM
{
public /*some type*/ owner; // PROBLEM 1
// OWNER CAN BE TEAM, GOALKEEPEEPER
// OR FIELD PLAYER
public /*some type*/ globalState;
public /*some type*/ currentState;
public /*some type*/ previousState;
public void Update()
{
if (globalState != null)
{
globalState.Execute(owner); // PROBLEM 2
// IF GLOBAL STATE'S TYPE
// IS AN OBJECT, CANT CALL EXECUTE
// OBJECTS TYPE WILL BE KNOWN ONLY
// DURING RUNTIME
}
}
}
Each object of type "Goalkeeper", "FieldPlayer" and "Team" will have a State Machine instance. The problem is.. generics cant be properties.
What should I do ?
If you make State an ordinary interface, not generic, and have its Enter method take another interface that your teams, goalkeeprs, players, etc all implement (it can even just be empty), it ought to work.
public interface IOwner {}
public interface IState
{
void Enter(IOwner item);
}
public class ChaseState : IState
{
public void Enter(IOwner pl)
{
// ...
//...
}
}
public class Player :IOwner { }
public class Something {
IOwner owner = new Team();
IState globalState = new ChaseState();
IState currentState = new DefendState();
public void Update()
{
if (globalState != null)
{
globalState.Enter(owner);
}
else if (currentState != null)
{
currentState.Enter(owner);
}
}
}
After reading your code some more, an Abstract Class is unnecessary here. You should convert State to an interface, ex: IState and remove the generic signature from it. Then your properties in your FSM object can all be public IState globalState, etc..

Get an object out of a mixed type collection

Hi I'm new to OOP and I need help on a little problem.
I used a collection called Monsters to store 3 types of object. Spiders, Farmers, Gollum(irrelevant).
My collection as an indexer but when I use it to get an object out of the collection the object is typeless but I really need to TypeCast my next opperation.
private void Form1_Load(object sender, EventArgs e)
{
CurrentOpponent Opponent = new CurrentOpponent();
Gollum myGollum = new Gollum();
AngryFarmer myFarmer = new AngryFarmer();
Ugly_Spider mySpider = new Ugly_Spider();
myMonsters.AddGollum(myGollum);
myMonsters.AddFarmer(myFarmer);
myMonsters.AddUgly(mySpider);
progressBar1.Increment(100);
progressBar2.Increment(100);
Monster myCurrentOpponent = Opponent.randomEncounter();
//textBox1.Text = (this is where i need the type for a cast)myCurrentOpponent.name
}
Here is the randomEncounter where i extract the object
class CurrentOpponent
{
public Monster randomEncounter()
{
Random _random = new Random();
int opp = _random.Next(4);
return myMonsters[opp];
}
And finally the indexer wich returns a monster (parent of all 3 monster types)
public Monster this[int xxx]
{
get
{
return (Monster)List[xxx];
}
}
Help would be really appreciated..!!
Thanks in advance
Ideally, AngryFarmer, Ugly_Spider and Gollum should all inherit from Monster:
public class AngryFarmer : Monster
{
// ...
}
// etc.
You could then just use a List<Monster>:
myMonsters = new List<Monster>();
myMonsters.Add(new AngryFarmer()); // works because AngryFarmer is a kind of Monster
This will allow you to use polymorphism.
you need to use interfaces...... IMonster..... IMonster then has a name
then make all your monsters implement IMonster
and just have a List of IMonsters
you may wanna try it by using interfaces also! have a look...
public interface IMonster
{
String Name { get; }
Int32 Health { get; set; }
}
public class Spider : IMonster
{
public Spider()
{
_health = 100;
}
public string Name
{
get { return "Spider"; }
}
private int _health;
public int Health
{
get { return _health; }
set { _health = value; }
}
}
public class Gollum : IMonster
{
public Gollum()
{
_health = 250;
}
public string Name
{
get { return "Gollum"; }
}
private int _health;
public int Health
{
get { return _health; }
set { _health = value; }
}
}
class Program
{
static void Main(string[] args)
{
List<IMonster> monsters = new List<IMonster>()
{
new Gollum(),
new Spider()
};
IMonster randomMonster = GetRandomMonster(monsters);
Console.WriteLine(randomMonster.Name + "/" + randomMonster.Health);
}
private static IMonster GetRandomMonster(List<IMonster> monsters)
{
//Your code for getting a random monster goes here!
throw new NotImplementedException();
}
}
I like very much this approach... Imagine you have an element on your game that initially is not exactly a monster. Say it is a random element on your game that after a given event it becomes a monster that your Hero (say a game like heroes of mighty and magic) have to fight with. If you decided to add this feature long time after you created the game, it would become harmful/difficult/risky to change it, as this element might have already be inheriting from another class. If you were using interfaces you would simply implement it on this entity and it would promptly be capable of behaving like any other IMonster in your game. It means that this random entity would be able to be passed as a param to the method Fight(IHero hero, IMonster monster);
Ideally, AngryFarmer, Ugly_Spider and Gollum should all inherit
from Monster
I have learn your problem like the problem in the Tetris game:
1/ You have Monsters like I have Shapes.
2/ Each kind of Monster have it own properties (Health, Magic Point,...) and behaviours (attack, run, cast spell,..) like the Blocks have properties (color, position, state,..) and
behaviours (go down, rotate right, rotate left,...)
In the scene of the game you want to random a Monster that have the specific properties and behaviours, like I want to random a Shape. If it is your problem you can try my code:
public abstract class CMonster
{
int _HP;
int _MP;
//..and something like this
public int HP
{
get { return this._HP; }
set { this._HP=value;}
}
public int MP
{
get { return this._MP; }
set { this._MP = value; }
}
public abstract void Run();
public abstract void Attach();
public abstract void CastSpell();
}
public class CUgly_Spider : CMonster
{
public CUgly_Spider()
{
this.MP = 100;//your value here
this.HP = 100;//your value here
}
public override void Attach()
{
//your implemetation here
}
public override void Run()
{
//your implemetation here
}
public override void CastSpell()
{
//your implemetation here
}
}
public class CGollum : CMonster
{
public CGollum()
{
this.MP = 100;//your value here
this.HP = 100;//your value here
}
public override void Attach()
{
//your implemetation here
}
public override void Run()
{
//your implemetation here
}
public override void CastSpell()
{
//your implemetation here
}
}
class Test
{
private void InitTheGame()
{
CMonster curMonster=null;
Random rnd = new Random();
//sample random
if ((rnd.Next() % 2) == 0)
{
curMonster = new CGollum();
}
else
{
curMonster = new CUgly_Spider();
}
curMonster.Run();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
curMonster.Attach();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
curMonster.CastSpell();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
}
}
I hope that can help you.

Categories

Resources