• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to create an object

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Denver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand using object like java.net, what i dont understand is how you know when to create your own....
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"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.
 
James Denver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . it only serves as a rough cut. . . .

That is probably what I meant to write, and didn’t.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic