-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Basic abstraction
PostPosted: Fri Jan 05, 2007 8:17 am 
Newbie

Joined: Sun Nov 26, 2006 12:53 pm
Posts: 13
Hi Community, I have a design question, newbie-type:

In my app I have, for example, two POJO classes:

Cat, Dog

no matter their properties and methods. Then I have a controller:

AnimalController

in this class I have methods such as

getCat, getDog, setCat, setDog, editCat, editDog


In setDog I have something like this:

Code:
   public Dog setDog(String color, int type) {
      startTransaction();
      Dog d = null;
      try {
         d = new Dog();
         d.setValue(value);
         d.setType(type);
         
         session.save(d);
         
         commitTransaction();
      } catch (HibernateException he) {
         rollbackTransaction();
         
         he.printStackTrace();
         throw he;
      }
      return d;
   }


Obviously this works fine. But I have to write the same code for Cat. The question is:

Is there a way to write, without interfacing Dog and Cat in a common Animal interface, an abstract method like this:

Code:
   public Object setObject(String ClassName, String color, int type) {
      startTransaction();
      ObjectOfTheTypeInClassName d = null;
      try {
         d = new ObjectOfTheTypeInClassName();
         d.setValue(value);
         d.setType(type);
         
         session.save(d);
         
         commitTransaction();
      } catch (HibernateException he) {
         rollbackTransaction();
         
         he.printStackTrace();
         throw he;
      }
      return d;
   }


thank u in advance, hope that max doesn't reply telling me that there is a previous post similar to this. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 10:41 am 
Beginner
Beginner

Joined: Wed Aug 31, 2005 3:54 am
Posts: 45
> Is there a way to write, without interfacing Dog and Cat in a common Animal interface, an abstract method like this:

It's difficulte to generalize a behavior without create interface ...
you can make Dog and Cat inherit from a common abstract class which have this behavior ???


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 11:49 am 
Newbie

Joined: Sun Nov 26, 2006 12:53 pm
Posts: 13
Can u tell me which is your idea?
I think that using Class.forName() teorically the stuff should work but I still haven't found the right syntax.


Top
 Profile  
 
 Post subject: instanceof
PostPosted: Mon Jan 08, 2007 6:56 am 
Beginner
Beginner

Joined: Wed Aug 31, 2005 3:54 am
Posts: 45
Can you use an instanceof ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.