Monday, November 22, 2010

Design Patterns in the JDK.

I saw an article (well more of a rant) the other day, by Rob Williams
Brain Drain in enterprise Dev. I have to say, I do agree with some of what he is saying. I know from my personal experience, I had spent a good 2 or so years just wallowing in the enterprise development world, not learning anything and actually losing my skills I developed before. The corporate confront zone is not conducive to eager technologists.

In this article he also stated:
"1 in 10 cant even pass a simple test like ‘which design pattern is used in the streams library that makes BufferedFileReader interchangeable with a FileReader?'"

I also tested it at work and I only had 1 out of the 8 people asked that got it right

Without much confidence, I had guessed Decorator based on "interchangeable". I then decided that was actually some worth sneaking into future interviews, and probably a good time to revise a little.

So I went scouring the internet to find all I could on the topic and I didn't actually find as much as I thought I would. Most of it came from BalusC at Stackoverflow, the rest was very scattered between blog posts, java ranch, some old pdf's and articles I had. I didn't take every single example of every single pattern I found, but rather the common ones.
This may be a good way for people to learn about patterns, quite often they are using them everyday without realizing.

This is used to convert the programming interface/class into that of another.


This decouples an abstraction from the implementation of its abstract operations, so that the abstraction and its implementation can vary independently.

-AWT (It provides an abstraction layer which maps onto the native OS the windowing support.)

Lets clients treat individual objects and compositions of objects uniformly. So in other words methods on a type accepting the same type.


Attach additional responsibilities to an object dynamically and therefore it is also an alternative to subclassing. Can be seen when creating a type passes in the same type. This is actually used all over the JDK, the more you look the more you find, so the list below is definitely not complete.

To provide a simplified interface to a group of components, interfaces, abstractions or subsystems.


Caching to support large numbers of smaller objects efficiently. I stumbled apon this a couple months back.


The Proxy pattern is used to represent with a simpler object an object that is complex or time consuming to create.


Abstract factory:
To provide a contract for creating families of related or dependent objects without having to specify their concrete classes. It enables one to decouple an application from the concrete implementation of an entire framework one is using. This is also found all over the JDK and a lot of frameworks like Spring. They are simple to spot, any method that is used to create an object but still returns a interface or abstract class.


Used simplify complex object creation by defining a class whose purpose is to build instances of another class. The builder pattern also allows for the implementation of a Fluent Interface.


Factory method:
Simply a method that returns an actual type.


Allows for classes whose instances can create duplicates of themselves. This can be used when creating an instance of a class is very time-consuming or complex in some way, rather than creating new instances, you can make copies of the original instance and modify it.


This tries to ensure that there is only a single instance of a class. I didn't find an example but another solution would be to use an Enum as Joshua Bloch suggests in Effective Java.



Chain of responsibility:
Allows for the decoupling between objects by passing a request from one object to the next in a chain until the request is recognized. The objects in the chain are different implementations of the same interface or abstract class.


To wrap a command in an object so that it can be stored, passed into methods, and returned like any other object.


This pattern generally describes defining a grammar for that language and using that grammar to interpret statements in that format.


To provide a consistent way to sequentially access items in a collection that is independent of and separate from the underlying collection.


Used to reduce the number of direct dependencies between classes by introducing a single object that manages message distribution.


This is a snapshot of an object’s state, so that the object can return to its original state without having to reveal it's content. Date does this by actually having a long value internally.


Null Object:
This can be used encapsulate the absence of an object by providing an alternative 'do nothing' behavior. It allows you to abstract the handling of null objects.


Used to provide a way for a component to flexibly broadcast messages to interested receivers.


This allows you easily change an object’s behavior at runtime based on internal state.


Is intended to provide a means to define a family of algorithms, encapsulate each one as an object. These can then be flexibly passed in to change the functionality.


Template method:
Allows subclasses to override parts of the method without rewriting it, also allows you to control which operations subclasses are required to override.


To provide a maintainable, easy way to perform actions for a family of classes. Visitor centralizes the behaviors and allows them to be modified or extended without changing the classes they operate on.

-javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor
-javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor


  1. cool idea! here's a small contribution to the list - one of my favorite patterns is Null Object, and a great example can be found in java.util.Collections. emptyList() / emptyMap() / emptySet().

  2. Thanks Pavel, I have included your contribution, everyone seems to forget about null object... including myself.

  3. Hi Bozhidar, If you read my post I said:
    "Most of it came from BalusC at Stackoverflow," I also tried to include some common ones that I found were not included..

  4. Perhaps most of their confusion is over there's no such thing as a BufferedFileReader. And if there were, the precise pattern for interchangeability would be dependent on it's precise makeup (ie it could be one of many...) and a matter of much quibbling.

    What a twit, it's people like that you DONT want to work for. These types are the equivalents of the religious debates over how many angels can fit on the head of a pin. Design patterns are standard ways to solve problems which depending on the precise nature of the problem differ greatly in details. They aid in discussion of solutions for problems as you quickly see the broad thrust of the proposed solution. A "test" like the one above is by and for idiots.

  5. Haha.. I didn't actually notice the BufferedFileReader thing, thankfully I didn't include it in my post. :)

    I do think may be taking it the wrong way though, I purely meant this article as revision for myself and to quote myself:
    "This may be a good way for people to learn about patterns, quite often they are using them everyday without realizing."

  6. Brian, sorry, didn't see this point :)

  7. I read this blog and link was really useful to me Java learning. We share this Java topics is any knowledge improve to me Java Programming. How to Java Process using for the Python Automation?
    SEO Training Course in Chennai

  8. Very important article for JAVA. I found this article some best resources of JAVA. For more details here this site

  9. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.

    software testing course in chennai

  10. Excellent informative blog, Thanks for sharing.
    Web Design Training

  11. Nice Blog, Thank you so much sharing with us. Visit for
    Web Designing Company in Delhi

  12. We are happy now to see this post because of the you put good images, good choice of the words. You choose best topic and good information provide. Thanks a sharing nice article.

    Web Designing Company in Delhi

  13. Wow! That's really great information guys.I know lot of new things here. Really great contribution.Thank you ...
    datapower interview questions

  14. It was so bravely and depth information. I feel so good read to your blog. I would you like thanks for your posting such a wonderful blog!!!
    SEO Course in Velachery
    SEO Training in Chennai Velachery
    SEO Course in Tnagar
    SEO Training in Omr
    SEO Course in Navalur
    SEO Course in Omr


  16. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that course in chennai

  17. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    industrial safety course in chennai

  18. Thanks for the great information , i was looking for this information from long.Great blog
    tally course in hyderabad

  19. Our line by line lyrics generator is great for writing and fine-tuning songs and raps, especially when you want them to rhyme. Unlike our other song lyrics.

  20. Nice information.. Thanks for sharing this blog. see my website also
    .. VIEW MORE:- Freelance Seo Expert in Delhi


  21. Nice information.. Thanks for sharing this blog. see my website also
    .. VIEW MORE:- Website Design Company in Delhi

  22. Ah,so beautiful and wonderful post!An opportunity to read a fantastic and imaginary blogs.It gives me lots of pleasure and interest.Thanks for sharing.
    Find the Interior Designers in Madhurawada

  23. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! architecture


  24. تيجان
    عزيزي العميل تهتم تيجان المدينة المنورة بكل ما يهمك لذا توفر لك خدمات مذهله عن العزل و نقل عفش و تسليك المجاري و خدمات تنظيف شاملة و مكافحة كافة أنواع الحشرات الزاحفة و الطائرة و شتى أعمال الفنية مثل الصيانة و السباكة و الدهانات و أعمال الكهرباء و أيضاً نقدم خدمات أخرى خاصة بمحبي تنسيق الحدائق و أعمال الجبس بورد بالمدينة المنورة و المفاجئة الكبرى في الأسعار حيث نقدم لكم أقوى العروض و التخفيضات و الخصومات المذهلة بمناسبة فصل الصيف لننال رضا العميل و تتم كل تلك الخدمات من خلال أفضل طاقم عمل على الإطلاق من مهندسين تواصل معنا الآن.


  25. شركة نقل عفش بالمدينة المنورة

    تعتبر شركة نقل عفش بالمدينة المنورة واحدة من أهم الشركات التي تعمل في مجال نقل العفش منذ أكثر من خمسة عشر عام، حيث أنها من الشركات الكبرى التي تعتمد في أداء عملها على أكفأ العمال والمهندسين والفنين فكل من عمال الشركة له نخصص خاص به يتوجب عليه القيام بها، فمثلاً تمتلك الشركة مجموعة من المهندسين والفنين المتخصصين في فك وتركيب الأجهزة الكهربائية والمكيفات.
    وأيضاً لدى شركة نقل عفش بالمدينة المنورة نجارين محترفين للغاية في فك وتركيب جميع أنواع الأثاث ودواليب المطبخ وغيرها، كما أننا نمتلك الإمكانيات التي تساهم في نقل عفش بالمدينة المدينة المنورة دون أن يتعرض للكسر أو الخدش والتجريح.

  26. This is an awesome post. Really very informative and creative contents.
    ios app Devlopment company in chennai

  27. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    top microservices online training

  28. Great blog thanks for sharing Masters of building brands, Adhuntt Media is making waves in the Chennai digital market! Known for their out of their box ideas, if a creative overhaul for your brand is what you need, you’ve come to the right place. Right from Search Engine Optimization-SEO to Social Media Marketing, Adhuntt Media is your pitstop for original brand identity creation from scratch.
    digital marketing company in chennai

  29. Great blog thanks for sharing Instagram and Facebook have provided an amazing place for new brands to grow and flourish. We can find the perfect niche for your brand on the best social media platforms. Marketing through social media brings forth global audience without all these physical boundaries. Analyze and take over the competition with ease with Adhuntt Media’s digital marketing tools and strategies.
    digital marketing company in chennai

  30. Nice blog thanks for sharing Choosing the right place to buy your first plant isn’t that hard of a choice anymore. Presenting the best plant nursery in Chennai - Karuna Nursery Gardens is proud to showcase more than 3000+ plants ready to be chosen from.
    plant nursery in chennai

  31. Excellent blog thanks for sharing Run your salon business successfully by tying up with the best beauty shop in Chennai - The Pixies Beauty Shop. With tons of prestigious brands to choose from, and amazing offers we’ll have you amazed.
    Cosmetics Shop in Chennai

  32. Bharat CSP Agents are those individuals who acts as an agent of the bank at places where it is not possible to open branch of the bank.

    CSP apply
    CSP online application
    apply for CSP
    Apply Online For Bank CSP

  33. We have worked with many businesses in New Zealand and abroad and we have found that although there has been massive growth in technology, most small to medium sized business owners have been left behind.

    Website Revamp Company in New Zealand
    Logo Designing Company in New Zealand
    Logo Designing Services in New Zealand
    Graphic Designing Company in New Zealand
    Website Design Company in New Zealand
    Website Maintenance Company in New Zealand

  34. Oxigen BC Private Limited Company is India's Largest CSP Provider, which works in all the states of India to open customer service point of all banks. Such as - sbi, boi, bob, pnb etc.

    CSP Apply
    CSP Online Application
    Online CSP Apply
    CSP Registration
    CSP Online Application
    CSP Provider

  35. A large number of people, particularly the migrant laborers and factory workers do not have a saving account and even not able to open an account due to lack of valid address and ID proof. As a result they face difficulties to save their earnings in a safe place and look out for solution to send money to their families.

    CSP Apply
    CSP Online Application
    Apply for CSP
    Top CSP Provider in India

  36. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.
    ExcelR Data Science course in mumbai
    data science interview questions

    We are a website designing company in Delhi help you in creating responsive websites with expert team of website designers and developers with strict timelines and affordable prices in Delhi. We have delivered more then 450 ++ Projects in India. Our team help companies, startups, individuals to design and redesigning responsive websites in India, Delhi. We have a in house team of expert web design and developers based in India, Delhi.

  38. Spot on with this write-up, I actually believe that this web site needs much more attention. I’ll probably be returning to see more, thanks for the info!
    Tech PC

  39. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

  40. I have read your blog its very attractive and impressive. Very systematic indeed! Excellent work!
    Data Science Course Training in Bangalore


  41. An outstanding share! I've just forwarded this onto a friend who has been conducting a little research on this. And he in fact bought me dinner due to the fact that I found it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanks for spending show time to talk about this issue here on your site.

  42. Very useful blog thanks for sharing IndPac India the German technology Packaging and sealing machines in India is the leading manufacturer and exporter of Packing Machines in India.

  43. Very useful blog thanks for sharing IndPac India the German technology Packaging and sealing machines in India is the leading manufacturer and exporter of Packing Machines in India.

  44. One of the other significant components is to get into a concurrence with a host that offers broad scope of showcasing options and has absolute charge over feel, functionality, architecture and stream.small business IT support services

  45. It’s interesting content and Great work....Most of the part want to analyze their individual scores in the exam. In this process of checking your Exam Latest Result, We support you by giving the Result links to get you All India Sarkari Result in an easy way.

  46. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions
    Data Science Course

  47. keep up the good work. this is an Ossam post. This is to helpful, i have read here all post. i am impressed. thank you. this is our data analytics courses mumbai
    data analytics course mumbai |

  48. What a great article!. I am bookmarking it to read it over again after work. It seems like a very interesting topic to write about.
    Best Data Science training in Mumbai

    Data Science training in Mumbai

  49. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    data analytics course in Bangalore


Popular Posts