Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IActor { ... }
- public interface IHealthUser : IActor { void AddHealth(int amount); }
- public interface IAction { bool Perform(IActor actor); }
- public class HealthPotion : IAction
- {
- public bool Perform(IActor actor)
- {
- var performedAction = false;
- var healthUser = actor as IHealthUser;
- if (healthUser != null)
- {
- healthUser.AddHealth(Amount);
- performedAction = true;
- }
- return performedAction;
- }
- }
- var action = Item as IAction;
- if (action != null)
- {
- var performedAction = action.Perform(Owner); // Where 'Owner' is IActor
- ...
- }
- public interface IAction<T> where T : IActor { void Perform(T actor); }
- public class HealthPotion : IAction<IHealthUser>
- {
- public void Perform(IHealthUser healthUser)
- {
- healthUser.AddHealth(Amount);
- }
- }
- // Do we have to cast and test for every possible IActor interface?
- var action = Item as IAction<IHealthUser>;
- var action = Item as IAction<IManaUser>;
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement