I am currently reading been reading Professional ASP.NET MVC 2: NerdDinner and in the book there is use of a couple of patterns like repository patterns ,dependency control and others (still reading ) .The was one aspect that caught me and i think it was very interesting It was about loose design with interfaces that can make test driven development(TDD)easy .Objects are not to be passed as parameters just what they do is passed as parameters .After understanding it i decided to try to implement it by building a small home simulator .The program simple simulates a home .I am still learning but to learn something you have to start by learning something somewhere in some way you can understand .
I could have just created a home object and other homes can inherited from it.
What if i want simulate a hotel someday ?
Do i have to start everything adding a bigger home and doing changes in the code in many places .?
All i want is a simulator for a home .The object can simulate home activities .This object can simulate any home .
Call it and tell it how you want the home to be simulated.
Lets simulate a home using interfaces .
namespace LearningInterfaces { public interface ISimulateHomeActivities // main interface { void CookFood(string foodType); void Sleep(string time); void DoLaundry(); } }
This class uses the interface to simulate the home .
namespace LearningInterfaces { public class HomeActivities // main simulator manager { public void DoActivity(ISimulateHomeActivities activity, string foodType , string sleepTime); { activity.DoLaundry(); activity.CookFood(foodType); activity.Sleep(sleepTime); } } }
Its time to get homes that want to be simulated
The Talla's home is the first on the list .It implements the ISimulateHomeactivities
namespace LearningInterfaces { public class TallasHome:ISimulateHomeActivities { #region ISimulateHomeActivities Members public void CookFood(string foodType) { Console.WriteLine("We are going to cook " + foodType + " today"); } public void Sleep(string time) { Console.WriteLine("We usually sleep at " + time); } public void DoLaundry() { Console.WriteLine("We are doing laundry "); } #endregion } }
Peter loved my home simulator and wants his home to be simulated
namespace LearningInterfaces { public class PetersHome:ISimulateHomeActivities { #region ISimulateHomeActivities Members public void CookFood(string foodType) { Console.WriteLine("We are going to cook " + foodType + " today"); } public void Sleep(string time) { Console.WriteLine("We usually sleep at "+ time); } public void DoLaundry() { Console.WriteLine("We are doing laundry "); } #endregion } }Finally lets get the simulation running
using System; using System.Collections.Generic; using System.Linq; using System namespace m.Text; LearningInterfaces { class Program { static void Main(string[] args) { var tallasHome = new TallasHome(); var petersHome = new PetersHome(); var activities = new HomeActivities(); activities.DoActivity(tallasHome, "Rice and Chicken", "10 pm"); Console.WriteLine("----------------------------------------------"); activities.DoActivity(petersHome, "Pasta with Fish", "11:30 pm"); } } }
The simulator running prints out the following------simulating Talla,s and Peter's home
We are doing laundry
We are going to cook Rice and Chicken today
We usually sleep at 10 pm
----------------------------------------------
We are doing laundry
We are going to cook Pasta with Fish today
We usually sleep at 11:30 pm
Press any key to continue . . .
Happy programming ............................
Thanks for this post. What you've written here is exactly what I have read from other developers as well. I understand the concept of interfaces. Here is my exact situation, I am using the repository pattern in laravel wherein my code is organized as follows (model-repository-interface-controller->view). In a typical controller constructor, I am injecting the interface like so
ReplyDeletepublic function __construct(IAdminRepository $admin){
$this->admin = $admin;
}
A concrete class AdminRepository implements the method defined in the interface IAdminRepository. How would testing had been difficult if I didn't have the interface IAdminRepository? (possible to go this route with the framework) and just inject the concrete object directly like below:
public function __construct(AdminRepository $admin){
$this->admin = $admin;
}
I am doing it like this because I have read that TDD is easier, extending the app is easier, etc but I really wanna appreciate the concept.