tags:

views:

375

answers:

6

I'm trying to help a coworker come to terms with OO, and I'm finding that for some cases, it's hard to find solid real-world examples for the concept of a tag (or marker) interface. (An interface that contains no methods; it is used as a tag or marker or label only). While it really shouldn't matter for the sakes of our discussions, we're using PHP as the platform behind the discussions (because it's a common language between us). I'm probably not the best person to teach OO since most of my background is highly theoretical and about 15 years old, but I'm what he's got.

In any case, the dearth of discussions I've found regarding tag interfaces leads me to believe it's not even being used enough to warrant discussion. Am I wrong here?

+8  A: 

Tag interfaces are used in Java (Serializable being the obvious example). C# and even Java seem to be moving away from this though in favor of attributes, which can accomplish the same thing but also do much more.

I still think there's a place for them in other languages that don't have the attribute concept that .NET and Java have.

ETA:

You would typically use this when you have an interface that implies an implementation, but you don't want the class that implements the interface to actually have to provide that implementation.

Some real world examples:

Serializable is a good example - it implies that there is an implementation (somewhere) that can serialize the object data, but since a generic implementation for that is available, there is no need to actually have the object implement that functionality itself.

Another example might be a web page caching system. Say you have a "Page" object and a "RequestHandler" object. The RequestHandler takes a request for a page, locates/creates the corresponding Page object, calls a Render() method on the Page object, and sends the results to the browser.

Now, say you wanted to implement caching for rendered pages. But the hitch is that some pages are dynamic, so they can't be cached. One way to implement this would be to have the cacheable Page objects implement an ICacheable "tag" interface (Or vice-versa, you could have an INotCacheable interface). The RequestHandler would then check if the Page implemented ICacheable, and if it did, it would cache the results after calling Render() and serve up those cached results on subsequent requests for that page.

Eric Petroelje
Could you cite a real world example?
Oorang
@Oorang - ok, provided some examples. Hopefully that will clarify things just a bit.
Eric Petroelje
+2  A: 

I'd call myself an OO programmer, and I've never heard of a tag interface.

Scott Langham
I concur and say ditto.
Daniel A. White
I just assumed he meant a marker interface.
R. Bemrose
ok, you're not familiar with the term but you have seen the idiom. I've heard them more often called "marker interfaces". An empty interface, no methods or properties. Serializable in Java is one. http://java.sun.com/javase/6/docs/api/java/io/Serializable.html In .NET the idiom is frowned-upon.
Cheeso
Yes, now I've looked it up, I know what it is. I've used Serializable in Java without knowing it was called a tag interface. I kind of think it's a bit of a trick though really. I can't think of any time I've wanted to do something like this in my code.
Scott Langham
A: 

I've used tag interfaces a couple of times in an object model representing a SQL database. In these instances, it's a subtype of the root interface for particular types of objects. It's easier to check for a tag interface than an attribute ('obj is IInterface' rather than using reflection)

thecoop
+1  A: 

I think tag interfaces are worth discussing, because they're an interesting corner case of the concept of an interface. Their infrequent use is also worth noting, though!

DDaviesBrackett
I agree, especially with the last statement. Frequent use of tag interfaces is probably an indication of a poorly designed object model.
Eric Petroelje
@Eric... I have to disagree with you. Tag interfaces can be very useful. Another way of getting compile time meta data without using attributes.
Matthew Whited
+7  A: 

In .Net Tag interfaces can be great for use with reflection and extension methods. Tag interfaces are typically interfaces without any methods. They allow you to see if an object is of a certain type without having the penatilty of reflecting over your objects.

An examples in the .Net Framework INamingContainer is part of ASP.Net

Matthew Whited
Sweet... drive by down vote. Looks like they are asking if tag interfaces of are of any use. And I guess an example of them being used is a bad thing.
Matthew Whited
lol @ "drive by down vote". I don't understand the downvote.
unforgiven3
Other reasons I have found Tag interfaces useful is faking multiple and static inheritance.
Matthew Whited
The downvote is because the advice given in this answer is in direct conflict with the MS-recommended design guidelines. Framework Design Guidelines say: DO NOT use empty interfaces; DO use attributes. It's not that I don't agree with the answer; it's that I think it is actually harmful.
Cheeso
And when was that guide writen. I would of considered agreeing with that point of view before .Net 3.5 came out. And if the generic filters are updated to match against attributes I may consider changing back. But at the same time it is les of a hit to see if an object is of a particular type than to enumerate all of the attributes for the one you want.
Matthew Whited
This answer http://stackoverflow.com/questions/1023068/what-is-the-purpose-of-a-marker-interface/1023144#1023144 lists some very good reasons to favor marker interfaces as opposed to attributes
John Rasch
+1  A: 
Cheeso
Attributes are useless for tacking on extension methods
Matthew Whited
Right - attributes are not interfaces, if that's what you mean.
Cheeso
Which is why tagging interfaces can be useful. (don't be a puppet 8o)
Matthew Whited
I don't get your comment or why you are calling me a puppet. Are you saying that tagging interfaces are useful because that usage goes against the recommended practice? Because it is surprising to developers? Because it is a creative way to do something that attributes do better?
Cheeso
I was saying it as a jab for down voting me. There is a reason those are guidelines. It's just like using a goto. Saying they work will get you down voted but the truth is they do work.
Matthew Whited
Also that book is from before .Net 3.5 (And beyond that my example as an interface from the framework. So they can't be too disgusted by them)
Matthew Whited