I've read Head First Java, and I understand how OOP works. Here's my problem: I'm a PHP programmer, and while I've used OOP in PHP, I'm having trouble figuring out what should be an object and what methods to give it.
For example, let's say I have a app that allows people to log in and edit a document. Why should the document be an object if there will ever only be one instance? Should I give the deleteDocument()
method to the document object or the admin object? The document is the one being deleted, but the admin is the one performing the action.
So my real question is, coming from a procedural background, how do I figure out what should be objects and what should have what methods?
Well in your example, I'm not sure why in your design there's only one document, but it should still be an object in case at a later point you want more than one.
As far as the delete function, there's really no straightforward answer; you're likely to find arguments on both sides. Myself, I would put the lower level delete functionality (things like deleting database entries) inside the document class, but any other functionality may go in the parent. If all documents are owned by an admin, the admin should have a DeleteDocument which calls delete on the document, and also removes all associations from the database.
In general, coming from procedural, if you ever find yourself passing around a big array of state variables or declaring lots of globals, then turn that related functionality into a class. Try to keep the functionality that an object contains as closely related as possible, or you may find your classes bloating way out of control.