• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about the whole object/method thing  RSS feed

 
Cody Wade
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok--I seem to be having some big problems with this whole object thing (apparently, I'm a procedural guy). So if someone wouldn't mind giving me a hint, I'd appreciate it.

Let's say that I need to read a file that contains a person's name, address and phone number, then display that on the screen.

I'm not saying this is correct or "good technique", but this makes sense to me (in PHP):

From main page, call the function that opens the file and hands it back
call the parser and hand it the file variable
parser reads the file, parses it, stores it in a structure/class of some kind, then returns that
Main page takes output from the parser and displays it as it wishes.


So in Java, this is confusing to me. Partly because there's the issue of the code running in the system and the code recorded in the actual files.

So let's start with the code written in the class files.

I want a JFrame, with a menubar and a textarea. So I put the menubar code in a separate file (menubar.java). That file contains a constructor and the Action handler. The action handler for the File>Open menu item is in there.

If I try to put the action handler into a separate file, Eclipse tells me I need to put it in the menubar.java. If I do that, then I run into this other issue, when the code is running.

So I instantiate the JFrame, and from there create a new object of type menubar. I add the menubar and the textarea to the frame. I can open and parse a file and dump it to the console...but I can't figure out how to reference the text area in the frame.

I'm so confused
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want a few classes which will do the reading and writing. Get them working; that is the Model of your system. Then you can get them to display the writing on a nice panel, which is the View, and you can have some buttons to get the next line, which you call the Control. Get one bit working first. It's much easier like that.

You have actually described an object-oriented way to handle your data. You have a file, you get the data from it, and you use those data to create an object. Once you get those data into the object, they are available for you to use to your heart's content. But your page doesn't take the output from the parser; it takes the object and gets the data already organised from that.
 
Parag Shah
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of objects as those things that are running in your system. They are very much like real objects in the real world (car, window, room, etc). The classes are blueprints for those objects. Think of them like an engineers design of a car, an architects design of a room, etc. The files are simply a way to organize the classes (and not objects). Think of them as folders or drawers in which the architect keeps paper designs.

Let's think about your problem in OO terms:

You want a parser that can parse the file and create an object which represents the contents of the file. I don't know what is in the file, but let's say it contained the details of a book (title, author, ISBN, etc), then the parser would create a Book object from that file. This object needs to be displayed, so you need a BookView object. The BookView object would contain all the visual artifacts you need. Since we are using OO, these artifacts would also be objects, and objects would be composed of other objects. A theoretical example would be like a room being composed of windows, doors, etc. A practical example would be a BookView objects being composed of JFrame, JMenu, JTextArea, etc.

How would all these objects be created by the system? It needs a blueprint. Just like we need a floor plan and elevation before we can build a house. These blueprints are your classes. Again the files are simply a way of organizing those classes so that your software is understandable and maintainable. Typically we use one file per class WE create (there are exceptions to this rule, but I will not get into that right now).

You do not need a file for menubar. You need a file for your view class, which will be composed of a JMenuBar, etc. You also need a file for your Parser and a file for a class which will start your software (this is a good practice).

--
Regards
Parag Shah
http://diycomputerscience.com

 
Cody Wade
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's starting to sink in.

So rather than creating a menubar.java file, I want a window.java file that includes the frame, textarea and menubar (and actions).

Then a (in Parag's example) a book class in a book.java file, that includes methods for reading the file and parsing it.

So I create an object that is the frame (the view), and from there I can call/create a book object.

book.load can read the file and parse it into memory

then frame.print could take that book object and get the information from it (either from the internal variables directly, or from a getter method inside the Book object) and print that into the text area.

Does that sound right? Because that makes a whole lot more sense.

Assuming this is (at least mostly) correct, thank you guys. I had heard about MVC before, but didn't really understand why it mattered. I have a better idea.

Also, can I +1 both of you?

 
Parag Shah
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Wade wrote:I think it's starting to sink in.

So rather than creating a menubar.java file, I want a window.java file that includes the frame, textarea and menubar (and actions).

Then a (in Parag's example) a book class in a book.java file, that includes methods for reading the file and parsing it.

So I create an object that is the frame (the view), and from there I can call/create a book object.

book.load can read the file and parse it into memory

then frame.print could take that book object and get the information from it (either from the internal variables directly, or from a getter method inside the Book object) and print that into the text area.



A few thoughts :

In an OO system, think in terms of responsibilities and objects. Objects have responsibilities, and they fulfill their responsibilities through their methods. When another object wants to get some work done, it calls a method of an object. The software works by these objects collaborating with each other.

The Window object (whose blueprint/design is in Window.java) is responsible for displaying the main view of your software. To display the view, it needs a JFrame, a JMenuBar, and other visual components which you want to include. Before moving on to the next point, I would like to mention that naming a class WIndow, may not be a good idea because it is a very generic name. You can perhaps call it MainView.

Book.java is what could be called a POJO (Plain Old Java Object). It simply holds values, and has getters/setters. It's responsibility is to simply be a container for attributes that make a book.

The class responsible for parsing could be called BookParser. This class is responsible for parsing an InputStream and creating a book object from the InputStream.

You create the MainView object (which could either inherit from JFrame or could be composed if it), which provides the display. In the display there should be a way for a user to select a file. When the file is selected, and ActionListener will be invoked. This action listener instantiates BookParser, which returns a Book object. The ActionListener takes the book object and displays it in a view.


--
Regards
Parag
http://diycomputerscience.com
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!