aspose file tools*
The moose likes Beginning Java and the fly likes When to create an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When to create an object" Watch "When to create an object" New topic
Author

When to create an object

James Denver
Greenhorn

Joined: Jan 08, 2013
Posts: 3
Hello,
So I have a question on a concept.
I have read several programming tutorial and some books and the examples they use make it seem like the idea of objects is really simple. "An object represents a real world 'thing'"
that is great on paper but when you are trying to write a program when do you create your own objects(classes) and how do you know you need to? Is it just a matter of re-use?
let's look at an example
I want to write a program that goes to a website takes the first word of the site in the body tag and puts that into a database.
Now in this example it does not seem like you need to create your own objects.
So is the need for objects only based on the complexity of the system?

Just trying to understand...thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

How are you going to "go to that website"? How do you manage the connection? How do you make the request to get the data? How do you store the returned result?

How do you parse the data?

How do you connect to the DB? pass it a SQL statement (and store the aforementioned statement)? get the results back? parse those? display those?

All of these are done with objects. Most of those have already been built for you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Denver
Greenhorn

Joined: Jan 08, 2013
Posts: 3
I understand using object like java.net, what i dont understand is how you know when to create your own....
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

As 'lowercase baba' already has tipped you on the query, I'd just add some nuances.

Lets take GMail.com. When you log in, the program needs to query the database and do all sort of work expected.
Note that, there are more than thousands of users who simultaneously access GMail.com, so definitely, Google has to assign a seperate session for each user. That session stays until you are logged out.
In a particular session, Google is able to manipulate and query data thats relevant to a particular user only.
Things like these require objects, so you know now why and how we know!

People needed only one Sun, so making objects of Sun is not required. In Java, think of it like a static resource shared by all. But people need a lot of other things too which are similar, like fishes, animals, other people, trees, etc, so these are "real-world" objects. Now you know why the definition is so much a textbook!

Objects are nothing but instances of a similar type of entity, to be broad, just as building construction workers (one type), architects (another type), engineers (another type), etc.

Hope that helps...


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Rajdeep Biswas wrote: . . . People needed only one Sun, so making objects of Sun is not required. . . .
You mean it is only necessary to make one Sun object and that is instantiated before our life application is executed. And does that mean we have to get rid of all the stars at night? I think it is more a case that the Sun is a singleton object, rather than its attributes being static to the Sun class.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
"An object represents a real world 'thing'"

Right off the bat, that seems a bad guideline. Many class have no corresponding object in the physical world, and trying to model a process in the physical world by keeping a "1 physical object = 1 Java class" correspondence frequently results in bad design.


Ping & DNS - updated with new look and Ping home screen widget
James Denver
Greenhorn

Joined: Jan 08, 2013
Posts: 3
Ulf Dittmer wrote:
"An object represents a real world 'thing'"

Right off the bat, that seems a bad guideline. Many class have no corresponding object in the physical world, and trying to model a process in the physical world by keeping a "1 physical object = 1 Java class" correspondence frequently results in bad design.


I do not understand why so many texts try to teach OO programming by relating to real world objects (a dog is-a ...wouldnt it make more sense to teach it by real world applicable example ?
If you had to give a definition of what an object is or a rule as to when you should create an object what would that be?
thanks for the feedback
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

James Denver wrote:I do not understand why so many texts try to teach OO programming by relating to real world objects (a dog is-a ...wouldnt it make more sense to teach it by real world applicable example ?
If you had to give a definition of what an object is or a rule as to when you should create an object what would that be?

That's a tough one, but I would perhaps use something like:

A tangible component of your application that has a definable life-cycle and behaviour, and usually carries information.

but I expect a lot of disagreement from my peers ; and you can see that the definition is still quite woolly.

What is 'tangible'? In literal terms it means 'touchable', but an Event isn't touchable. It's most definitely an object though (at least, in the right context it is). It also doesn't really cover the definition of a function (or utility) Class; although it could be argued that these are aberrations themselves.
It also covers an enormous scope of possibilities, from something as simple as a Point to an entire business process.

There are several real-world examples of classes that do mirror 'things' though: User, Customer, Account, Message, Teacher, Student, Seat... and it's generally true that class names are nouns, whereas method names are verbs.
Indeed, one technique used in OO analysis is to search requirements descriptions for precisely those kinds of words.

I think one of the reasons you see classes like Dog and Circle in so many books is that they're used to explain the larger concept of class hierarchies (like an 'Animal' or 'Shape' hierarchy) without having to get bogged down in the minutiae of real-world implementations.

My 2 cents.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Winston Gutkowski wrote: . . . Indeed, one technique used in OO analysis is to search requirements descriptions for precisely those kinds of words. . . .
It is one of the simpler forms of OO analysis, however. I was taiught that as a beginner, but yo uhave to realise that you can only model simple situations like that.
Bertrand Meyer (Object‑Oriented Software Construction, 2/e 2000) says that object‑oriented programming is a misnomer; it should be called class‑oriented programming. The Eiffel® language which Meyer developed and promoted has a keyword for non‑object calls, which sounds rather like Java’s static members.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Campbell Ritchie wrote:It is one of the simpler forms of OO analysis, however. I was taiught that as a beginner, but yo uhave to realise that you can only model simple situations like that.

Hmmm. Not so sure about that, although I'd certainly agree that it only serves as a rough cut. I still use it when I'm at the early stages of design.

The other (and possibly more important) thing it teaches is the importance of complete and well-written requirements documents, including a proper mission statement. How many projects have those these days?

Winston
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2067
    
  22

Everything I learnt about Object Oriented design, I learnt from a comic that showed differrent parts of a computer as little gnomes. There was a little gnome with glasses acting as the CPU, furiously scribbling away. There was a little gnome painting on the screen. There was a little gnome writing to the tape drive, etc, etc

SO, when I have to design a system, I think of little gnomes doing my bidding. My only restriction on my gnomes is that each gnome should do one thing and do it well. So, for example, if I had to build a program that looks at a site and stores the first word in a database, I'll have a worker gnome that is responsible for reading the site, another worker gnome for extracting the first word, and another worker gnome saving to the database, and another boss gnome that directs all the other gnomes. Each gnome corresponds to a high level component. Then I start thinking about if each gnome is doing too much work. If he is, I give him helper gnomes that he can direct. Then I see if maybe some of these helper gnomes can be shared between that worker gnomes. These form my reusable utility classes

This is how I roughly do my design. Not everyone has to do it this way, but I've seen anthromorphizing your components helps. It helps you visualize your design, and it makes the design process a little fun. Actually, there was a design technique that was used in the 90s (I forgot the name), where a team designed a system by acting it out. Each person would play the role of a component, and they will act out a use case by pushing bits of paper between the actors. It helped everyone visualize the whole design
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Winston Gutkowski wrote: . . . it only serves as a rough cut. . . .
That is probably what I meant to write, and didn’t.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Jayesh A Lalwani wrote:SO, when I have to design a system, I think of little gnomes doing my bidding...

You know what? I really like that.

As an old procedural fart, one of the hardest parts about understanding OO (before my 'Eureka' moment) was visualization. I could write decent, modular code, but I always had the feeling I that was "missing something". Wish somebody had told me about gnomes back then.

Winston
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2067
    
  22



Also, anthromorphizing your components helps in determining the class hierarchy. If 2 of your gnomes act very similar, you can start thinking of them as brothers. But first you ask yourself, are they really brothers, or do they really need a helper gnome that they share. First you explore whether there is a IS-A relationship, or a HAS-A relationship. It If they are brothers, you can start visualizing them with similar features, like they can both have the same kind of hat, or a mole on their face, or something.

Sometimes, I wish I was good at drawing so I would put all these gnomes in my head on paper. They would atleast be good for the entertainment value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to create an object
 
Similar Threads
Simple question about OO
Implementing FilenameFilters
whats the difference between Objects and reference variables?
Invoking methods within the same class and from different classes
Problem with equals on collection