if you want to program something, first, write down what you want in plain english (or your own language, if english isn't your first language :) )
the nouns you find in that text, are the objects (classes) you'll need. the verbs are the actions (methods) to write.
actually, they don't fail to mention it in those books, but not every one has got grip on the OO concepts on the first read.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
... does that not mean that in more sophisticated programs there would be hundreds of classes, making it pretty unorganised?
Yes there will be hundreds of classes, but no. that's not necessarily unorganised, especially compared to the alternative of having all that code but not organised into classes. OO isn't perfect, but it's the best we've got.
Also, not every noun in the spec needs a class - many of them will create an attribute (variable) within a class. It sounds vague, but when you actually do it a couple of times it's not that hard to guess which nouns are classes and which are just attributes. For big applications you also spend a lot of time reviewing the class design and manually running through the messaging sequences for the use cases. This gives yhou plenty of opportunity to refine the class design before writing any code.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
"A Library has books, each book has an ISBN, a title and an author..."
Library and Book are classes, ISBN, title, author are attributes of a Book.
One rule of thumb is if something can be represented by a Java primitive or a String then it's probably an attribute, if it needs multiple primitives or Strings then it's definitely an object.
You have to read the whole spec to ensure none of the attributes are actually more complex than that, and need to be upgraded to objects, eg
" ... authors are paid royalties for each book sold..." - aha! there's more to Author than just a String, make it a class.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Yes, sort of. In practice you usually end up with each class having an attribute that corresponds to the database table key (eg ISBN), so the class corresponds to a table, and instances correspond to rows. Of course, attributes can themselves be instances of classes or collections, so most classes need more than 1 table.
Once you start to implement you will add a load more classes to handle the mechanics of the application, and a launcher is very common. There will often be a lot of startup to be coordinated - opening databases, starting servers, loading parameters and defaults, initialising GUIs... So a (small) class to do all that makes sense.
Yes, you could put all that in one of the other classes, but that would degrade the encapsulation - each class should be/do one thing. Designs with many single-purpose classes are easier to write and understand than designs with fewer multi-function classes.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073