Created
March 2, 2018 17:55
-
-
Save wesleyduff/6f8bdd6c32e7ca0b86bb52976cc97403 to your computer and use it in GitHub Desktop.
C# composition over inheritance : DotNet Fiddle : https://dotnetfiddle.net/d6ZAG7
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
using System; | |
namespace Test { | |
//Interfaces : contracts for classes to be used for compoisiton | |
public interface IEat<T>{ | |
//set the food level | |
int Consume(T Item); | |
} | |
public interface ISpecies{ | |
void Draw(); | |
} | |
public enum Health { | |
Sick, ok, good, healthy | |
} | |
public interface ICharacterState { | |
Health CharacterState {get; set;} | |
} | |
public interface IHealthValue{ | |
int HealthLevel(int value); | |
} | |
//classes for componsition | |
public class Mamal : ISpecies{ | |
public void Draw(){ | |
Console.WriteLine("I am a mamal"); | |
} | |
} | |
public class Reptile : ISpecies { | |
public void Draw() { | |
Console.WriteLine("I am a reptile"); | |
} | |
} | |
public class MyHealth : IHealthValue{ | |
public virtual int HealthLevel(int value){ | |
if(value > 80){ | |
return 80; | |
} else if(value > 50){ | |
return 50; | |
} else if (value > 20){ | |
return 20; | |
} else { | |
return 5; | |
} | |
} | |
} | |
public class StateOfCharacter : MyHealth, ICharacterState { | |
public Health CharacterState {get; set;} | |
} | |
public class Food { | |
public int rejectLevel {get; set;} | |
public int consumeLevel {get; set;} | |
public string name {get; set;} | |
public int sugarLevel {get; set;} | |
public int fatLevel {get; set;} | |
public int vitaminLevel {get; set;} | |
} | |
public class Apple: Food { | |
public Apple(){ | |
this.rejectLevel = 1; | |
this.consumeLevel = 2; | |
this.name = "Apple"; | |
this.sugarLevel = 10; | |
this.fatLevel = 0; | |
this.vitaminLevel = 15; | |
} | |
} | |
public class Cake : Food { | |
public Cake(){ | |
this.rejectLevel = 0; | |
this.consumeLevel = 10; | |
this.name = "Cake"; | |
this.sugarLevel = 60; | |
this.fatLevel = 39; | |
this.vitaminLevel = 3; | |
} | |
} | |
public class Eat : IEat<Food> | |
{ | |
public int Consume(Food Item){ | |
Console.WriteLine("Yumm.... {0}", Item.name); | |
return Item.consumeLevel; | |
} | |
} | |
public class Reject : IEat<Food> { | |
public int Consume(Food Item){ | |
Console.WriteLine("Yuck... I hate {0}", Item.name); | |
return Item.rejectLevel; | |
} | |
} | |
//abstract class that contains all of the methods | |
public abstract class Character : IHealthValue, ISpecies { | |
private readonly IHealthValue _healthValue; | |
private readonly ICharacterState _characterState; | |
private readonly ISpecies _speicies; | |
public Character(IHealthValue healthValue, ICharacterState characterState, ISpecies species){ | |
_healthValue = healthValue; | |
_characterState = characterState; | |
_speicies = species; | |
} | |
public void Draw (){ | |
_speicies.Draw(); | |
} | |
public int HealthLevel(int value){ | |
return _healthValue.HealthLevel(value); | |
} | |
} | |
//concrete class that contains all of the functionality | |
public class Bear : Character { | |
public Bear() : base( | |
new MyHealth(), | |
new StateOfCharacter(), | |
new Mamal() | |
) { | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine("Hello World"); | |
Bear teddy = new Bear(); | |
teddy.Draw(); | |
Console.WriteLine(teddy.HealthLevel(80)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment