I have a simple design question. I have a tree based C++ class. It needs to use a distance function in order to calculate nearest neighbors. But the distance function is available from an external library such as GeographicLib or others. Ideally you want to vary the distance function based on client preferences. If they have Cartesian coordinates they can use a Euclidean distance. They can also provide a user based distance function.
I want to be able to pass to the tree class a function pointer that represents the distance function as this distance function will be used in several places in the tree class and needs to have instance scope. Right now I have hard coded a single type of distance function in the code and hence the class is not very reusable.
So as an example( I do not have access to sophisticated modeling tools) Some psuedo code
Include External Distance Library ABCTree() Distance = ExternalDistanceLibrary.calculateDistance(point1, point2)
As you can see I have hard coded the type in the source code(as well as in the header file). I do not wish to do this.
What I would like to do is to do something like this. I would like the client to pass in a function pointer (have the client to the tree class define the implementation of the function pointer)
//Constructor ABCTree(pointer_to_func distance_func) Distance = pointer_to_func(point1,point2);
It has been a while since I used design patterns. Would any design pattern be applicable here ? I have been thinking about the Adapter and Strategy pattern but I am not sure whether that is applicable here or there is any other one. Any guidance is appreciated.