One of the important design patterns is the adapter design pattern, let's say you have new mobile and the old charger not compatible with the new mobile then you have two options to buy a new charger or buy an adapter that reuses the old charger, so we buy an adapter instead of buy a new charger because the charger is more expensive.

So this behavior what adapter design pattern use is to reuse existing code instead of adding new implementation.

Let's go in-depth and show examples.
I have an adaptee EmployeeManager class that get employees in XML like this,
- public class EmployeeManager {
- public List < Employee > employees;
- public EmployeeManager() {
- employees = new List < Employee > ();
- this.employees.Add(new Employee(1, "John"));
- this.employees.Add(new Employee(2, "Michael"));
- this.employees.Add(new Employee(3, "Jason"));
- }
- public virtual string GetAllEmployees() {
- var emptyNamepsaces = new XmlSerializerNamespaces(new [] {
- XmlQualifiedName.Empty
- });
- var serializer = new XmlSerializer(employees.GetType());
- var settings = new XmlWriterSettings();
- settings.Indent = true;
- settings.OmitXmlDeclaration = true;
- using(var stream = new StringWriter())
- using(var writer = XmlWriter.Create(stream, settings)) {
- serializer.Serialize(writer, employees, emptyNamepsaces);
- return stream.ToString();
- }
- }
- }


Join the conversation! Your thoughts help the community grow.