Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design Pattern Involved in Comparator

 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Recently i encountered one question about design patterns.
The question is can you think of any design pattern which motivated use of comparator.
Well i thought for a while then i replied "Decorator Pattern".

My logic behind this answer is that "Decorator Pattern" is used to decorate a single object in different ways by using different decorator still maintaining the object functionality the same.

When we use Comparator we don's change the object but we just change the way they get used for e.g. In Sort i can pass different comparator and can have the method or API show different result but the functionality is same its still sorting just using a different way.

Now the big question is AM I CORRECT?

Please shed some light on your thoughts on my answer.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the interpretation of software design patterns and how they are used is very subjective and rarely measured with "correct" "incorrect" labels.

A Comparator contains actual code that is used to sort objects. It is not adding any functionality or behaviors to the objects being sorted.

A Decorator enables you to add functionality to an object without having to change the object's source code.

The Decorator design pattern is not related to the purpose of a Comparator.
[ August 20, 2008: Message edited by: James Clark ]
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think comparator is more of related to Strategy pattern.
Strategy: allows one of a family of algorithms to be selected on-the-fly at runtime.


So here we are selecting the equality algorithm based on the given comparator.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Decorator has the same interface as the decorated object, and delegates most of the method calls to the decorated object, while adding behavior to some of them.
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to be closer to both visitor or strategy than decorator, but I wouldn't call it a great example of either.....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Nielsen:
Seems to be closer to both visitor or strategy than decorator, but I wouldn't call it a great example of either.....


Why wouldn't it be a perfect example for a Strategy?
 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for your input.
 
Billy Tsai
Ranch Hand
Posts: 1304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hereby confirm it is the Strategy pattern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic