|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionDevelopers in .NET sometimes come from scripting language environments that are not strong on Object Oriented methodologies. OO methodologies like Refactoring and using design patterns can be intimidating and the value for the developer is hard to see. What developers new to the OO world, and even more seasoned developers, need to understand is that good design is not beyond scope in any project. Good habits simply must be learned that will make the developer a better designer. In that mindset, I am submitting the first of several "how to" real world examples, used in my professional life, on how to use patterns in a simple, easy to follow manner. Many of us have ended up either writing or working on in-line, scripting based code. Taking some ideologies from refactoring methodology, is not necessarily a bad thing. Of course, when you are first writing an algorithm or a series of logic statements, it seems easier to start with This article details a real world example of how to use the mediator pattern to help you develop a method to communicate between several classes. The mediator pattern is used when mediation is needed between several classes, to communicate in an indirect manner. BackgroundI have used mediators in several different ways throughout my professional career, but the cleanest way to recognize a mediator pattern is any class that facilitates indirect communication between a series of other classes. This example is fairly light in inheritance, simply because I did not want to add too much into the conversion between How to use the codeThe first thing we should note is the public class WorkerObject
{
private string _message;
public WorkerObject(string message)
{
_message = message;
}
public string Message
{
get{return _message;}
set{_message = value;
}
public SendMessage(string message)
{
Console.WriteLine("Message sent : " + message);
}
}
Next we see the functional code, as it exists at the beginning of the refactoring effort. Notice the class construction declarations, each passing in a slightly different WorkerObject senderObject = new WorkerObject("message0");
WorkerObject workerObject1 = new WorkerObject("message1");
WorkerObject workerObject2 = new WorkerObject("message2");
WorkerObject workerObject3 = new WorkerObject("message3");
if(!workerObject1.Message.Equals(senderObject.Message)
{
workerObject1.SendMessage(senderObject.Message);
}
if(!workerObject2.Message.Equals(senderObject.Message)
{
workerObject2.SendMessage(senderObject.Message);
}
if(!workerObject3.Message.Equals(senderObject.Message)
{
workerObject3.SendMessage(senderObject.Message);
}
Now to move this code from the overly cumbersome public class DoSomeMediation
{
private static ArrayList _workerObjects = new ArrayList();
public static int Register(WorkerObject workerObject)
{
return _workerObjects.Add(workerObject);
}
public static void SendMessage(WorkerObject senderObject)
{
if(senderObject == null) return;
string messageToSend = senderObject.Message;
foreach(WorkerObject workerObject in _workerObjects)
{
//send message to all other objects registered
if(!workerObject.Message.Equals(senderObject.Message))
workerObject.SendMessage(messageToSend);
}
}
}
Next we see the execution code. Notice that while we still construct the WorkerObject senderObject = new WorkerObject("message0");
WorkerObject workerObject1 = new WorkerObject("message1");
WorkerObject workerObject2 = new WorkerObject("message2");
WorkerObject workerObject3 = new WorkerObject("message3");
DoSomeMediation.Register(senderObject);
DoSomeMediation.Register(workerObject1);
DoSomeMediation.Register(workerObject2);
DoSomeMediation.Register(workerObject3);
DoSomeMediation.SendMessage(senderObject);
Points of interestThis particular example seems to be redoing the This is the third installment in the series I am writing on real world design patterns. All examples and the bulk of this article are taken from my professional experience as an architect. The examples given are templates only, and the designer must keep in mind that they are the ones who must decide where different patterns, if any, may be best used in their code. Deciding to perform a refactoring effort from the existing code to a pattern must be weighed on the necessity and need of the code itself. Patterns are only design templates, helpers to accommodate better overall design. I must stress that making an effort to use the patterns will strengthen your overall design ability, but like your basic coding skills, it is something that is to be learnt and cultivated. If this or any other in this series on design patterns is helpful or you have questions or comments please e-mail me at chris.lasater@gmail.com. HistoryThis is the first revision and is the third installment in a series.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||