Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

general methods to different classes

 
Ricardo Soares
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, im new to java so be gentle

imagine you have three different classes ( they are from different types also ), lets say:
class circle
class dog
class car
each one of this classes will have a method create, edit and remove. The create method will for example assign values to the variables ( which are different in each class ) and add the item to an ArrayList ( three different ArrayLists will contain the three different types ). Same for the edit and remove methods. My question is, is there anyway i can make those methods general ? Have a method create that will work for every class ?

Thanks in advance
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are those really different types? Dog Circle and Car look different, but you are telling me they have common behaviour. You would have to explain a bit more. What is the create method doing which is not done in the constructor? Is it creating a new instance?
Can you put wrappers round those objects? Can you make the implement a common interface? Can they inherit from a common (maybe abstract) supertype?
There is something strange about methods which create an object and add the object to a List simultaneously. In fact, doing that in a constructor might cause serious errors. Really, a method should do one thing and only one. Why do the objects have to add themselves to the list, rather than something else adding them to the List?

You would have to explain a lot more about what you want to do, so we can understand the problem.
 
Ricardo Soares
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dog car circle was an example, in reality im working with Agent, Artist and Event. They are gonna have similar methods because i need to create, edit, delete and print all of them but they are different.
Example:
create an artist - set name, id - add to artists list
create an event - set name, data, duration ( days ) - add to events list
create an agent - set name, id - add to agents list
I asked if i could make those methods general to avoid code repetition and make it the best way possible.
Then they will have to interact with each other, there will be a contract that will be signed between an artist and an agent. I will have to assign a few artists to a given event, etc
I think they can inherit from a superclass although i cant think how should i do that. Maybe creating an interface could be a good solution.
Im not into that wrapper thing yet, is it what is explained here ? http://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html
Hope you can understand it better now. Thanks for your time and help
 
Winston Gutkowski
Bartender
Pie
Posts: 10501
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ricardo Soares wrote:Example:
create an artist - set name, id - add to artists list

I think you're confusing two different things here. An Artist is an Artist; the fact that you're adding him/her to a List is totally irrelevant.

I suppose it would be theoretically possible to define a common interface for all 3 classes called something like WillBePartOfAList, but to be honest I can't see any benefit to it.

Programming is hard enough without dreaming up added complexity simply to save you typing. To paraphrase Albert Einstein: Keep things as simple (←click) as possible, but no simpler.

HIH

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are describing for create sounds like a constructor. Constructors are not methods. You ought (in my opinion) to initialise all your instance fields in the constructor (but there is not consensus on that point, as you will see from this interesting discussion). As I said before, you ought not to add the object to the List from the constructor. In fact you probably ought not to add the object to a List from inside itself. An object should take care of itself, but things like adding to Lists belong in different classes.
Artists, Events and Agents are different, having little common behaviour. Agree with Winston it is very peculiar design to try to make the three implement a common interface. Even though they all inherit from java.lang.Object.
 
Ricardo Soares
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have at the moment without the adding to ArrayList, etc. Is this well structured ? Do i have to do it this way with Agents and Artists ?

Thanks for the time spent









 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! Never use public fields like that. Make all the fields private. In the case of an event, they might beneficially be final, too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic