Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to know what Object to use.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is this:

How do I know when to use different objects. (these object are the ones that I made, not objects that others have made)

for example:

when my company switched over from an older language to a newer language, we were told to use 'objects'. I was thinking that each customer transaction would be an object. But instead, we are using a group of sales (dataset) as an object and this object is sent to the printer. (instead of each individual sale being sent to the printer)


another example:

when creating a Conway's Game Of Life (GOL) program, I am thinking of using each cell as an object, but I have seen other's GOL programs using the entire grid as an object. (for those unfamiliar with Conway's Game of Life, it is a 2 -dimensional grid of 'live' or 'dead' cells. a simple set of instructions tells each cell whether they are 'alive' or 'dead'. usually live cells are white and dead cells are black.)


How do I know when it's better to use an individual cells as an object or the entire group of them as an object, or use something entirely different.


How do I know which 'object' is the correct one to be coding?


thank you.

 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me take a step back here, why don't you start with "what is object"?
Then you will be able to judge what object to use.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both cases, as in any real-world program, multiple different kinds of objects can be defined and can exist together. For example, in the Life case, you can define a Board class and a Cell class, and the Board can be made up of a group of Cells. Which class you use at any given time depends on what you're trying to do, what steps you're executing, what you're modeling, just like in real life. If you're building a house, you wouldn't say, "One person said I should use floors, but somebody else said I should use walls." Each one has its job to do, and you use it as appropriate for the job at hand.

How do I know when it's better to use an individual cells as an object or the entire group of them as an object, or use something entirely different.



If you're at the part in your program where your algorithm tells you to do something to a cell, like turn it on or turn it off, you'll operate on a Cell object. If you're at the part of your program where your algorithm says to do something to a group of cells, such as, "count the number of neighboring cells that are currently alive," you would use an object that represents a group of Cells. Of course, the GroupOfCells object's countAlive() method would in turn operate on individual Cell objects, one by one, but the user of that class doesn't need to know or care about that detail.

Finally, note that you're really asking which class you should be using, not which object.
 
Steve Jogan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that does make sense.

what we are doing indicates what we need.

(although I am still learning "what are we doing" and "what do we need")

Thank you
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where you use the word "object", you really mean the word "class".

In programming, there are always multiple ways to solve a problem. There is not one solution that is the one and only correct solution. Each possible solution has its own pros and cons. For example, you might have a solution that runs fast, but that uses a lot of memory, or another one that runs slower but uses less memory. One solution might make it easy to extend the program with new functionality, while another solution makes it harder to extend the program. Etcetera - there are always tradeoffs to be made.

What is good design and what is bad design is hard to explain in general. You learn through experience what works and what doesn't work. There are books about OO analysis and design which might help you with this.

If you want to practice with this, try taking one problem, the Game of Life for example, and create different programs that do the same thing but in different ways. You'll learn a lot about how you can design programs in different ways.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:
If you want to practice with this, try taking one problem, the Game of Life for example, and create different programs that do the same thing but in different ways. You'll learn a lot about how you can design programs in different ways.



I've found that one of the best of examples of this, for me at least, has been the Euler Project. It's very eye-opening to see how many different ways other people solve the same problem.
 
It's a tiny ad. At least, that's what she said.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic