• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design Pattern Involved in Comparator

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your input.
 
Ranch Hand
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hereby confirm it is the Strategy pattern
 
reply
    Bookmark Topic Watch Topic
  • New Topic