Tuesday, August 22, 2006 12:55 PM
To me the adapter pattern is all about bringing someone elses code API into line with your Domain model. Their API may be a beautiful API but that doesn't mean that their concerns will match your concerns in the right way. Of course their API could be so clueless that the only to make sense of it is to hide it behind another class. These are both usage of the adapter pattern.
Another benefit of the Adapter pattern is that it can keep your code decoupled from third-party code. By providing a wrapper around say log4net you can easily switch later to NLog should it be necessary, by just changing the internals of the adapter class.
public class LoggingAdapter //log4net
{
ILog log = log4net.LogManager.GetLogger("MyApp"); //1
public static void Log(string message)
{
log.Info(message);
}
}
public class LoggingAdapter //NLog
{
Logger log = NLog.LogManager.GetCurrentClassLogger(); //1
public static void Log(string message)
{
log.Info(message);
}
}
So the interface of the LoggingAdapter didn't change at all, so we won't have to go and change our code at all except at the commented //1 locations. How cool is that?
Portland Repository
Wikipedia