Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my project I am creating an abstraction layer for some APIs. The purpose of the layer is to make multi-platform easier, and also to simplify the APIs to the feature set that I need while also providing some functionality, the implementation of which will be unique to each platform.

At the moment, I have implemented it by defining and abstract class, which has methods which creates objects that implement interfaces. The abstract class and these interfaces define the capabilities of my abstraction layer.

The implementation of these in my layer should of course be arbitrary from the POV view of my application, but I have done it, for my first API, by creating chains of subclasses which add more specific functionality as the features of the APIs they expose become less generic.

An example would probably demonstrate this better:

//The interface as seen by the application

interface IGenericResource
{
    byte[] GetSomeData();
}

interface ISpecificResourceOne : IGenericResource
{
    int SomePropertyOfResourceOne {get;}
}

interface ISpecificResourceTwo : IGenericResource
{
    string SomePropertyOfResourceTwo {get;}
}

public abstract class MyLayer
{
    ISpecificResourceOne    CreateResourceOne();
    ISpecificResourceTwo    CreateResourceTwo();

    void UseResourceOne(ISpecificResourceOne one);
    void UseResourceTwo(ISpecificResourceTwo two);
}

//The layer as created in my library

public class LowLevelResource : IGenericResource
{
    byte[] GetSomeData()
    {}
}

public class ResourceOne : LowLevelResource, ISpecificResourceOne
{
    int SomePropertyOfResourceOne {get{}}
}

public class ResourceTwo : ResourceOne, ISpecificResourceTwo
{
    string SomePropertyOfResourceTwo {get {}}
}

public partial class Implementation : MyLayer
{
    override UseResourceOne(ISpecificResourceOne one)
    {
        DoStuff((ResourceOne)one);
    }
}

As can be seen, I am essentially trying to have two inheritance chains on the same object, but of course I can't do this so I simulate the second version with interfaces.

The thing is though, I don't like using interfaces for this; it seems wrong, in my mind an interface defines a contract, any class that implements that interface should be able to be used where that interface is used but here that is clearly not the case because the interfaces are being used to allow an object from the layer to masquerade as something else, without the application needing to have access to its definition.

What technique would allow me to define a comprehensive, intuitive collection of objects for an abstraction layer, while their implementation remains independent?

(Language is C#)

share|improve this question
    
Have you checked the applicability of bridge design pattern to your case? –  Fil Jun 12 at 10:42
add comment

1 Answer

Without knowing more I would focus on... Not doing that. Chains of subclasses is very rarely the answer, and I would submit never the answer for API implementation of the interface.

The problem is that the API isn't implementing the interface, it's already done. You're creating a least common interface from the different libraries. The focus then should be (if I understand the problem correctly) making your own API, with your own implementation agnostic data types and an adapter layer to the platform implementations (not an abstraction layer from the implementations).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.