Converting an IP address into some useful location information can be useful if you want to find out where sites are hosted or customise content to users depending on their location.
All this code is freely available over at github.
There are several ways to do this, all of which have their advantages and disadvantages, but sticking with one can cause rewriting a lot of code in the future. So rather than pick one and stick with it I decided to use dependency injection to allow different classes to be used that convert IP addresses to locations in different ways. The first task is to create an abstract class that will be used to construct the rest of the IP location classes. Each class that extends this abstract class will contain a method called getIpLocation() that will convert an IP address into a location, and a method that will update the data source for the location lookup. Rather than lump all of the classes into a single directory I have created a directory called Service, into which all of the different classes that lookup IP addresses will be kept.