Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public interface IActor { ... }
  2. public interface IHealthUser : IActor { void AddHealth(int amount); }
  3. public interface IAction { bool Perform(IActor actor); }
  4. public class HealthPotion : IAction
  5. {
  6. public bool Perform(IActor actor)
  7. {
  8. var performedAction = false;
  9. var healthUser = actor as IHealthUser;
  10. if (healthUser != null)
  11. {
  12. healthUser.AddHealth(Amount);
  13. performedAction = true;
  14. }
  15. return performedAction;
  16. }
  17. }
  18.  
  19. var action = Item as IAction;
  20. if (action != null)
  21. {
  22. var performedAction = action.Perform(Owner); // Where 'Owner' is IActor
  23. ...
  24. }
  25.  
  26. public interface IAction<T> where T : IActor { void Perform(T actor); }
  27.  
  28. public class HealthPotion : IAction<IHealthUser>
  29. {
  30. public void Perform(IHealthUser healthUser)
  31. {
  32. healthUser.AddHealth(Amount);
  33. }
  34. }
  35.  
  36. // Do we have to cast and test for every possible IActor interface?
  37. var action = Item as IAction<IHealthUser>;
  38. var action = Item as IAction<IManaUser>;
  39. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement