|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 the 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 mind set, 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, this 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 bridge and adapter patterns to help you develop a method to allow decoupling of an abstraction class from its implementation class or to allow a change of implementation from an interface. This is generally used when you have a class that might change, but you don't want those changes to affect a number of clients. Note: This article is for helping developers understand how to convert their inline code to patterns better not provide strict examples on patterns. There are more than enough strict pattern examples, but to facilitate developers and others out there who are not familiar with patterns, I have written this and other articles like this. The adaptor and bridge patterns are very similar. The main difference between an adaptor and a bridge pattern, is that a bridge pattern serves to decouple an abstraction class from its implementation, and an adaptor pattern converts the interface between classes with less inheritance. I have reworked this article, from the demand of my readers, to illustrate the differences between the adaptor and bridge patterns. We will start with the bridge pattern, and at the end of the article I will show the difference between the two. BackgroundUsing a bridge or an adapter pattern is a form of encapsulation, which allows a developer to better control the way a client code base deals with the implementation class structure. How to use the codeWe start this example with the class public class WorkerObject
{
public WorkerObject()
{
}
public virtual void DoWork()
{
Console.WriteLine("Working...");
}
}
.....client code
WorkerObject obj = new WorkerObject();
obj.DoWork();
So, to solve this problem, we need to create a bridge or an adapter class that will allow a client to access methods on this object, but also make sure that the changes in this object will not break the client object's calls to the methods, even if we change the Note: Notice the class Bridge
{
private WorkerObject _workerObject;
public Bridge
{
ImplementationObject = new WorkerObject();
}
protected WorkerObject ImplementationObject
{
get{return _workerObject;}
set{_workerObject = value;}
}
public virtual void DoWork()
{
ImplementationObject.DoWork();
}
}
Now let's say we wanted to use a different object other than public class LoaferObject : WorkerObject
{
public LoaferObject()
{
}
public override void DoWork()
{
Console.WriteLine("Loafing...");
}
}
class AlternateBridge : Bridge
{
public AlternateBridge()
{
ImplementationObject = new LoaferObject();
}
public virtual void DoWork()
{
ImplementationObject.DoWork();
}
}
Now let's say we did not want to use class TotallyDifferntBridge : Bridge
{
public TotallyDifferntBridge()
{
ImplementationObject = null;
}
public virtual void DoWork()
{
TotallyDifferentObject diffObj = new TotallyDifferentObject();
diffObj.DoWork();
}
}
Now let's see how this would affect the client code. Well we started with a single class //.....client code calls the factory
Bridge adapter = BridgeFactory.GetAdapter();
adapter.DoWork();
Another way of using the //.....client code
Bridge adapter = new Bridge();
adapter.DoWork();
adapter = new AlternateBridge();
adapter.DoWork();
adapter = new TotallyDifferntBridge();
adapter.DoWork();
Now let's take a look at an alternative, less involved way to do the same kind of thing, with less need for inheritance, using the adaptor pattern. Here we see the class Adaptor
{
private WorkerObject _workerObject;
public Adaptor
{
_workerObject = new WorkerObject();
}
public virtual void DoWork()
{
_workerObject.DoWork();
}
}
Points of InterestThe adapter and bridge patterns offer us another way to make our code more flexible, and gives us another option for code design. Developers new to OO may have already found they have used a version of one of these patterns in their code before, or can find a myriad of uses immediately. Like many of the more commonly used patterns, these two patterns are useful in a wide variety of code situations. This is the fifth 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 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 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 second revision and is the fifth installment in a series.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||