Created
October 14, 2018 01:17
-
-
Save daltonbr/72e09c7ad352652c4ab58241b246adfa to your computer and use it in GitHub Desktop.
SOLID Code - Interface Segregation Principle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AttackProcessor | |
{ | |
public void ProcessMelee(IHaveStats attacker, IHaveHealth target) | |
{ | |
int amount = CalculateAttackAmount(attacker); | |
ProcessAttack(target, amount); | |
} | |
public void CalculateAttackAmount(IHaveStats attacker) | |
{ | |
return attacker.STR * 2; | |
} | |
public void ProcessAttack(IHaveHealth target, int amount) | |
{ | |
target.ModifyHealth(amount) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Door : IHaveHealth | |
{ | |
float Health { get; set; } | |
int HealthMax { get; } | |
int ModifyHealth(int amount) | |
{ | |
Health -= amount; | |
// Do some door death | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A nice video about this Solid Principle | |
// https://www.youtube.com/watch?v=ll6bxQGkyCk&list=PLB5_EOMkLx_WjcjrsGUXq9wpTib3NCuqg&index=4 | |
// I put all interfaces in a single file for simplicity | |
public interface IHaveBuffs | |
{ | |
BuffController BuffController { get; } | |
} | |
public interface ICanBeMermerized | |
{ | |
void Mesmerize(Entity ownerEntity); | |
} | |
// Interface inheritance | |
public interface IHaveStats : IHaveHealth | |
{ | |
int STR { get; } | |
int STA { get; } | |
int CON { get; } | |
int WIS { get; } | |
int INT { get; } | |
int CHA { get; } | |
FloatStack<object> AttackSpeedStack { get; } | |
} | |
public interface IHaveHealth | |
{ | |
float Health { get; set; } | |
int HealthMax { get; } | |
int ModifyHealth(int amount); | |
} | |
public interface IHaveDamageShield | |
{ | |
DamageShieldController DamageShieldController { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NPC : IHaveStats | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment