Click here to Skip to main content
Click here to Skip to main content
Technical Blog

The Adaptive Interface Pattern

, 22 Oct 2012 Ms-PL
Rate this:
Please Sign up or sign in to vote.
Many times you read about some design pattern in a book or online, and you realise 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 [...]

Many times you read about some design pattern in a book or online, and you realise 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 recognised 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:

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 were 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:

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…

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)

Share

About the Author

MattDavey
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionCool PinmemberYannAchard11-Nov-12 2:25 
QuestionNice but how do you use it ? PinmemberGeeko3718-Oct-12 6:12 
AnswerRe: Nice but how do you use it ? PinmemberMattDavey22-Oct-12 1:23 
QuestionI am sorry PinmemberQwertie16-Oct-12 6:58 
I meant to click on this web page and it looks like I accidentally filed a "report" against it. I moved my mouse down and clicked the first paragraph, but a menu appeared a split-second before that and then it tells me I've reported the article. Stupid UI doesn't offer any way to cancel, and it doesn't even say which of the 11 reports I made!

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 22 Oct 2012
Article Copyright 2012 by MattDavey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid