Skip to content

Instantly share code, notes, and snippets.

@daltonbr
Created October 14, 2018 01:17
Show Gist options
  • Save daltonbr/72e09c7ad352652c4ab58241b246adfa to your computer and use it in GitHub Desktop.
Save daltonbr/72e09c7ad352652c4ab58241b246adfa to your computer and use it in GitHub Desktop.
SOLID Code - Interface Segregation Principle
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)
}
}
public class Door : IHaveHealth
{
float Health { get; set; }
int HealthMax { get; }
int ModifyHealth(int amount)
{
Health -= amount;
// Do some door death
}
}
// 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; }
}
public class NPC : IHaveStats
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment