|
using System;
using System.Collections.Generic;
using System.Text;
namespace Dependency.Injection.InterfaceInjection
{
public interface IFarm
{
//This method calculate number of farmers
//needed to take care of the animals in the farm
int NumberOfFarmers(int numberOfAnimals);
//This method calculate the area of farm
//needed to hold a given number of animals
double LandInAcres(int numberOfAnimals);
}
public interface IFarmFacade
{
//This method calculate number of farmers
//needed to take care of the animals in the farm
void SetFarmObject(IFarm farm);
}
class CowFarm : IFarm
{
#region "Constants"
int COW_PER_FARMER = 10;
int COW_IN_ACRE = 25;
int DEFAULT_NUM_OF_FARMERS = 1;
#endregion
#region "Methods"
public int NumberOfFarmers(int numberOfAnimals)
{
int farmers = DEFAULT_NUM_OF_FARMERS;
if (numberOfAnimals > COW_PER_FARMER)
farmers = (int)(numberOfAnimals / COW_PER_FARMER);
return farmers;
}
public double LandInAcres(int numberOfAnimals)
{
double acres = 1.0;
if (numberOfAnimals > COW_IN_ACRE)
acres = numberOfAnimals / COW_IN_ACRE;
return acres;
}
#endregion
}
class SheepFarm : IFarm
{
#region "Constants"
int SHEEP_PER_FARMER = 40;
int SHEEP_IN_ACRE = 100;
int DEFAULT_NUM_OF_FARMERS = 1;
#endregion
#region "Methods"
public int NumberOfFarmers(int numberOfAnimals)
{
int farmers = DEFAULT_NUM_OF_FARMERS;
if (numberOfAnimals > SHEEP_PER_FARMER)
farmers = (int)(numberOfAnimals / SHEEP_PER_FARMER);
return farmers;
}
public double LandInAcres(int numberOfAnimals)
{
double acres = 1.0;
if (numberOfAnimals > SHEEP_IN_ACRE)
acres = numberOfAnimals / SHEEP_IN_ACRE;
return acres;
}
#endregion
}
public class FarmFacade
{
private IFarm currentFarm;
public void SetFarmObject(IFarm farm)
{
//interface injection
currentFarm = farm;
}
public double TotalCost(int numberOfAnimals)
{
return ((50000 * currentFarm.NumberOfFarmers(numberOfAnimals)) + (100000 * currentFarm.LandInAcres(numberOfAnimals)));
}
}
}
|
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.