Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Can this ca...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 24 Topic 16045 of 16085
Post > Topic >>

Re: Can this callback mechanism be generified?

by Mark Space <markspace@[EMAIL PROTECTED] > May 2, 2008 at 04:21 AM

Does this work? I didn't try it.  I think the "doCallback()" method is 
ok without parameterized types... same with putting an unparameterized 
type into the map.

OTOH doesn't generics just insert the casts for you?  I don't think you 
are saving any execution time here, and the code does not necessarily 
look more clear to me.


Casper Bang wrote:

> 
> 1  interface Callback
interface Callback<C>
> 2  {
> 3      String format(Object object);
   String format( C Object );
> 4  }
> 5
> 6  class Somewhere
> 7  {
> 8      Map<Class, Callback> callbacks = new HashMap<Class, Callback>();
> 9
> 10     public void installCallback(Class clazz, Callback callback)
> 11     {
> 12         callbacks.put(clazz, callback);
> 13     }
> 14
> 15     public void doCallback(Object obj)
> 16     {
> 17         Callback callback = callbacks.get( obj.getClass() );
> 18         if(callback != null)
> 19             System.out.println( callback.format( obj ) );
> 20     }
> 21 }
> 22
> 23     somewhereInstance.installCallback( Date.class, new Callback(){
      somewhereInstance.installCallback( Date.class, new Callback<Date>(
> 24         public String format(Date obj)
      public String format(Date obj)
> 25         {
> 26             Date date = (Date)obj;
              Date date = obj;
> 27             return SimpleDateFormat.getInstance().format(date);
> 28         }
> 29     });




 24 Posts in Topic:
Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 04:51:44 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-02 04:21:23 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 13:14:16 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-02 08:34:23 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 19:58:28 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 20:58:55 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-02 13:33:23 
Re: Can this callback mechanism be generified?
Joshua Cranmer <Pidgeo  2008-05-02 20:59:06 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-03 23:33:40 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-02 14:14:03 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-03 03:45:00 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-03 23:27:15 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 04:01:44 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-04 20:24:40 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-05 04:06:24 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 20:54:51 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-05 21:09:20 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-05 21:23:44 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 20:36:28 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-05 21:16:23 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-06 10:25:33 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-06 18:56:19 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-06 15:58:21 
Re: Can this callback mechanism be generified?
thufir <hawat.thufir@[  2008-05-02 21:05:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 3:59:46 CDT 2008.