• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending Sun interface

 
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,

I had the need to add a few public methods to my Data class that implements my own interface that
extends the interface in the assignment provided by Sun.
For example in my own interface I have a getAllRecords() method.

Do all the public methods (signature) present in my Data class that are not in the interface provided by Sun,
should be present in my own interface I am extending from Sun?

Thanks,

Carlos.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlos,

In my humble opinion: yes, they should. And the reason is quiet obvious: best practice would be to program against an interface, so you can easily change the implementation with another implementation without braking your code. And if you don't add this methods to your interface then you can't invoke these methods. And if it's not necessary they are called from somewhere else, methods should of course not be public.



If I need to replace Data with another implementation I have to implement the contract (interface) and replace all Data occurences with the new implementation class. I will get compiler errors because I didn't know that I had to implement doSomething! And it is possible that the other implementation is developed by someone else (just implement the contract for a RDBMS for example) and then this class is passed to someone else who has to adjust the main program. He will get a lot of red crosses and will hunt you down and probably want to use your head for some




Kind regards,
Roel
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:In my humble opinion: yes, they should. And the reason is quiet obvious: best practice would be to program against an interface, so you can easily change the implementation with another implementation without braking your code. And if you don't add this methods to your interface then you can't invoke these methods. And if it's not necessary they are called from somewhere else, methods should of course not be public.



100% agreed.
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel and Roberto,


Just what I thought and suspected, just wanted to double check.
Thanks once again,



Carlos.
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto and Roel,


Going back to this subject again, I still have a few things to sort out:

How do you handle the getInstance() methods that are public static if you implement the Data class as a Singleton pattern?

Also, if you make your Data class to read dynamically all the values from the schema in the database file
such as numberOfFields without hardwiring and declaring a lot of constants, I was thinking about having
a method getNumberOfFields() declared as public static that could be called by my Room class that has conversion methods
public static String[] toStringArray(Room room) and public static Room toRoom(String[] record).
In my Room class I have:



I do understand Roel's point about having all the public methods that my class exports in my interface extending Sun interface.


Thanks,


Carlos.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlos,

The static getInstance method can't be in the interface of course. If you use a dao factory (like I did) this is not really a problem, because each dao will be created through this factory. So if it changes to not being a singleton for example, the only place you have to change it will be in this factory.

I didn't used the numberOfFields in the Room class, because in the perfect application you would create for example an interface MetaData (having methods to return numberOfFields, data type of each column, size of each column,...) and you will have a DataMD (implements MetaData). And then you would make calls against the MetaData interface. But in my opinion that was just 1 bridge too far (you could even use this metadata to pass to the client for limiting text fields etc) and I explained in my choices.txt this could be an enhancement for future releases

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Carlos!

Also, if you make your Data class to read dynamically all the values from the schema in the database file
such as numberOfFields without hardwiring and declaring a lot of constants, I was thinking about having
a method getNumberOfFields() declared as public static that could be called by my Room class that has conversion methods
public static String[] toStringArray(Room room) and public static Room toRoom(String[] record).



Hum... so you would call it from your Room class, right? I like the idea of having at least this intelligence (toRoom() and toStringArray() methods) in the Room class. But, why do you want to call the getNumberOfFields() method from there? To perform some validation?
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto,

In Room toRoom(String[] record) yes, I do check the length of the String[] to be converted to see if it's equal to
numberOfFields and if it isn't I throw an IllegalArgumentException
and in String[] toStringArray(Room room) I do instantiate a String[numberOfFields], that later I build and I will return.

Hi Roel,

I guess in your Room class you just solved this by declaring a


?


Thanks,


Carlos.

 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Carlos!

You know, last monday, in the master program, my professor was talking about responsibilities, which is what the class knows and does. The class knows how many fields it has, so you can perform this validation based on the number of fields the class has without having to call the getNumberOfFields() from the Data class.

But still champion, if you feel confident that the getNumberOfFields() method is really needed, go for it!
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto,

So you think it isn't necessary?
Shall I just have a constant instead?

Thanks,

Carlos.
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, champion!

Well, I'd say that I constant would save some computing... the class already knows how many fields it has, so you can define this value in a constant and use it when performing the validations (the validation will be done against the class, not the database schema).
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carlos Morillo wrote:I guess in your Room class you just solved this by declaring a static private final NUMBER_OF_FIELDS= 7;


No Carlos, I didn't had to solve anything, because I just created an anonymous array. A bit sneaky, I know

Kind regards,
Roel
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto,

I hate to drill down so much on this, but you guys are making me steer
towards just defining a constant and also looking at it in terms of decoupling
the Room class from the Data class so Room doesn't have to rely on any knowledge
of the Data class.
I think this way it will look way much cleaner.

because I just created an anonymous array.



Roel,

Well at some point you have to instantiate the String[] and specify the size with 7 elements.

Anyway, I am declaring the constant in Room and getting rid of static public int getNumberOfFields() in my Data class.

Thanks,

Carlos.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carlos Morillo wrote:Well at some point you have to instantiate the String[] and specify the size with 7 elements.

That's true, but that happens only in the create method of the Data class (and in this class you have knowledge about the database schema, so no constant required). In the method to convert my room object to a String[] I just have this code:
Kind regards,
Roel
 
Carlos Morillo
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

I still think if you are going to validate the String[] length in a method like public static Room toRoom(String[] record)
you would still need a constant.

The static getInstance method can't be in the interface of course. If you use a dao factory (like I did) this is not really a problem



Is it necessary to define an interface to be implemented by the DAO Factory object?

Thanks,

Carlos.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlos,

I still think if you are going to validate the String[] length in a method like public static Room toRoom(String[] record)
you would still need a constant.

That's true, but I didn't do a check. Is one of the very few methods which parameters are not validated.

Is it necessary to define an interface to be implemented by the DAO Factory object?

I don't know exactly what you mean, but I found this link very helpful.

Kind regards,
Roel
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic