In my CS110 class, we are developing an Android application using the Google Maps API and one of the concepts being taught the Observer design pattern. The design pattern teaches the concept of a publisher and a subscriber. So when the state of the publisher changes, it notifies all the subscribers. In this example, the LocationListener is a subscriber but the implementation is confusing.
A snippet of the code looks as follows:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
LocationListener is an interface, and my understanding is you can't instantiate an interface. Yet, the LocationListener locationListener...
statement is creating an object from an Interface
, how is this possible?
Also, my understanding is this is an Anonymous class. I'm not quite sure I understand what it means for a class to be Anonymous. I understand what the word anonymous means, but not in the context of a class.
Any help in clarifying what is happening here would be greatly appreciated.