Click here to Skip to main content
15,889,777 members
Articles / Programming Languages / C#

The Adaptive Interface Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Oct 2012Ms-PL1 min read 10.8K   1   4
The Adaptive Interface pattern

Many times you read about some design pattern in a book or online, and you realize that it’s a technique you’ve been using for years – you just didn’t have a name for it. It can be a bit of a double edged sword; on the one hand it gives you a nice warm fuzzy feeling inside to know that you independently and quite naturally adopted a recognized best practice, but on the other hand you’re disappointed to learn that you haven’t actually innovated or invented something new and exciting.

What I refer to as the "Adaptive Interface Pattern" is I’m sure one of these cases. In this technique, you define a separate interface defining all of the available operations which are available for a given state of an object. In some ways, it’s very similar to a state machine, but where the state transition changes the publicly exposed interface of an intersecting set of operations.

The most simple example of this would be a boolean switch. A traditional implementation might look something like this:

C#
class BooleanSwitch
{
    private bool _isSwitchedOn = false;

    public void SwitchOn()
    {
        if (this._isSwitchedOn)
            throw new InvalidOperationException("Switch is already switched on");

        this._isSwitchedOn = true;
    }
 
   public void SwitchOff()
    {
        if (this._isSwitchedOn == false)
            throw new InvalidOperationException("Switch has not been switched on");

        this._isSwitchedOn = false;
    }
}

Clearly having both SwitchOn() and SwitchOff() methods available at the same time can easily lead to runtime exceptions. It would be better if the SwitchOn() method was only available when the object was switched off, and the SwitchOff() method only available when the object was switched on.

We can accomplish this by defining an interface for each of these states:

C#
interface IOffSwitch
{
    IOnSwitch SwitchOn();
}

interface IOnSwitch
{
    IOffSwitch SwitchOff();
}

Our BooleanSwitch class can now be modified to implement both of these interfaces. And since calling the wrong method will now result in a compile time error, we can do away with the invalid operation exceptions…

C#
class BooleanSwitch : IOnSwitch, IOffSwitch
{
    private bool _isSwitchedOn = false;

    public IOnSwitch SwitchOn()
    {
        this._isSwitchedOn = true;
        return this;
    }

    public IOffSwitch SwitchOff()
    {
        this._isSwitchedOn = false;
        return this;
    }

    //Finally, we need to suppress the default constructor 
    //and write a factory method to create the object in its default state...
    static public IOffSwitch Create()
    {
        return new BooleanSwitch();
    }
}

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Comments and Discussions

 
QuestionCool Pin
YannAchard11-Nov-12 1:25
YannAchard11-Nov-12 1:25 
I've been thinking about this approach for a while, I really like how it forces the user of a class to follow a well-defined model.
It's both a nice user guide and an compile-time check.

However I've never really used it because I always get the feeling that it'll get too complex too quickly: for any non-trivial case (and don't get me wrong, I think it's still very valuable for the trivial cases) I'd expect the number of interfaces will grow too fast and their interconnections (the SM graph) too complex.
I don't really see that as a limitation of the AIP technique itself, but rather of the language or the tool-set: If we could visualize it as a graph then it would make things a lot more manageable both for the user and the creator.

Have you encountered such scalability problems yourself?
Do you have tips on how to keep it organised?

Thanks
Yann

PS: I had a look on your blog at the Factory example that's quite neat Smile | :)
QuestionNice but how do you use it ? Pin
Nateeko3718-Oct-12 5:12
Nateeko3718-Oct-12 5:12 
AnswerRe: Nice but how do you use it ? Pin
User 672451322-Oct-12 0:23
User 672451322-Oct-12 0:23 
QuestionI am sorry Pin
Qwertie16-Oct-12 5:58
Qwertie16-Oct-12 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.