Escolar Documentos
Profissional Documentos
Cultura Documentos
4.87 13 votes
This article shows a case study about how we use the Bridge Pattern to Elizabeth's Day Care Center
Background
Again, my favorite place Elizabeth's daycare center.
In Elizabeth's daycare center, all teachers are very friendly and try to make friends with all the kids.
At first, it's very hard to build a relationship between a new kid and a new teacher since they have never met each other. In order to
allow the teacher and the kid to communicate with each other, we somehow need to build a communication path to allow the
teacher/director anyone who wants to be a communicator to communicate with the kid.
A teacher Communicator object can talk to any kid talkable object, and a kid should also allow any teacher Communicator object to
start talking to him/her as well. Just like how printers work with computers. If we have a USB cable bridge, then we can connect any
printer to any computer to start printing. It really doesn't matter if it's a laser printer or a color printer, either Windows PC or Mac.
Because we know all the printers will allow the computers to print, makes sense?
Introduction
The Bridge Pattern could help us to separate the contract and implementation in two independent areas. We could use interface in C#
to build our contract. The contract will only provide/list the functionality, but leave the implementation to the implementer to handle. So
the contract can be used as a bridge within other classes, and it allows other classes to consume the concrete implementer without
knowing anything about the details.
This article introduces an implementation of how we use the bridge pattern to our daycare center.
Implementation Code
Abstraction Class
ICommunicator
ICommunicatoris an interface that will be inherited by other classes when those classes want to be able to communicate to an
ITalkableobject. In ICommunicatorclass, we have ITalkableobject defined and it could allow us to use all the
functionality from an Italkableobject. An Italkableobject acts as a bridge here to let ICommnucatorobject consume it.
usingSystem;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacewww.askbargains.com
{
namespaceBridgeDesignPattern
{
publicinterfaceICommunicator
{
//buildabridgebetweenaCommunicatorandatalkableobject(Kid)
ITalkableobjToTalk{get;set;}
//Startchattingprocess
voidStartChatting();
}
}
}
Teacher
Teacherclass, is a RefinedAbstractionclass, it actually implements the details for StartChatting() methods. In the
teacher class, I cast the ITalkableobject to a Kidobject in case I need to use some properties from the Kidclass. However, we
don't have to do the casting here since we only care about the ITalkablefunctionality.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacewww.askbargains.com
{
namespaceBridgeDesignPattern
{
publicclassTeacher:ICommunicator
{
KidaKid=newKid();
//or
//ITalkable_objToTalk
#regionICommunicatorMembers
publicITalkableobjToTalk
{
get
{
returnaKid;
}
set
{
aKid=(Kid)value;
}
}
//ImplementingtheChattingprocedures.
publicvoidStartChatting()
{
Console.WriteLine("What'syourname?");
aKid.TellMeAboutName();
Console.WriteLine("Howoldareyou?");
aKid.TellMeAboutAge();
Console.WriteLine("What'syourfavorfood");
aKid.TellMeAboutFavorFood();
//IuseobjToTalkasKidobjecttouseNamepropertyhere
//fordemopurpose
stringname=aKid.Name;
//Youdon'thavetocastobjToTalkobjecttoKid.
//objToTalk.TellMeAboutAge();
//objToTalk.TellMeAboutName();
//objToTalk.TellMeAboutFavorFood();
}
#endregion
}
}
}
Director
Same as Teacher, another ConcreteAbstractionclass. It directly uses the ITalkableobject aKid to implement the
StartChatting() method. No casting process here just to show a little differences with Teacher's Startchatting()
method.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacewww.askbargains.com.BridgeDesignPattern
{
publicclassDirector:ICommunicator
{
ITalkableaKid;
#regionICommunicatorMembers
publicITalkableobjToTalk
{
get
{
returnaKid;
}
set
{
aKid=value;
}
}
//Directorhasdifferentquestionsthanteacher.
publicvoidStartChatting()
{
Console.WriteLine("Hi,canyourtellmeyourname?");
aKid.TellMeAboutName();
Console.WriteLine("Hi,canyoutellmyyourage?");
aKid.TellMeAboutAge();
#endregion
}
}
Implementor Class
ITalkable
ITalkableinterface lists all the contract methods/functionality that a talkable class must have. A class that needs to be eligible to be
talked by a Communicatorobject needs to inherit the ITalkableinterface.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacewww.askbargains.com
{
namespaceBridgeDesignPattern
{
//itisgoingtobeconsumedbytheCommunicatorasabridgetotalktoany
//ITalkableobjects.AndalsoalltheITalkableobjectsmustprovidethe
//belowactivitiesaswell.
publicinterfaceITalkable
{
voidTellMeAboutName();
voidTellMeAboutAge();
voidTellMeAboutFavorFood();
}
}
}
Kid
Kidclass inherits from the ITalkableinterface to make itself talkable. It implements each ITalkablemethod by its own rules.
In this case study, I only define one Kidclass that inherits from ITalkableinterface. Actually, any class can inherit the
ITalkableinterface to make itselfable tobe talked by any Communicatorobject.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacewww.askbargains.com
{
namespaceBridgeDesignPattern
{
publicclassKid:ITalkable
{
publicstringName{get;set;}
publicintAge{get;set;}
publicstringFavorFood{get;set;}
#regionITalkerMembers
publicvoidTellMeAboutName()
{
Console.WriteLine("Mynameis{0}",Name);
}
publicvoidTellMeAboutAge()
{
Console.WriteLine("Iam{0}yearsold",Age.ToString());
}
publicvoidTellMeAboutFavorFood()
{
Console.WriteLine("Myfavorfoodis{0}",FavorFood);
}
#endregion
#endregion
}
}
}
Client App
From the client side, I create two kids Elizabeth& Ammie as our ITalkableobjects, and also create a Teacherand a
Directoras our ICommunicator objects.
Let both Teacherand Directorstart talking to the two kids, we will see both Elizabeth& Ammieare able to answer the
questions from the two ICommunicatorobjects.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingwww.askbargains.com.BridgeDesignPattern;
namespacewww.askbargains.com
{
namespaceClient
{
classProgram
{
staticvoidMain(string[]args)
{
//createateacherandadirector
TeacherMegan=newTeacher();
DirectorLisa=newDirector();
KidElizabeth=newKid();
Elizabeth.Name="Elizabeth";
Elizabeth.Age=3;
Elizabeth.FavorFood="ChickenNuggets";
KidAmmie=newKid();
Ammie.Name="Ammie";
Ammie.Age=4;
Ammie.FavorFood="FrenchFries";
//teacherMeganstartstalkingwithElizabeth
Console.WriteLine("MissMeganstartstalkingtoElizabeth");
Megan.objToTalk=Elizabeth;
Megan.StartChatting();
Console.WriteLine();
//DirectorLisastartstalkingwithAmmie
Console.WriteLine("DirectorLisastartstalkingtoAmmie");
Lisa.objToTalk=Ammie;
Lisa.StartChatting();
Console.WriteLine();
//TeacherMeganstartstalkingwithAmmie
Console.WriteLine("MissMeganstartstalkingtoAmmie");
Megan.objToTalk=Ammie;
Megan.StartChatting();
Console.WriteLine();
//DirectorLisastartstalkingwithElizabeth
Console.WriteLine("DirectorLisastartstalkingtoElizabeth");
Lisa.objToTalk=Elizabeth;
Lisa.StartChatting();
Console.Read();
}
}
}
}
Once we start our client app, you will see all that no matter who wants to talk to the kids, the kids will give the correct answer to the
communicators. Cool!
Conclusion
From this article, I demonstrated how we can use the Bridge Pattern to achieve the implementation for a communication system in
Elizabeth's daycare. I also use the daycare center for the Prototype Design pattern in my other articles as well.
History
30th September, 2009: Initial post
License
This article, along with any associated source code and files, is licensed under The Code Project Open License CPOL
Share
10 Ways to Boost COBOL Application Generate and add keyword variations using
Development AdWords API
Permalink | Advertise | Privacy | Terms of Use | Mobile Article Copyright 2009 by Allen _ Wang
Select Language
Web01 | 2.8.170514.1 | Last Updated 30 Sep 2009 Everything else Copyright CodeProject, 19992017