Reputation: 2294
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?
Upvotes: 2
Views: 345
Reputation: 8734
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.
Upvotes: 0
Reputation: 8100
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).
Upvotes: 0
Reputation: 3260
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.
Upvotes: 0
Reputation: 54864
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) ...
Upvotes: 0