tags:

views:

146

answers:

5

I 'm concern about what techniques should I use to choose the right object in OOP

Is there any must-read book about OOP in terms of how to choose objects?

Best,

+2  A: 

You probably mean "the right class", rather than "the right object". :-)

There are a few techniques, such as text analysis (a.k.a. underlining the nouns) and Class Responsibility Collaborator (CRC).

With "underlining the nouns", you basically start with a written, natural language (i.e. plain English) description of the problem you want to solve and underline the nouns. That gives you a list of candidate classes. You will need to perform several passes to refine it into a list of classes to implement.

For CRC, check out the Wikipedia.

I suggest The OPEN Toolbox of Techniques for full reference.

Hope it helps.

CesarGon
Yes, I mean the right class
demian
Sure. :-) Have a look at these techniques and, if you can, that book.
CesarGon
A: 

I am assuming that there is understanding of what is sctruct, type, class, set, state, alphabet, scalar and vector and relationship.

Object is a noun, method is a verb. Object members can represent identity, state or scalar value per field. Relationships between objects usually are represented with references, where references are members of objects. In cases, when relationships are complex, multidirectional, have arity greater than 2, represent some sort of grouping or containment, then relationships can be expressed as objects.

For other, broader technical reasons objects are most likely the only way to represent any form of information in OOP languages.

RocketSurgeon
yes, but it give you a common example .. a shopping cart, where maybe you may want to add the add_items, check_out to the Customer Class instead of creating a shoppingCart Class .. So where is the line? ..
demian
Yes, Customer is class. Question: is it 1-1 (exactly one to exactly one) with ShoppingCart ? Lets do lifecycle test. Does Customer info live longer than his shopping visit. For example in your design you say "Yes, we persist it in database for next possible shopping visit". Ok, another lifecycle test. Does your shopping cart always has one and only one customer. For example you answer "No, we have anonymized stub which is not really a customer when anonymous visitor did not identify him/herself". Then its a separate class.
RocketSurgeon
great answer RocketSurgeon .. that helps me a lot, thanks!
demian
"Object is a noun, method is a verb." OK, but note that in both languages, nouns can be actions (*touchdown*, *homocide*, `javax.swing.Action`) and verbs can mean all sorts of things besides actions (`getName()`, `hashCode()`, `iterator()`, *should*, *did*, *can*, *want*).
Jason Orendorff
+1  A: 

Just write something that gets the job done, even if it's ugly, then refactor continuously:

But:

It's not a precise recipe, just some general guidelines. Keep practicing.

P.S.

Code objects are not related to tangible real-life objects; they are just constructs that hold related information together.

Don't believe what the Java books/schools teach about objects; they're lying.

hasen j
+1 Great answer, would be even better if it were shorter. :) Good object oriented design focuses on hitting those 5 bullet points (especially about cohesion and coupling!) and (I would add) providing sweet simple APIs that people can easily use. *Not* matching classes to nouns and methods to verbs.
Jason Orendorff
Thanks! I will try to shorten it a bit.Providing sweet APIs is a whole field of art in and of itself. I mean, any programmer can come up with an API like the DOM, but how many people could come up with API like jQuery?
hasen j
A: 

You should check out Domain-Driven Design, by Eric Evans. It provides very useful concepts in thinking about the objects in your model, what their function are in the domain, and how they could be organized to work together. It's not a cookbook, and probably not a beginner book - but then, I read it at different stages of my career, and every time I found something valuable in it...
alt text

Mathias
A: 

I am adding a second answer due to demian's comment:

Sometimes the class is so obvious because it's tangible, but other times the concept of object it's to abstract like a db connector.

That is true. My preferred approach is to perform a behavioural analysis of the system (using use cases, for example), and then derive system operations. Once you have a stable list of system operations (such as PrintDocument, SaveDocument, SpellCheck, MergeMail, etc. for a word processor) you need to assign each of them to a class. If you have developed a list of candidate classes with some of the techniques that I mentioned earlier, you will be able to allocate some of the operations. But some will remain unallocated. These will signal the need of more abstract or unintuitive classes, which you will need to make up, using your good judgment.

The whole method is documented in a white paper at www.openmetis.com.

CesarGon