Adapt With Elegance

By admin | Oct 08, 2012

If you know when and how to apply them, Design Patterns are an invaluable tool for any programmer. They provide elegant solutions for common problems and using them will ramp up your programming skills.

My favorite Design Pattern is called the Adapter Pattern. Let’s say you have to combine multiple objects with incompatible interfaces into a single interface. Using the Adapter Pattern to fix the problem will make everyone think you’re a modern day MacGyver. The Adapter Design Pattern is also commonly nicknamed the “Wrapper Pattern” because you use code to wrap the existing class with the necessary interface to make it compatible.

To show you how it works, let’s demonstrate using a standard “Hello World” program in Objective-C:

We want a speaking Person to say “Hello World” using their own language.

- (void)speakingPerson:(Person *)person {

[person sayHelloWorld];

}

In our program, a Person says hello by using a standard method: sayHelloWorld. This example uses a Person’s translation (a hello string) to say “Hello World” in their own language.

// Person.h

@interface Person : NSObject {

NSString *hello;

}

- (void)sayHelloWorld;

@end

// Person.m

#import “Person.h”

@implementation Person

- (id)init {

self = [super init];

if (self) {

hello = @”Hello World in my own language”;

}return self;

}

- (void)sayHelloWorld {

NSLog(@”%@”, hello);

}

@end

We can set up a Spanish person and a French person to say hello using the same method, because each of them is a regular “Person.”

// Spanish.h

#import “Person.h”

@interface Spanish : Person

@end

// Spanish.m

#import “Spanish.h”

@implementation Spanish

- (id)init {

self = [super init];

if (self) {

hello = @”Hola Mundo”;

}return self;

}

@end// French.h

#import “Person.h”

@interface French : Person

@end

// French.m

#import “French.h”

@implementation French

- (id)init {

self = [super init];

if (self) {

hello = @”Bonjour Monde”;

}return self;

}

@end

However, a CoolAmerican is not a regular person and says hello in a different manner. A CoolAmerican will simply use his “whatsUp” method.

// CoolAmerican.h

@interface CoolAmerican : NSObject

- (void)whatsUp;

@end

// CoolAmerican.m

#import “CoolAmerican.h”

@implementation CoolAmerican

- (id)init {

self = [super init];

if (self) {

}return self;

}

- (void)whatsUp {

NSLog(@”What’s UUUUP World!”);

}
@end

To use a CoolAmerican as an “American” Person in our program, we would set him up so that he appears to act like a Person. We use his “whatsUp” method in the “sayHelloWorld” method instead of forcing him to use a hello string for a translation. Then, we can make him say Hello World like a regular Person.

// American.h

#import “Person.h”

#import “CoolAmerican.h”

@interface American : Person {

CoolAmerican *american;

}

@end

// American.m

#import “American.h”

@implementation American

- (id)init {

self = [super init];

if (self) {

american = [ [CoolAmerican alloc] init]; return self;

}

- (void)sayHelloWorld {

[american whatsUp];

}

@end

And voila! Without changing the CoolAmerican, we can get him to say Hello World his way but like a regular Person by adapting him to our specific method call.

Although what I’ve shown you here is a very basic example of using the Adapter Pattern, you can apply this same pattern to tame far more complicated programming challenges. For example, if you wanted to control all of the elevators in a building, but they each use different systems, you would use the Adapter Pattern to create a wrapper for each of the elevator systems. Now you can use them together in a consistent manner. Or maybe you have several employees that use different types of calendars and you want to update everyone’s calendar with a special event that applies to all of them. Again, Adapter Patterns save the day.

As you can see, the Adapter Pattern can efficiently solve an interface problem using very little code. However, Design Patterns are only useful if you can identify the problem that needs a Design Pattern solution. So, the next time you need to use a common interface for many objects that already use varying interfaces, remember the Adapter Pattern for a simple, elegant and compact solution. Everyone will think you’re a genius!

building_mobile_teams

Get in touch

Marketo Form