Adapter Pattern in Java

Adapter Pattern is a Structural Pattern in which, an interface of a class is converted into another interface that Client expects to work. With Adapter Pattern, we can reuse existing code without changing it.

I. OVERVIEW

Adapter Pattern defines an Adapter that can adapt a target class/interface (that we called Adaptee) to Client‘s requirement.

The Adapter contains an instance of Adaptee (also hides it from Client). It helps that, Client calls Adapter methods without knowing anything about Adaptee, then Adapter uses Adaptee instance inside to call its appropriate methods.

II. PRACTICE

1. Project Overview

We have Person ‘fullname’ information, but Client want to use ‘first name’ and ‘last name’ as Customer information. So we create a Person-to-Customer-Adapter that Client will work with.

2. Step by Step

2.1- Create Person class:


package com.javasampleapproach.adapter.pattern;

public class Person {
	private String fullName;

	public Person(String fullName) {
		this.fullName = fullName;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	@Override
	public String toString() {
		return "Person [fullName=" + fullName + "]";
	}
}	

2.2- Create ICustomer interface:


package com.javasampleapproach.adapter.pattern;

public interface ICustomer {

	public String getFirstName();
	public String getLastName();
}	

2.3- Create PersonToCustomerAdapter class that implements ICustomer interface:


package com.javasampleapproach.adapter.pattern;

public class PersonToCustomerAdapter implements ICustomer {

	private Person person;

	public PersonToCustomerAdapter(Person person) {
		this.person = person;
	}

	@Override
	public String getFirstName() {
		return this.person.getFullName().split(" ")[0];
	}

	@Override
	public String getLastName() {
		return this.person.getFullName().split(" ")[1];
	}

}	

2.4- Create Client Test Class:


package com.javasampleapproach.adapter;

import com.javasampleapproach.adapter.pattern.ICustomer;
import com.javasampleapproach.adapter.pattern.Person;
import com.javasampleapproach.adapter.pattern.PersonToCustomerAdapter;

public class MainApp {

	public static void main(String[] args) {

		Person person = new Person("Jack Smith");

		ICustomer adapter = new PersonToCustomerAdapter(person);

		System.out.println(person);
		System.out.println("--- Customer Information ---");
		System.out.println("First Name: " + adapter.getFirstName() + " | Last Name: " + adapter.getLastName());
	}
}	

2.5- Run the code, the console window shows:


Person [fullName=Jack Smith]
--- Customer Information ---
First Name: Jack | Last Name: Smith

III. SOURCE CODE

AdapterPattern

85 thoughts on “Adapter Pattern in Java”

  1. Simply want to say your article is as astounding. The clarity in your post is just great and
    i could assume you are an expert on this subject. Fine with your permission allow
    me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please carry on the enjoyable work.

  2. I’m not sure why but this web site is loading extremely slow for me.
    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.

  3. Hmm is anyone else having problems with the pictures on this blog loading?
    I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any feedback would be greatly appreciated.

  4. Good day I am so glad I found your web site,
    I really found you by error, while I was researching on Yahoo for something else, Regardless
    I am here now and would just like to say cheers for a
    incredible post and a all round exciting blog (I also love the theme/design), I don’t have time to go through it all at
    the moment but I have saved it and also included your RSS feeds, so when I
    have time I will be back to read a lot more, Please do keep up the fantastic work.

  5. I’d like to thank you for the efforts you have put in penning this site.
    I really hope to see the same high-grade content by you in the future as well.
    In fact, your creative writing abilities has inspired me to
    get my own blog now 😉

  6. It is actually a great and useful piece of information. I’m glad that
    you simply shared this helpful info with us.
    Please stay us informed like this. Thanks for sharing.

  7. naturally like your web-site however you have to test
    the spelling on several of your posts. Several of them are
    rife with spelling issues and I to find it very troublesome to
    tell the reality on the other hand I’ll certainly come back again.

  8. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed surfing around your blog
    posts. After all I will be subscribing to your feed and I hope you write again very soon!

