![]() |
Beginner
License: The Code Project Open License (CPOL)
Adapter Design Pattern How to use in Asp.net using C#By Salmanzzcodeproject What is Adapter Pattern? The Adapter pattern is a structural design pattern which enables a system to use classes whose interfaces don’t quite match its requirements or in other words is used to make a interface which client understands. When to use it? Its quite use |
C# (C#1.0, C#2.0, C#3.0), .NET (.NET2.0, .NET3.0, .NET3.5, .NET4.0), ASP.NET, Dev, Design
|
||||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
The Adapter pattern is a structural design pattern which enables a system to use classes whose interfaces don’t quite match its requirements or in other words is used to make a interface which client understands.
Lots of us uses the enterprise library data access application block. Suppose later if we find that some other third party data access application block is better than micorsoft data access application block then in the project would not change just adapter internally would call the new data access application block.
In Object adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.
// Existing class.. this may be your legacy code or third party code class Adaptee { // validates the email public bool IsEmail(string email) { return System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); } } // Required standard implementing through interface interface ITarget { // Rough estimate required bool ValidateEmail(string email); } // Implementing the required standard via Adaptee class Adapter : Adaptee, ITarget { public bool ValidateEmail(string email) { return IsEmail(email); } } class Client { static void Main() { // Showing the Adapteee in standalone mode Adaptee first = new Adaptee(); Console.Write("Before the new standard: "); Console.WriteLine(first.IsEmail("anyemail@gmail.com")); // What the client really wants ITarget second = new Adapter(); Console.WriteLine("\nMoving to the new standard"); Console.WriteLine(second.ValidateEmail("anyemail@gmail.com")); } }
ITarget
The interface that the Client wants to use
Adaptee
An implementation that needs adapting
Adapter
The class that implements the ITarget interface in terms of the Adaptee
ValidateEmail
An operation that the Client wants
IsEmail
The implementation of Request’s functionality in the Adaptee
Note :
The adapter pattern is also useful in where an already existing class provides some or all of the services you need but does not use the interface you need. Also using this way you can change third-party library easily without effecting your project . In above example if you want to change the validation method of email all you need to do is to use ValidateEmail function for any new method or third party library functions.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Aug 2009 Editor: |
Copyright 2009 by Salmanzz Everything else Copyright © CodeProject, 1999-2010 Web22 | Advertise on the Code Project |