This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design Pattern Involved in Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design Pattern Involved in Comparator" Watch "Design Pattern Involved in Comparator" New topic
Author

Design Pattern Involved in Comparator

Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
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.


Thanks,
Prabhat
SCJP 1.5, SCWCD 1.5, SCBCD 1.5
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Nov 07, 2000
Posts: 242
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Aug 13, 2007
Posts: 58
Hi All,

Thanks for your input.
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
I hereby confirm it is the Strategy pattern


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Design Pattern Involved in Comparator
 
Similar Threads
here we go again - design patterns from hell
Design Patterns...
This is going to be long... But appreciate all help i can get...
Confusion in Chain of responsibility and Decorator Pattern
Mark Cade's Study Guide p. 87 Q3: Pattern for Additional Responsibility