  9. You’re so interesting! I don’t suppose I’ve truly read anything like that before.
    So good to discover somebody with original thoughts on this topic.
    Seriously.. many thanks for starting this up.
    This web site is something that is required on the web, someone with a little
    originality!

  10. This is very interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of your great post.
    Also, I’ve shared your web site in my social networks!

  11. Wow that was odd. I just wrote an incredibly long comment but
    after I clicked submit my comment didn’t appear. Grrrr…
    well I’m not writing all that over again.
    Anyways, just wanted to say excellent blog!

  12. Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic.
    I actually like what you have acquired here,
    certainly like what you are saying and the way in which you
    say it. You make it enjoyable and you still care for to
    keep it smart. I cant wait to read far more from you.
    This is actually a terrific web site.

  13. Hi, I do think this is an excellent site. I stumbledupon it 😉 I am going to revisit yet again since i have book marked it.

    Money and freedom is the best way to change, may
    you be rich and continue to help others.

  14. Its like you read my mind! You appear to understand a lot approximately this,
    like you wrote the e-book in it or something. I feel that you could do with
    some p.c. to pressure the message home a bit, however instead
    of that, this is wonderful blog. A fantastic read.

    I’ll certainly be back.

  15. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service?
    Cheers!

  16. Hi, i feel that i noticed you visited my blog thus i came to return the desire?.I’m attempting to in finding things to improve my
    website!I assume its ok to make use of a few of your ideas!!

  17. I’m not sure why but this site is loading extremely slow for me.
    Is anyone else having this issue or is it a problem on my end?

    I’ll check back later and see if the problem still exists.

  18. Wonderful blog! I found it while searching on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Cheers

  19. Hey very cool web site!! Man .. Beautiful .. Superb ..
    I’ll bookmark your website and take the feeds also?
    I am glad to search out so many helpful info here within the publish, we’d like work out extra strategies in this regard, thank you for sharing.

    . . . . .

  20. It is perfect time to make some plans for the future and it’s
    time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or advice.
    Maybe you could write next articles referring to this
    article. I want to read even more things about it!

  21. My spouse and I stumbled over here by a different page and thought I may
    as well check things out. I like what I see so now i’m following you.
    Look forward to checking out your web page yet again.

  22. Good day! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank
    for some targeted keywords but I’m not seeing very good gains.
    If you know of any please share. Appreciate it!

  23. Hello there, I discovered your website by means of Google whilst looking for a similar
    subject, your website got here up, it seems to be good.
    I have bookmarked it in my google bookmarks.

    Hi there, simply turned into aware of your weblog through
    Google, and found that it is really informative.

    I am gonna watch out for brussels. I will appreciate if you continue
    this in future. Numerous other folks can be benefited out of your writing.

    Cheers!

  24. I think that is among the so much vital information for me.

    And i am glad reading your article. But should
    observation on few common things, The web site taste
    is ideal, the articles is in reality great : D. Good job, cheers

  25. Please let me know if you’re looking for a writer for your site.
    You have some really good posts and I believe I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Kudos!

  26. Fascinating blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog jump out.
    Please let me know where you got your design. Appreciate it

  27. I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such
    detailed about my difficulty. You’re wonderful!
    Thanks!

  28. Woah! I’m really digging the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and visual appeal.

    I must say that you’ve done a awesome job with this.
    Also, the blog loads extremely fast for me on Opera. Superb Blog!

  29. Hi јust wanted to give you a quick heads up and let ʏoou know a few of the images aren’t loading correctly.
    I’m not sure wwhy but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the same results.

  30. I’m impressed, I must say. Rarely do I come across a blog that’s equally educative and interesting,
    and without a doubt, you’ve hit the nail on the head. The problem
    is something not enough people are speaking intelligently about.

    I’m very happy that I stumbled across this during my search for something concerning this.

  31. My family members every time say that I am wasting my time here at net, however I know I am
    getting familiarity everyday by reading such
    good articles or reviews.

