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

toString() and MVC Pattern(observer/observable)

 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi members,
i'm trying to complete my assignment at present. However, I don't understand how it works of the method toString()in DataInfo class.
Also, I woule like to find more information in detail how to work of Observe/observable and how I can use it very well.
Thus, I was looking for any good example to understand, but it's not easy.
Please let me know if anyone could help in this matter.
look forward to hear soon.
Paul
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11911
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Try adding this code into the DataInfo class:

Compile it and run it. You should see something like:
suncertify.db.DataInfo@16f0472recordNumber = 3
Fields:Name = Title value = Mr
Name = LastName value = Jones
Now comment out the entire toString() method, comile it and run it again. This time you should see something like:
suncertify.db.DataInfo@1ea2dfe
Basically, the toString() method is called whenever you use an instance of a class in a place where a String is expected ... in the case of the code I provided above, I was using the instance of the class in the System.out.println() which expects Strings as parameters.
As you saw, without a custom toString method, the output was just the hashCode - not very usefull if you are printing this out in order to debug your code.
But when you override the to toString method, you can have a much more meaningful output.
You can override the toString method in all your classes so that a user can get a meaningful view of what is happening in your class at any given time.
As for the MVC, try looking at:
The Sun Model View Controller blueprint for an explanation of what the responsibilities of each item is.
Also, try looking at this thread which has a very good explanation and some sample code.
Regards, Andrew
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"pmaeng"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark
 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your good explanation regarding the toString() method. I got it now.
I also try to add your code and compile to run.
I anticipate.
Have you got any good example for observer/obxerable? If so, could you show me again when you have a time.
Cheers,
Paul
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11911
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Please change your name as requested by the bartender.
The link I gave has code which includes Observer Observable pattern in an MVC. However it does not show the Sun Observer interface and Observable classes. From memory the reason it doesnt use Sun's classes is also mentioned in the thread.
Are you asking for a simpler example? Something that only shows Observer-Observable so you can see it and understand it without worrying about what else is going on? Or are you asking for something that show's Sun's classes? If so, let us know and we will see what we can do.
Regards, Andrew
[ May 14, 2003: Message edited by: Andrew Monkhouse ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic