Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Interfaces

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading JAVA SE 7 Programmer Exams by SG Ganesh and Tushar Sharma. I have questions on simple Interface example. The authors mentioned that a good design for DataSet program goes as followed




my question is what could possibly go wrong if I drop the interface and write the code like this? The DynamicDataSet and SnapshotDataSet will function as the same right?
What would be the future disaster?

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Waylon Wolf wrote:my question is what could possibly go wrong if I drop the interface and write the code like this? The DynamicDataSet and SnapshotDataSet will function as the same right?


Did you try it out? Did it work?
 
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious why they didn't show this using a single DataSet class:

That's how a TreeSet works (sort of).

Or do they expand on this later?
 
Ranch Hand
Posts: 529
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Waylon Wolf wrote:I am reading JAVA SE 7 Programmer Exams by SG Ganesh and Tushar Sharma. I have questions on simple Interface example. The authors mentioned that a good design for DataSet program goes as followed


I guess there are SOLID reasons:)
 
Marshal
Posts: 70636
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have corrected your broken link; use the URL button rather than trying to write the tags by hand because it is more reliable.

I am not convinced about some of the things in that link. The O (open‑closed principle) was postulated by Bertrand Meyer, saying that a class shoul‍d be closed to modification and open to extension. Many people no longer believe that; if you look in many modern Java® packages, e.g. this one, you will find that most classes are closed to extension by being made immutable. So immutability with all the advantages it brings now takes precedence over open‑closed. I think also, that web link doesn't do a good job of explaining open‑closed. It shows how the Circle class can be corrected, not by extension, but by having an area() method added. That suggests to me that they are breaching the closed part of open‑closed. I also think a Circle class without an area() method fails to fulfil the requirements of the S part (single reponsibility).
Maybe “program to the interface” is not part of open‑closed either, at least not in its original form. Meyer disapproved of the concept of interfaces (see his book Object Oriented Software Construction), though he may have changed his opinion more recently; I don't know.
 
Giovanni Montano
Ranch Hand
Posts: 529
10
Android Open BSD Slackware
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have corrected your broken link; use the URL button rather than trying to write the tags by hand because it is more reliable.

I am not convinced about some of the things in that link. The O (open‑closed principle) was postulated by Bertrand Meyer, saying that a class shoul‍d be closed to modification and open to extension. Many people no longer believe that; if you look in many modern Java® packages, e.g. this one, you will find that most classes are closed to extension by being made immutable. So immutability with all the advantages it brings now takes precedence over open‑closed. I think also, that web link doesn't do a good job of explaining open‑closed. It shows how the Circle class can be corrected, not by extension, but by having an area() method added. That suggests to me that they are breaching the closed part of open‑closed. I also think a Circle class without an area() method fails to fulfil the requirements of the S part (single reponsibility).
Maybe “program to the interface” is not part of open‑closed either, at least not in its original form. Meyer disapproved of the concept of interfaces (see his book Object Oriented Software Construction), though he may have changed his opinion more recently; I don't know.



Really bright observations,  outside guru's academic dissertations with banal common sense I think architecture is a flexible concept that depends by the choices, goals and individual team comfort to use a system instead of another.
The site I gave is not optimal, but the principle are enunciated really clearly so I reckon was a good start, after all  is customary approach that architecture concepts needs different blogs, books, points of views because is an area less objective that the study of the language per se and the almost unequivocal way the compiler interprets the data, is like that since punched cards times.

Today TDD surged on top of the best practices, testability is king, an interface allow to propose mocks easier that candidates in political campaigns( Sir Joshua Bloch would love this considering his tweets).
Interfaces are king with one of the most used principle MVP Model View Presenter, furnishing a perfect way to inject inside the Presenter pure Junit business logic, making Uncle Bob and open source community happy and.. clean:)
but as we are in the philosophical concept of architecture everybody can contradict a point of view with often fair pro and cons
 
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Waylon Wolf wrote:my question is what could possibly go wrong if I drop the interface and write the code like this?


Well, I see that the letter 't' on your keyboard seems to have gotten stuck when you did that...

The example does fall short of being good -- Dave's example usage is better. The point is to program to interfaces so that different implementations can be easily switched in and out. In this example, it would be the sorting algorithm that you're using that can be switched out. By programming to an interface and reifying (making it an object) and parameterizing the sorting algorithm, the code in the DataSet class will not need to be changed if you decide to use different sorting algorithms later on.  The same thing can be achieved with an abstract base class and subclasses that implement different sorting algorithms. The alternative that you proposed obviously does not compile because of strong typing in Java. In languages that use duck typing like Go and Ruby, something like that would work.
 
Campbell Ritchie
Marshal
Posts: 70636
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:. . . The site I gave is not optimal, but the principle are enunciated really clearly . . .

That link doesn't enunciate anything clearly at all.


This discussion is too difficult for this forum, so I shall move it.
 
Giovanni Montano
Ranch Hand
Posts: 529
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That link doesn't enunciate anything clearly at all.




Ah ah (that stays for  Honesty Hits Agreeable Helpers)
 
Giovanni Montano
Ranch Hand
Posts: 529
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:

Campbell Ritchie wrote:That link doesn't enunciate anything clearly at all.




I mean  Attention Hits Agreeable Helpers)

 
Waylon Wolf
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:I'm curious why they didn't show this using a single DataSet class:

That's how a TreeSet works (sort of).

Or do they expand on this later?






Dave,
I meant to write that. "Sorting sorting" should be commented out on the second set of codes. Since I intentionally take the interface off to see if it works.
 
Dave Tolls
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking the interface off will result in it not compiling, as there is no possible relationship between the xxxSort classes and the interface.

Removing the sorting attribute form the class, as you just have, will result in it not compiling as well.
I'm not sure what you are trying to show with that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic