tags:

views:

186

answers:

5

In the application I am writing I have a Policy class. There are 4 different types of Policy. Each Policy is weighted against the other Policies such that PolicyA > PolicyB > PolicyC > PolicyD.

Who's responsibility is it to implement the logic to determine whether one Policy is greather than another? My initial thought is to overload the > and < operators and implement the logic in the Policy type itself.

Does that violate the SRP?

+5  A: 

I would think that a PolicyComparer class should do the evaluation.

marc
A: 

I think you are on the right track with the overload however the extension of this is obviously a lot longer

if (A > B || B > C || C > D) ...

Nick Berardi
A: 

You could also store a PolicyWeight attribute in your class, that being a simple built-in type ( int, unsigned int, ... ) which can then be easily compared.

QBziZ
A: 

Certianly a dedicated comparer class. If you ever need to provide additional logic (e.g. have two or three different ways of comparing policies), this approach allows you more flexibility (not achievable through operator overloads).

petr k.
A: 

You want a PolicyComparator class. If you want to override < and >, that's fine, but do that overriding in the Policy base class, and have those implementations utilize PolicyComparator to do it.

Kevin Conner