By the name itself it denotes the bridge between the two objects. Suppose first time any new associate joined to the new organization and that time HR introduces the associate to the lead under whom he will work for. Hr will make the communication link between the lead and associate.
The Bridge Pattern could help us to separate the contract and implementation in two independent areas.
Now demonstrating the example how in code level it will behave.
There is a communicator class which will talk between the two objects.
ICommunicator – is an interface which will use for communicating the objects.
ITalk – is interface which is the conversation details between associates and Leader or associate and HRAssociates.
HrAssosiate - is class which is inheriting the ICommunicator.
Leader - is class which is inheriting the ICommunicator.
Final code will be like;
namespace BridgePattern
{
class Program
{
static void Main(string[] args)
{
//create a teacher and a director
Leader oBob = new Leader();
HRAssosiates oEric = new HRAssosiates();
Assosiate oAli = new Assosiate();
oAli.Name = "Ali sigh";
oAli.Experience = 3;
oAli.Technology = "ASP.NET";
Assosiate Danny = new Assosiate();
Danny.Name = "Danny";
Danny.Experience = 4;
Danny.Technology = "JQuery";
//teacher Megan starts talking with Elizabeth
Console.WriteLine("--Megan starts talking to Ali");
oBob.objTalk = oAli;
oBob.StartChatting();
Console.WriteLine();
//Director Lisa starts talking with Ammie
Console.WriteLine("---HR ERIC starts talking to dany");
oEric.objTalk = Danny;
oEric.StartChatting();
Console.WriteLine();
//Teacher Megan starts talking with Ammie
Console.WriteLine("--Megan starts talking to dany");
oBob.objTalk = Danny;
oBob.StartChatting();
Console.WriteLine();
//Director Lisa starts talking with Elizabeth
Console.WriteLine("---HR Lisa starts talking to Ali");
oEric.objTalk = oAli;
oEric.StartChatting();
Console.Read();
}
}
public interface ICommunicator
{
//build a bridge between a Communicator and a talkable object (Assosiate)
ITalk objTalk { get; set; }
//Start chatting process
void StartChatting();
}
public interface ITalk
{
void TellMeAboutName();
void TellMeAboutExperince();
void TellMeAboutTechnology();
}
public class Assosiate : ITalk
{
public string Name { get; set; }
public int Experience { get; set; }
public string Technology { get; set; }
#region ITalk Members
public void TellMeAboutName()
{
Console.WriteLine("My name is {0}", Name);
}
public void TellMeAboutExperince()
{
Console.WriteLine("I Have {0} years of experince", Experience.ToString());
}
public void TellMeAboutTechnology()
{
Console.WriteLine("I am working on {0}", Technology);
}
#endregion
}
public class HRAssosiates : ICommunicator
{
ITalk aAssosiate;
public ITalk objTalk
{
get
{
return aAssosiate;
}
set
{
aAssosiate = value;
}
}
//Director has different questions than teacher.
public void StartChatting()
{
Console.WriteLine("Hi , can your please tell me your name?");
aAssosiate.TellMeAboutName();
Console.WriteLine("Hi , can you please tell my your experience?");
aAssosiate.TellMeAboutExperince();
}
}
public class Leader : ICommunicator
{
Assosiate aAssosiate = new Assosiate();
#region ICommunicator Members
public ITalk objTalk
{
get
{
return aAssosiate;
}
set
{
aAssosiate = (Assosiate)value;
}
}
//Implementing the Chatting procedures.
public void StartChatting()
{
Console.WriteLine("What's your name?");
aAssosiate.TellMeAboutName();
Console.WriteLine("What is your Experience?");
aAssosiate.TellMeAboutExperince();
Console.WriteLine("In which technology you work for");
aAssosiate.TellMeAboutTechnology();
//for demo purpose
string name = aAssosiate.Name;
}
#endregion
}
}
Output will display like;
No comments:
Post a Comment