An Object Adapters is a wrapper class the wraps an old object that needs data manipulated before the new object can use it, just like you would with a real life object such as an electronic device where main power plugs are different in each country, so you will need a power adapter to plug it in. This post I will demonstrate one type of adapter which is the object adapter, this type of adapter basically contains an instance of the object that needs adapting or also known as the adaptee.
Lets put this into a real word situation where you have an old class called OldPerson which only allows you to set and get a person name by calling a member method called SetFullName() and GetFullName(). In the new system you have a new class called Person which only takes First and Last name as strings. So what do we do you ask, we need to write a PersonAdapter class!
The Person class implements the IPersion interface which contains four methods, SetFirstName(), GetFirstName(), SetLastName() and GetLastName(). In PersonAdapter class we will implement the IPerson interface so that we have the same implementation as Person class.
public class PersonAdapter : IPerson { // code here. }
In the PersonAdapter class constructer we are going to pass the OldPerson object as a parameter and retrieve the person’s full name. By using the string’s Split method we can separate the first and last name and assign it to the private member variables called firstName and LastName.
public PersonAdapter (OldPerson oldPerson) { this.oldPerson = oldPerson; var fullname = this.oldPerson.GetFullName(); this.firstName = fullname.Split(' ')[0]; this.lastName = fullname.Split(' ')[1]; }
So now if we want to create a new instance of a Person object and set the first and last name by calling the PersonAdapter’s member methods GetFirstName() and GetLastName() and assign each of the returned values to the Person object by calling SetFirstName() and SetLastName().
public static void Main (string[] args) { // Create an OldPerson that only allows a full name string. var oldPerson = new OldPerson(); oldPerson.SetFullName("Joe Bloggs"); Console.WriteLine ("OldPerson Full Name: {0}", oldPerson.GetFullName()); // Adapter will convert the OldPerson full name into seperate // firstname and lastname strings. var personAdapter = new PersonAdapter(oldPerson); // Create a new Person and set the first and last name. var person = new Person(); person.SetFirstName(personAdapter.GetFirstName()); person.SetLastName(personAdapter.GetLastName()); Console.WriteLine ("Person First Name: {0} Last Name: {1}", person.GetFirstName(), person.GetLastName()); }
This PersonAdapter class can be improved further by return a new instance of a Person object instead of just the first and last name. If you wish to read more about Adapter Patterns go to your local library or book shop and look for a book called Design Patterns: Elements of Reusable Object-Oriented Software.
Source Code:
- GitHub Repository – Design Patterns. Full source code for this post can be pulled from my GitHub repository.
Reference: