Click here to Skip to main content
15,893,381 members
Articles / Desktop Programming / Windows Forms

Dependency Injection Pattern (Loose Coupling)

Rate me:
Please Sign up or sign in to vote.
2.82/5 (6 votes)
1 May 2008CPOL2 min read 40.5K   413   11  
Description of Dependency Injection Pattern
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
Education:
Master.CS
Bachelor.CIS
MCTS -- BizTalk Server

Sport:
Favorite teams are:
1) Atlas lions of morocco (soccer)
2) Canadians of Montreal (Hockey)




Comments and Discussions