6/27/2014 Adapter Design Pattern - C#
Adapter Design Pattern - C#
P o st e d By : S h aile ndra Ch auh an, 0 8 Jul 2 0 1 3
U pdat e d On : 2 4 Jun 2 0 1 4
V e rs i o n Su p p o rt : A l l V e rs i o n C# & .N e t
Ke y wo rd s : wh e n t o u s e a d a p t e r d e s i g n p a t t e rn , a d a p t e r p a t t e rn wi t h e xa mp l e i n
.n e t , re a l l i fe e xa mp l e o f a d a p t e r p a t t e rn
A dapter pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. The
Adapter pattern allows a system to use classes of another system that is incompatible with it. It is
especially used for toolkits and libraries. In this article, I would like share what is adapter pattern and how is
it work?
What is Adapter Pattern
Adapter pattern acts as a bridge between two incompatible interfaces. This pattern involves a single class
called adapter which is responsible for communication between two independent or incompatible
interfaces.
For Example: A card reader acts as an adapter between memory card and a laptop. You plugins the
memory card into card reader and card reader into the laptop so that memory card can be read via laptop.
Adapter Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the Adapter design pattern is given below:
The classes, interfaces and objects in the above UML class diagram are as follows:
01. ITarget
This is an interface which is used by the client to achieve its functionality/request.
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 1/7
6/27/2014 Adapter Design Pattern - C#
02. Adapter
This is a class which implements the ITarget interface and inherits the Adaptee class. It is responsible for
communication between Client and Adaptee.
03. Adaptee
This is a class which have the functionality, required by the client. However, its interface is not
compatible with the client.
04. Client
This is a class which interact with a type that implements the ITarget interface. However, the
communication class called adaptee, is not compatible with the client
C# - Implementation Code
1. public class Client
2. {
3. private ITarget target;
4.
5. public Client(ITarget target)
6. {
7. this.target = target;
8. }
9.
10. public void MakeRequest()
11. {
12. target.MethodA();
13. }
14. }
15.
16. public interface ITarget
17. {
18. void MethodA();
19. }
20.
21. public class Adapter : Adaptee, ITarget
22. {
23. public void MethodA()
24. {
25. MethodB();
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 2/7
6/27/2014 Adapter Design Pattern - C#
26. }
27. }
28.
29. public class Adaptee
30. {
31. public void MethodB()
32. {
33. Console.WriteLine("MethodB() is called");
34. }
35. }
Adapter Pattern - Example
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as follows:
01. ITraget - Target interface
02. Employee Adapter - Adapter Class
03. HR System- Adaptee Class
04. ThirdPartyBillingSystem - Client
C# - Sample Code
1. /// <summary>
2. /// The 'Client' class
3. /// </summary>
4. public class ThirdPartyBillingSystem
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 3/7
6/27/2014 Adapter Design Pattern - C#
5. {
6. private ITarget employeeSource;
7.
8. public ThirdPartyBillingSystem(ITarget employeeSource)
9. {
10. this.employeeSource = employeeSource;
11. }
12.
13. public void ShowEmployeeList()
14. {
15. List<string> employee = employeeSource.GetEmployeeList();
16. //To DO: Implement you business logic
17.
18. Console.WriteLine("######### Employee List ##########");
19. foreach (var item in employee)
20. {
21. Console.Write(item);
22. }
23.
24. }
25. }
26.
27. /// <summary>
28. /// The 'ITarget' interface
29. /// </summary>
30. public interface ITarget
31. {
32. List<string> GetEmployeeList();
33. }
34.
35. /// <summary>
36. /// The 'Adaptee' class
37. /// </summary>
38. public class HRSystem
39. {
40. public string[][] GetEmployees()
41. {
42. string[][] employees = new string[4][];
43.
44. employees[0] = new string[] { "100", "Deepak", "Team Leader" };
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 4/7
6/27/2014 Adapter Design Pattern - C#
45. employees[1] = new string[] { "101", "Rohit", "Developer" };
46. employees[2] = new string[] { "102", "Gautam", "Developer" };
47. employees[3] = new string[] { "103", "Dev", "Tester" };
48.
49. return employees;
50. }
51. }
52.
53. /// <summary>
54. /// The 'Adapter' class
55. /// </summary>
56. public class EmployeeAdapter : HRSystem, ITarget
57. {
58. public List<string> GetEmployeeList()
59. {
60. List<string> employeeList = new List<string>();
61. string[][] employees = GetEmployees();
62. foreach (string[] employee in employees)
63. {
64. employeeList.Add(employee[0]);
65. employeeList.Add(",");
66. employeeList.Add(employee[1]);
67. employeeList.Add(",");
68. employeeList.Add(employee[2]);
69. employeeList.Add("\n");
70. }
71.
72. return employeeList;
73. }
74. }
75.
76. ///
77. /// Adapter Design Pattern Demo
78. ///
79. class Program
80. {
81. static void Main(string[] args)
82. {
83. ITarget Itarget = new EmployeeAdapter();
84. ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 5/7
6/27/2014 Adapter Design Pattern - C#
85. client.ShowEmployeeList();
86.
87. Console.ReadKey();
88.
89. }
90. }
Adapter Pattern Demo - Output
When to use it?
01. Allow a system to use classes of another system that is incompatible with it.
02. Allow communication between new and already existing system which are independent to each other
03. Ado.Net SqlAdapter, OracleAdapter, MySqlAdapter are best example of Adapter Pattern.
Note
01. Internally, Adapter use Factory design pattern for creating objects. But it can also use Builder design
pattern and prototype design pattern for creating product. It completely depends upon your
implementation for creating products.
02. Adapter can be used as an alternative to Facade to hide platform-specific classes.
03. When Adapter, Builder, and Prototype define a factory for creating the products, we should consider
the following points :
1. Adapter use the factory for creating objects of several classes.
2. Builder use the factory for creating a complex product by using simple objects and a step by step
approach.
3. Prototype use the factory for building a product by copying an existing product.
What do you think?
I hope you will enjoy the Adapter Pattern while designing your software. I would like to have feedback
from my blog readers. Your valuable feedback, question, or comments about this article are always
welcome.
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 6/7
6/27/2014 Adapter Design Pattern - C#
Print Article
Share this article with your friends!
in S h a r e 1 2
Tw eet
About the Author
Shailendra Chauhan works as Software Analyst at reputed MNC and has more than 5 years of hand
over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of
www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of book ASP.NET
MVC Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET,
MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery,
jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
http://www.dotnet-tricks.com/Tutorial/designpatterns/Y3OI080713-Adapter-Design-Pattern---C#.html 7/7