Click here to Skip to main content
13,559,868 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 9 Oct 2012
Licenced Ms-PL

The Adaptive Interface Pattern

, 22 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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:

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:

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();


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


About the Author

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

You may also be interested in...

Comments and Discussions

QuestionCool Pin
YannAchard11-Nov-12 1:25
memberYannAchard11-Nov-12 1:25 
QuestionNice but how do you use it ? Pin
Geeko3718-Oct-12 5:12
memberGeeko3718-Oct-12 5:12 
AnswerRe: Nice but how do you use it ? Pin
MattDavey22-Oct-12 0:23
memberMattDavey22-Oct-12 0:23 
QuestionI am sorry Pin
Qwertie16-Oct-12 5:58
memberQwertie16-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180527.3 | Last Updated 22 Oct 2012
Article Copyright 2012 by MattDavey
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid