Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Tagging interfaces

Reply
Thread Tools

Tagging interfaces

 
 
VisionSet
Guest
Posts: n/a
 
      11-29-2005
Why exactly are they bad?

I have a entity, a collection of which is to be associated with an object
that has no need to call any methods on or know anymore than the entity is
of a certain type. These entities are then passed back to the object that
instantiated them which can cast to the concrete type to do something useful
with them. So a tagging interface seems to fit the bill for my entity.

--
Mike W


 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      11-29-2005
Who said they're bad?

 
Reply With Quote
 
 
 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      11-29-2005
VisionSet wrote:
> Why exactly are they bad?
>
> I have a entity, a collection of which is to be associated with an object
> that has no need to call any methods on or know anymore than the entity is
> of a certain type. These entities are then passed back to the object that
> instantiated them which can cast to the concrete type to do something useful
> with them. So a tagging interface seems to fit the bill for my entity.


Please correct me if I have misunderstood anything:

Each "Entity" requires type-specific processing, but you want the code
for all the processing, for all the different types, to be defined in
one "Master" object.

The Master, which actually created all of these Entities, forgets the
Entities' exact types at some point. It therefore wants them to be
"tagged," possibly by having them implement empty but nominally
different interfaces.

This sounds like a perfect case for polymorphism. Forget all the
tagging interfaces. Have all the Entities implement just one interface,
and each concrete Entity type can implement the interface differently.
If you really want to keep the processing code in the Master object, let
each concrete Entity type just immediately invoke a type-specific
callback method in the Master.

If this isn't clear, or doesn't sound like a good idea, let me know &
I'll whip up some example code.
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      11-29-2005

"Jeffrey Schwab" <(E-Mail Removed)> wrote in message
news:Hw%if.8543$(E-Mail Removed) om...
> VisionSet wrote:
> > Why exactly are they bad?
> >
> > I have a entity, a collection of which is to be associated with an

object
> > that has no need to call any methods on or know anymore than the entity

is
> > of a certain type. These entities are then passed back to the object

that
> > instantiated them which can cast to the concrete type to do something

useful
> > with them. So a tagging interface seems to fit the bill for my entity.

>
> Please correct me if I have misunderstood anything:
>
> Each "Entity" requires type-specific processing, but you want the code
> for all the processing, for all the different types, to be defined in
> one "Master" object.


The number of types (tagged types) and number of master objects is a 1:1
relationship
In fact there is only ever likely to be one entity type and one master
object, but that is not important.
We shall assume other types may exist at some point.

>
> The Master, which actually created all of these Entities, forgets the
> Entities' exact types at some point. It therefore wants them to be
> "tagged," possibly by having them implement empty but nominally
> different interfaces.


The tagging is merely type safety mechanism so the surrogate collector can
only collect a certain type, and the master only gets back what it expects.

--
Mike W


 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      11-29-2005
VisionSet wrote:
> "Jeffrey Schwab" <(E-Mail Removed)> wrote in message
> news:Hw%if.8543$(E-Mail Removed) om...
>
>>VisionSet wrote:
>>
>>>Why exactly are they bad?
>>>
>>>I have a entity, a collection of which is to be associated with an

>
> object
>
>>>that has no need to call any methods on or know anymore than the entity

>
> is
>
>>>of a certain type. These entities are then passed back to the object

>
> that
>
>>>instantiated them which can cast to the concrete type to do something

>
> useful
>
>>>with them. So a tagging interface seems to fit the bill for my entity.

>>
>>Please correct me if I have misunderstood anything:
>>
>>Each "Entity" requires type-specific processing, but you want the code
>>for all the processing, for all the different types, to be defined in
>>one "Master" object.

>
>
> The number of types (tagged types) and number of master objects is a 1:1
> relationship
> In fact there is only ever likely to be one entity type and one master
> object, but that is not important.
> We shall assume other types may exist at some point.
>
>
>>The Master, which actually created all of these Entities, forgets the
>>Entities' exact types at some point. It therefore wants them to be
>>"tagged," possibly by having them implement empty but nominally
>>different interfaces.

>
>
> The tagging is merely type safety mechanism so the surrogate collector can
> only collect a certain type, and the master only gets back what it expects.


Wouldn't it be easier, and make your intent clearer, to use generics?
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      11-29-2005

"Jeffrey Schwab" <(E-Mail Removed)> wrote in message
news11jf.7997$(E-Mail Removed) om...

> >
> > The tagging is merely type safety mechanism so the surrogate collector

can
> > only collect a certain type, and the master only gets back what it

expects.
>
> Wouldn't it be easier, and make your intent clearer, to use generics?


I'll still need a type, no?

Collection<? extends TagIntf>

--
Mike W


 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      11-29-2005
VisionSet wrote:
> "Jeffrey Schwab" <(E-Mail Removed)> wrote in message
> news11jf.7997$(E-Mail Removed) om...
>
>
>>>The tagging is merely type safety mechanism so the surrogate collector

>
> can
>
>>>only collect a certain type, and the master only gets back what it

>
> expects.
>
>>Wouldn't it be easier, and make your intent clearer, to use generics?

>
>
> I'll still need a type, no?
>
> Collection<? extends TagIntf>


You said you wanted the surrogate collector to "only collect a certain
type." You should be able to use this type as the parameter for the
generic container, e.g. Collection<Entity>. No?
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      11-29-2005

"Jeffrey Schwab" <(E-Mail Removed)> wrote in message
news:aY1jf.5420$(E-Mail Removed) om...

> >>Wouldn't it be easier, and make your intent clearer, to use generics?

> >
> >
> > I'll still need a type, no?
> >
> > Collection<? extends TagIntf>

>
> You said you wanted the surrogate collector to "only collect a certain
> type." You should be able to use this type as the parameter for the
> generic container, e.g. Collection<Entity>. No?


I said I wanted to assume that the 1 type may grow to include others,
although only 1 type would be used in any 1 instance of the program, but
that is irrelevent I want the code to be able to support that eventuallity.
Hence the tagging interface.

But I'd heard there really was no defence for tagging interface approach,
and that is is merely a hack to allow introspection. But when you are faced
with the situation where an objects references are collected at various
places because that is where they naturally belong, and that is the best way
to manage there existence, but no methods are called on them, and you want
to decouple their logic, then this seems like an okay use to me.

--
Mike W


 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      11-29-2005
VisionSet wrote:
> Why exactly are they bad?
>
> I have a entity, a collection of which is to be associated with an object
> that has no need to call any methods on or know anymore than the entity is
> of a certain type. These entities are then passed back to the object that
> instantiated them which can cast to the concrete type to do something useful
> with them. So a tagging interface seems to fit the bill for my entity.
>
> --
> Mike W
>
>


Before 1.5, Tagging (or more commonly know as 'Marker') Interfaces were
a good way of achieving the same effect as 1.5 Annotations.

They were never bad - mis-understood and therefore mis-used.

Now we have annotations there is certainly less need for them.

Andrew
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-30-2005
VisionSet <(E-Mail Removed)> wrote:
> Why exactly are they bad?
>


My thoughts on the matter.

If you think of what you're doing as a tag interface, then it's probably
a bad idea, at least as of Java 1.5. The reason is that interfaces are
part of the type system, and as such they imply a certain relationship
between the types of objects that implement them.

There are two ways you can solve this apparent mismatch.

(a) Give up on making the "tag" a type. With Java 1.5, you'd then
decide to use an annotation, instead. Annotations don't define type
system relationships, but they do allow you to note information about a
type that can be used (among other places) dynamically at runtime. For
example, if Java were newly designed today, Cloneable and Serializable
would probably be annotations, because no one ever keeps a variable of
type Serializable or Cloneable.

(b) The other direction: stop thinking of it as a tagging interface, and
start thinking of it as a full-fledged type. Even if that type doesn't
necessarily need to declare any methods at all (as in your example), it
is a type... and it COULD declare methods. Once you're comfortable with
that, stop calling it a tagging interface, and stop worrying about bad
design. (Of course, if you then think of operations that make sense and
are useful to your application on all referents of that type, by all
means add them to the interface... especially if this allows you to take
advantage of polymorphism.)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
802.1q tagging/untagging on Catalyst 2950 arthur Cisco 4 04-20-2006 08:04 PM
802.1q Tagging on WS-C2924C-XL (old 4MB Model)? Micha Schoeler Cisco 1 07-22-2005 12:09 AM
cisco vlan tagging ants Cisco 2 05-05-2005 01:19 PM
STP and VLAN tagging uofitorn@gmail.com Cisco 2 03-29-2005 09:32 PM
Vlan tagging switches. JP Morgan Cisco 3 10-27-2004 05:54 PM



Advertisments