  32. We’re a group of volunteers and starting a new scheme in our community.
    Your web site provided us with valuable information to work on. You’ve done an impressive job and our entire community will be thankful to you.

  33. Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about setting up my own but I’m not sure where
    to start. Do you have any ideas or suggestions?
    Cheers

  34. I like the valuable info you supply to your articles.
    I will bookmark your weblog and take a look at once more right here frequently.

    I’m relatively certain I will learn many new stuff right here!
    Good luck for the next!

  35. Hi there, You have done a fantastic job. I’ll definitely digg it and personally recommend to my friends.

    I’m sure they will be benefited from this website.

  36. Howdy! Do you know if they make any plugins to
    help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good gains. If you know of any please share.

    Thank you!

  37. Wonderful items from you, man. I have take note your stuff prior to and you are simply extremely great.
    I actually like what you’ve bought right here, really like what you’re saying and the way in which wherein you
    assert it. You are making it enjoyable and you continue to take care of to stay it
    smart. I can’t wait to read far more from you. This is actually a great website.

  38. hi!,I really like your writing so so much! share we keep
    up a correspondence more approximately your post on AOL?
    I require an expert on this house to unravel my problem.
    May be that’s you! Having a look forward to see you.

  39. Great post. I was checking continuously this blog and I am impressed!
    Extremely helpful info specially the last part :
    ) I care for such info much. I was looking for this particular info for a very
    long time. Thank you and best of luck.

  40. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some
    time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  41. Hey just wanted to give you a quick heads up. The text in your post
    seem to be running off the screen in Firefox. I’m not sure if this is a formatting issue or
    something to do with browser compatibility but I thought
    I’d post to let you know. The layout look great though!
    Hope you get the problem fixed soon. Thanks

  42. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three
    e-mails with the same comment. Is there any way you can remove people
    from that service? Thanks a lot!

  43. You really make it seem really easy with your presentation but I in finding this matter to be actually one thing which I think I might never understand.
    It sort of feels too complicated and extremely broad for me.
    I am having a look forward for your next publish, I will try to get the hold of it!

  44. It’s a shame you don’t have a donate button! I’d certainly donate
    to this fantastic blog! I guess for now i’ll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to fresh updates and
    will share this website with my Facebook group.
    Talk soon!

  45. Please let me know if you’re looking for a article author for your blog.
    You have some really great articles and I feel I would be a good asset.

    If you ever want to take some of the load off, I’d really like to write some content for your blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Many thanks!

  46. Good day! This is my first visit to your blog! We are a group
    of volunteers and starting a new project in a community in the same niche.
    Your blog provided us useful information to work on. You have done a marvellous job!

  47. Hey! I know this is kinda off topic but I was wondering which blog platform are you using for this
    site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m
    looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a
    good platform.

  48. Amazing blog! Is your theme custom made or did you
    download it from somewhere? A theme like yours with a few
    simple tweeks would really make my blog stand out. Please let me know where you
    got your design. Cheers

  49. Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you!
    By the way, how could we keep up a correspondence?

  50. You actually make it seem really easy along with your presentation but I to find this topic to be really
    one thing that I feel I’d never understand. It kind of feels too complicated and very wide for me.
    I’m looking forward on your next put up, I will try to
    get the hold of it!

  51. Link exchange is nothing else but it is simply placing the other person’s weblog link on your page
    at appropriate place and other person will also do same in favor of you.

  52. I’m really inspired with your writing skills and also with the layout for your weblog.
    Is that this a paid theme or did you modify it your self?

    Either way stay up the excellent quality writing, it’s uncommon to peer a great weblog like this
    one nowadays..

  53. Hello There. I found your weblog the use of msn. That is a really smartly written article.
    I’ll be sure to bookmark it and return to learn extra of your useful information. Thank you for the
    post. I will certainly comeback.

  54. When someone writes an piece of writing he/she
    retains the idea of a user in his/her brain that how a user can understand it.

    So that’s why this piece of writing is outstdanding. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *