my dog learned polymorphism*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes toString() and MVC Pattern(observer/observable) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "toString() and MVC Pattern(observer/observable)" Watch "toString() and MVC Pattern(observer/observable)" New topic

toString() and MVC Pattern(observer/observable)

Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
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.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

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:
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

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
Thanks! and welcome to the JavaRanch!

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
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.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

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 ]
It is sorta covered in the JavaRanch Style Guide.
subject: toString() and MVC Pattern(observer/observable)
Similar Threads
Observer and Observable
how to turn Observable to a String[][]
"Observable" packages?
regarding observability
Question on class design for DataClient and Client