hi,
been a long time since my last post eh?
sorry for that.
we shall discuss about a new design patter ntoday - Facade pattern
what exactly is a facade pattern?
technically speaking, it is a class which controls many other classes.something like a manager class.
let us take up a simple scenario :
i want to watch a movie at home..what all do i do?
1) i turn on the TV.
2) i turn on the speakers.
3) i turn on the DVD player
4) i turn off the lights
ok..so i have a class called user (that is me) which has to create instances of TV,DVD,Speakers,Lights and then on each of them call the start method. to switch off also its exactly same- we call the stop method. this is fine..no issues with this manner. but think of the user class, it has to maintain object references to the 4 classes. the user class may be very huge in itself. so we are increasing the burden on the user class. now if i have to have another class which has to perform the same 4 steps, tehn i have to have instances of these 4 classes again. and any new step included will change all the classes which perform these 4 steps.
so how can we make this more easier to manage? we will introduce a manager class in between..in this case "Remote" class.this will handle all the other 4 classes and perform the necessary steps.This is teh facade class.
advantages of using facade pattern are
1) the class (user class) which had to perform all the steps is not overloaded.
2) all the steps are maintained in a central location (Remote class).
look at the code sample below :
public interface IFacadeObject
{
void Start();
void Stop();
}
public class Remote : IFacadeObject
{
IFacadeObject DVDobj,SpeakerObj,LightObj,TVObj;
public Remote()
{
DVDobj = new DVD();
SpeakerObj = new Speakers();
LightObj = new Lights();
TVObj = new TV();
}
#region IFacadeObject Members
public void Start()
{
Console.WriteLine("Remote switched on");
TVObj.Start();
DVDobj.Start();
LightObj.Start();
SpeakerObj.Start();
}
public void Stop()
{
Console.WriteLine("Remote switched off");
TVObj.Stop();
DVDobj.Stop();
LightObj.Stop();
SpeakerObj.Stop();
}
#endregion
}
public class TV : IFacadeObject
{
public TV()
{
}
public void Start()
{
Console.WriteLine("TV switched on");
}
public void Stop()
{
Console.WriteLine("TV switched off");
}
}
public class AirCondition : IFacadeObject
{
public AirCondition()
{
}
public void Start()
{
Console.WriteLine("AirCondition switched on");
}
public void Stop()
{
Console.WriteLine("AirCondition switched off");
}
}
public class Speakers : IFacadeObject
{
public Speakers()
{
}
public void Start()
{
Console.WriteLine("Speakers switched on");
}
public void Stop()
{
Console.WriteLine("Speakers switched off");
}
}
public class DVD : IFacadeObject
{
public DVD()
{
}
public void Start()
{
Console.WriteLine("DVD switched on");
}
public void Stop()
{
Console.WriteLine("DVD switched off");
}
}
public class Lights : IFacadeObject
{
public Lights()
{
}
public void Start()
{
Console.WriteLine("Lights switched off");
}
public void Stop()
{
Console.WriteLine("Lights switched on");
}
}
This is our classes.
so when we call the "Remote" class instance, it will be doing all the necessary steps.
the call is made from the user class in this manner :
IFacadeObject RemoteObj = new Remote();
RemoteObj.Start();
Console.WriteLine("Things are working now ");
RemoteObj.Stop();
the output for this is
Remote switched on
TV switched on
DVD switched on
Lights switched off
Speakers switched on
Things are working now
Remote switched off
TV switched off
DVD switched off
Lights switched on
Speakers switched off
happy coding !!!!
regards,
nandan
No comments:
Post a Comment