Skip to content

Greedy Boss – Adapter Pattern

July 28, 2013

You work for a company which its goal is to held concerts all of the world, your job is to model seats of concerts and everything is clear, You know Object Oriented very well and this task is just a kidding 😉 So you start your modeling. In this domain you have some seats, some of them are first class and the others are business. Audience can sit on a sit if the seat is empty.

So you start your task, Hmmm maybe at first you like to model seats with an interface, it is a good choice, at all preferring interfaces to concrete class is not bad.

public interface Seat {
 void sit(Audience audience);

Boolean isEmpty();

void release();

Audience getAudience();

Seat interface is very simple, understandable and you follow “Interface Segregation Principle”  well. Now you implement Business seat and First class seat very easy. Maybe there are some duplicated code that you can solve them with an abstract class, because their duplicated code is static, but it’s not our case.

Your boss wants to held a concert in a green field! At first it’s just strange but you think why your boss wants to do that? Boss found a green field with many stumps! HAHA you are right, your boss wants to sell ticket for sitting on stumps too. He wants you change your model to support stumps as seat too. Stump implementation has been specified before your entrance and you have to use it.

public class Stump {
 private boolean empty;

public void sit() {
 empty = false;

public void release() {
 empty = true;

public boolean isEmpty() {
return empty;

Very very simple implementation of stump, but don’t forget you must not change the stump class to implement your Seat interface, it’s obvious. Stump is not a seat at all just you want to use it as a seat in your current model. So what is your solution?

All you need is to extend stump and add new Seat interface to it. For this purpose you can create a new class which implements Seat interface, then your new class has a composition relation with Stump class. then you can write your adapter codes in your new intermediate class.

public class StumpAdapter implements Seat {
 private final Stump stump;
 private Audience audience;

public StumpAdapter(Stump stump) {
 this.stump = stump;

 public void sit(Audience audience) {
 this.audience = audience;

 public Boolean isEmpty() {
 if (this.audience == null) {
 return this.stump.isEmpty();
 return false;


 public void release() {
 this.audience = null;

 public Audience getAudience() {
 return this.audience;

In your new Adapter you keep information and add behaviors that you need to adapt your legacy interface  or class to need interface or class.

Now for testing your code you can write an epic test:

public class ConcertTest {
 public void concert_epic() {
 SimpleAudience audience1 = new SimpleAudience();
 Stump stump = new Stump();
 final Seat stumpAsSeat = new StumpAdapter(stump);
 Assert.assertEquals(audience1, stumpAsSeat.getAudience());


Congratulation you could adapt your legacy code with your new design, You can use this pattern every time that you are the owner of code, legacy code and the most important when you want to follow “Open Close Principle”.

  1. Great site you have got here.. It’s hard to find good quality writing like yours nowadays. I really appreciate people like you! Take care!!

  2. Definitely believe that which you stated. Your favorite reason appeared to be
    on the net the simplest thing to be aware of. I say to you, I definitely get irked while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  3. Hey very nice blog!! Guy .. Excellent .. Superb .
    . I’ll bookmark your web site and take the feeds additionally? I’m glad to
    find a lot of helpful info right here within the put up,
    we want develop more strategies on this regard,
    thanks for sharing. . . . . .

  4. Hello to all, it’s in fact a fastidious for me to go to see this website, it includes helpful Information.

  5. Howdy! This article couldn’t be written much better! Reading through this post reminds me of my previous roommate! He continually kept talking about this. I am going to send this post to him. Fairly certain he’s going to have a
    great read. I appreciate you for sharing!

  6. Remarkable! Its really amazing article, I have got much clear
    idea on the topic of from this paragraph.

  7. papa johns permalink

    Your style is unique in comparison to other folks I’ve read stuff from. Many thanks for posting when you have the opportunity, Guess I will just bookmark this blog.

  8. I do consider all of the concepts you’ve offered on your post. They’re really convincing and
    will definitely work. Nonetheless, the posts are very
    brief for newbies. May just you please lengthen them a
    little from next time? Thanks for the post.

    • Thank you,
      Sure but some posts must not be very long, because the reader will leave them.
      I will lengthen from next time.

Trackbacks & Pingbacks

  1. Open Close Principle | tech geek
  2. Greedy Boss – Adapter Pattern | IT World Web

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: