M G Vinod Kumar\’s Technology Blog

April 24, 2006

Marker Pattern aka Marker Interface Pattern

Filed under: .NET, Pattern — mgvinod @ 10:22 am

The Marker Pattern is popularly known as Marker Interface Pattern. I removed the Interface since this pattern can be implemented in two ways. Let us consider each of the ways.

1. Using an interface

First an interface is created as given below.

interface ITransactionAware{


Then, the class implements the interface.


class ClassWithMarkerInterface : ITransactionAware{


Using reflection, class can be checked to whether it implements the interface or not. Based on that result, any required action can be taken.


ClassWithMarkerInterface clsInterface = new ClassWithMarkerInterface();

if (clsInterface is ITransactionAware){

//start transaction



2. Using an attribute

Instead of an interface, an attribute is used.

class TransactionAttribute : System.Attribute{


The class is then marked with the above attribute.



class ClassWithAttributeMarker {


The code to check for attribute is little more verbose than using the interface.


ClassWithAttributeMarker clsAttribute = new ClassWithAttributeMarker();

if (clsAttribute.GetType().IsDefined(typeof(TransactionAttribute),false)){

//start transaction



As you can see, using an attribute is slower than the normal interface. Purists would argue that an attribute should be used, but if performance is your top criteria then interfaces are the way.





Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: