|
namespace SampleProj
{
/// <summary>
/// This file demonstrates one of the uses of interfaces. Here, it shows how to keep code loosely coupled and easily extendable and maintainable.
/// </summary>
public enum AnimalType
{
Cat,
Dog
}
public class PetAnimals
{
IFoodCost _foodCost;
public PetAnimals(AnimalType animalType)
{
// Create IFoodCost object based on animal type that was sent to this class. Instantiation can be
// done in many other ways too like using something like Entierprise Library's unity application block
switch (animalType)
{
case AnimalType.Cat:
_foodCost = new Cat();
break;
case AnimalType.Dog:
_foodCost = new Dog();
break;
}
}
public decimal GetFoodCost()
{
return _foodCost.GetFoodCost();
}
}
interface IFoodCost
{
decimal GetFoodCost();
}
public class Dog : IFoodCost
{
public decimal GetFoodCost()
{
return 5M;
}
}
public class Cat : IFoodCost
{
public decimal GetFoodCost()
{
return 10M;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.