Validator Object

What you really want is to isolate the validation operation and be able to subclass that without changing anything else.

You've heard of comparators: external objects that can be passed into sorted structures to change their behavior.

Why not create a validator object? Make your PMQuadtree contain a Validator object as a member, and make your PMQuadtree constructors require that you pass in one of these Validator objects. Make it an interface. Now create three implementations: PM1Validator and PM3Validator. The code for the validator's valid() method is only about 10 lines long. You've just magically done what I've done (because I told you how) and now you have a PMQuadtree that becomes one of three orders with only one method changing (and very little code). The best thing to do is create three subclasses, PM1Quad and PM3Quad, who contain private static inner classes which implement the validator and hard code the PM1's default constructor (the one that takes zero parameters) to pass its private validator to the super-class constructor (via a call to super(...) as the first line of the constructor), and make PMQuadtree an abstract class. Note that you can still provide constructors for abstract classes (for the purpose of their subclasses calling them).

This is technically an instance of the Decorator design pattern. Read more about it if you'd like. Design patterns actually do rock. Take CMSC433.

MM Hugue 2017-10-12