|
IDoNothing
Last post 02-05-2013 6:18 PM by jasmine2501. 26 replies.
-
-
-
-
ObiWayneKenobi


- Joined on 03-23-2007
- Posts 323
|
There are valid reasons to use something like this; it's a common software idiom called "Marker Interfaces". You have an empty interface that exists so you can test if a derived class should have a certain behavior (because C# doesn't support multiple inheritance). For instance, let's say I have some domain entity, and I need to determine if it should have update timestamps applied to it. I can create an interface called say ITrackable that doesn't do anything itself, implement it in my class, and then check later if my class implements it. Given the above example I'd guess it's something like that. There's probably some conditional logic that checks if x is IBaseInput or IBaseOutput and does something if it is. May or may not be a WTF depending on usage.
The Daily WTF Forums. You will never find a more wretched hive of scum and villainy.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,197
|
lscharen:Marker interfaces?
Google?
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
-
TGV


- Joined on 10-09-2005
- Posts 563
|
Actually, that does not make a lot of sense to me. If you want to check if a class supports certain behavior, check the name of the bloody class that actually defines that behavior! And checking that kind of stuff by naming is, well, asking for trouble. "Look, guv', the log files are growin'. And every bleedin' line says sumfin' bout an eye track ball."
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,197
|
toon:What's PJH supposed to Google, pray?
Nothing. I was expecting Ischaren to Google "Marker interfaces."
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
Severity One


- Joined on 12-04-2008
- Malta
- Posts 362
|
TGV:Actually, that does not make a lot of sense to me. If you want to check if a class supports certain behavior, check the name of the bloody class that actually defines that behavior! And checking that kind of stuff by naming is, well, asking for trouble.
I'm not sure whether I don't understand the point you're trying to make, or whether you don't understand marker interfaces.
|
|
-
-
toon


- Joined on 01-07-2012
- Posts 281
|
PJH: toon:What's PJH supposed to Google, pray?
Nothing. I was expecting Ischaren to Google "Marker interfaces."
Sorry; it seems I'm not the brightest knife in the chandelier drawer today.
My point was meant to be, that I don't understand why you'd have Ischaren google something he/she obviously already knows about.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,197
|
toon:My point was meant to be, that I don't understand why you'd have Ischaren google something he/she obviously already knows about.
It wasn't obvious. Indeed, the opposite appears to be the case.
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
-
emurphy


- Joined on 01-14-2005
- Granada Hills, CA
- Posts 568
|
Even so, they should contain some comments along the lines of "deliberately empty because <reasons>".
|
|
-
-
GMMan


- Joined on 08-23-2012
- Posts 32
|
TGV:Actually, that does not make a lot of sense to me. If you want to check if a class supports certain behavior, check the name of the bloody class that actually defines that behavior! And checking that kind of stuff by naming is, well, asking for trouble. "Look, guv', the log files are growin'. And every bleedin' line says sumfin' bout an eye track ball."
Are you suggesting I should object.GetType().Name.Contains("PartOfClassName")? Good luck with that.
|
|
-
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 1,180
|
I'm actually using marker interfaces in some code of mine. It looks kind of like
public interface IAutoFrob {
}
public interface IAutoFrobInASpecificWay : IAutoFrob {
int FrobRate { get; set; }
FrobPreperationStatus GetFrobPreperationStatus(int foo, string bar);
}
The first class says the class can do a certain procedure automatically, the second says it will use a lot of features from the automatic system but it has a property and method to control it more fine-grained.
[Sanity Not Available until further notice. The trolls have won.]
|
|
-
-
mihi


- Joined on 05-10-2008
- Posts 94
|
This is C#, not Java 1.1. Therefore you should use Attributes instead of Marker interfaces :)
|
|
-
-
chooks


- Joined on 11-15-2006
- Posts 6
|
Is there a particular scenario where Attributes would be useful (other than brand new shiny feature)? Not being a dick here -- it just seems like
if (instanceof Foo) { doSomethingConcrete() }
Is fairly straightforward. Attributes could give you a richer set of things to inspect, but maybe at the cost of a level of indirection that could cause more problems in the long run. Is there a particular area where this would be useful? Just curious, as my imagination has hit the wall after 14 hours at work...
|
|
-
-
Cat


- Joined on 07-24-2010
- Posts 121
|
TGV:Actually, that does not make a lot of sense to me. If you want to check if a class supports certain behavior, check the name of the bloody class that actually defines that behavior! And checking that kind of stuff by naming is, well, asking for trouble. The whole point of a marker interface is that the classes that you're looking for don't even exist at this time. For example, in Java the Serializable marker interface tells Java that you can serialize the class to a file. Clearly the people who wrote serialization didn't know about every custom class anyone would ever want to serialize. In C#, there are attributes which are generally preferred, but there can be cases where a marker interface is more appropriate.. For example, a marker interface can be used as a constraint for generics, allowing for some additional compile-time type checking; you can't do a compile-time generic constraint on an attribute.
|
|
-
-
Cat


- Joined on 07-24-2010
- Posts 121
|
chooks:Is there a particular scenario where Attributes would be useful (other than brand new shiny feature)? Not being a dick here -- it just seems like
if (instanceof Foo) { doSomethingConcrete() }
Is fairly straightforward. Attributes could give you a richer set of things to inspect, but maybe at the cost of a level of indirection that could cause more problems in the long run. Is there a particular area where this would be useful? Just curious, as my imagination has hit the wall after 14 hours at work...
Attributes have a few advantages over marker interfaces. One big one is inheritance - an interface is always inherited, but an attribute may or may not be inherited. Plus, you can tag individual methods and properties with attributes, but not with marker interfaces.
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,315
|
Cat: chooks:Is there a particular scenario where Attributes would be useful (other than brand new shiny feature)? Not being a dick here -- it just seems like
if (instanceof Foo) { doSomethingConcrete() }
Is fairly straightforward. Attributes could give you a richer set of things to inspect, but maybe at the cost of a level of indirection that could cause more problems in the long run. Is there a particular area where this would be useful? Just curious, as my imagination has hit the wall after 14 hours at work...
Attributes have a few advantages over marker interfaces. One big one is inheritance - an interface is always inherited, but an attribute may or may not be inherited. Plus, you can tag individual methods and properties with attributes, but not with marker interfaces.
Attributes may also be passed parameters.
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
derari


- Joined on 12-04-2009
- Posts 30
|
Cat:Attributes have a few advantages over marker interfaces. Attributes are accessed via reflection, which is much slower than a simple instanceof check. Sometimes you need efficiency, not features. A good example for a marker interface is Java's RandomAccess, btw.
|
|
-
-
-
BC_Programmer


- Joined on 10-08-2009
- Posts 247
|
derari:Attributes are accessed via reflection, which is much slower than a simple instanceof check. Sometimes you need efficiency, not features. A good example for a marker interface is Java's RandomAccess, btw.
One shouldn't be performing instanceof checks- or attribute checks, for that matter- in a tight loop to begin with, though.
|
|
-
-
-
mihi


- Joined on 05-10-2008
- Posts 94
|
In fact, Java annotations were more or less based of C#'s attributes. :)
And if you think RandomAccess as an interface is a good idea, have a look at implementation of Collections.synchronizedList method (or some other examples) - it makes it really hard to implement wrapper implementations for lists. Having an isRandomAccess() method inside the List interface would have made it so much easier (having the additional benefit that everyone who implements a List himself is forced to think about this decision, and cannot forget to implement RandomAccess).
|
|
-
-
smxlong


- Joined on 06-23-2006
- Posts 150
|
derari: Cat:Attributes have a few advantages over marker interfaces. Attributes are accessed via reflection, which is much slower than a simple instanceof check. Sometimes you need efficiency, not features. A good example for a marker interface is Java's RandomAccess, btw.
If the relative efficiency between an instanceof and an attribute check actually matters, your design is stupid.
|
|
-
Page 1 of 1 (27 items)
|
|
|