C# Coding Solutions—Dealing with Marker Interfaces or Base Classes

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# Coding Solutions

© 2006 Christian Gross

Dealing with Marker Interfaces or Base Classes

When implementing the Command pattern or Chain of Responsibility pattern the objective is to execute a set of interface instances and make them do something useful. "Do something useful" is extremely vague, but that is what the patterns expect.

The idea behind the Command pattern is to be able to string together a set of interface implementations and have them execute a series of commands. The vagueness is the result of having to string together commands that may or may not be related, and thus you are looking for the lowest common denominator. For example, as the user is using an application, a record of each command executed would be added to a list. Then if the user wanted to undo or redo some actions, he would have only to inspect the list of recorded Command interfaces for the interfaces to be replayed or undone.

In the case of the Chain of Command pattern implementation, the idea is to present an object instance to a set of interface instances and expect the interface instances to process the data. For example, imagine writing a translation program and wishing to have some text translated. Using the Chain of Command pattern would find the appropriate translation.

These pattern implementations are very effective in dealing with vague intentions and converting them into specific actions. The following two interfaces are a classical definition of the Command pattern interfaces:

public interface ICommand {
    void Execute();
}
public interface ICommandVariation {
    void Execute();
    void Undo();
}

And the following interface and delegate definition could be considered the classical definition of the Chain of Responsibility interfaces:

public interface ICommand<contexttype> {
    bool Execute(contexttype context);
}
 
public delegate bool Handler<contexttype>(contexttype context);

In all three example definitions there is a common theme: the definition of a minimalist or marker interface, which defines only what is required. Under no circumstances can an implementation of the marker interface function on its own.


Previous_Page_.gif Next_Page_.gif


Personal tools