Noor
Noor

Reputation: 20178

Design pattern for flexible interface

Suppose I have an interface API and classes FacebookAPI and FlickrAPI that implements this interface,

public interface API {
    Photo getPhoto(int id);
    Album getAlbum(int id);
}

package api;

import domainObjects.Album;
import domainObjects.Photo;

public class FacebookAPI implements API{

    @Override
    public Photo getPhoto(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Album getAlbum(int id) {
        // TODO Auto-generated method stub
        return null;
    }

}


import domainObjects.Album;
import domainObjects.Photo;

public class FlickrAPI implements API{

    @Override
    public Photo getPhoto(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Album getAlbum(int id) {
        // TODO Auto-generated method stub
        return null;
    }

}

The issue is that I only know that at minimum both APIs(facebook and flickr) requires the photoId. Now suppose that to get a photo FacebookAPI requires AccessToken in addition to Id while FlickAPI requires APIKey + UserId in addition to photoId.

What Design Pattern can i use to solve this issue?

Upvotes: 2

Views: 135

Answers (3)

guido
guido

Reputation: 19224

Create a Credentials abstract class to be extended by concrete APi implementation and get that in method contracts.

public Album getAlbum(int id, Credentials c) {

and similarily

public FlickrCredentials extends Credentials {
     String APIKey
     String UserId
}

That is only feasible if the authentication method is similar with changing parameters (like URL parameters). The abstract class should specify the contract of the method actually using the values, something like:

public String buildToken();

that could be implemented for instance as:

@Override
public String buildToken() {
     return "APIKey="+getAPIKey()+"&UserId="+getUserId();
}

Upvotes: 4

g45rg34d
g45rg34d

Reputation: 9660

Not sure which language you're using (objective c?) but if done it in C# then you'd want to use generics:

public interface API<TIdentifier> {
    Photo getPhoto(TIdentifier id);
    Album getAlbum(TIdentifier id);
}

Then your classes would look like this:

public class FlickrAPI implements API<FlickrIdentifier>
{
    @Override
    public Photo getPhoto(FlickrIdentifier id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Album getAlbum(FlickrIdentifier id) {
        // TODO Auto-generated method stub
        return null;
    }

}

Then you'd also need the FlickrIdentifier class:

public class FlickrIdentifier
{
    public string ApiKey { get; set; }
    public string UserId { get; set; }
}

Upvotes: 1

smk
smk

Reputation: 5932

Why cant you do something like this

public class FlickrAPI implements API{

  private String key;
  private UserId id;
  public FlickrAPI(String key, UserId id){
    this.key = key;
    this.id = id;
   //rest of initialzation
  }

}

Similarly for the FacebookAPI class

Upvotes: 0

Related Questions