Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object oriented Design 6

Status
Not open for further replies.

SPrelewicz

Programmer
Jul 16, 2001
124
US
i apologize if this is sort of semi-perl related, but I figured the best palce to come.

I'm making the step into OOP for my larger web based apps and would like some advice about how others layout their OO Perl apps. I will be using CGI::Application and all its plugins, etc.

My project now is a Sports stats/data site, back-end for entering player data game by game, front on world wide accessible displaying data. [Nothing unusual] i will be using mySQL.

So, I am confused about what to classify and things. I'll need a data layer, but where do i put that and how to let my other classes know about it. Say I have a Player class with position, name, etc...then a Basketball Player class with stats, ability to score point, get rebounds, etc. How then does the data loading/saving into the mySQL DB fit in. Are those methods in the Player class, should they be used to instantiate the object, should I have a separate DATA class I 'use' in all my other classes.

Also, in OO design, is it common to create an array of many objects at once, as in a roster of basketball player objects, and if so how does this affect performance?

Thank you in advance for your time.

Scott

About me at Flying Roman.com lyric meaning and discussion
Prelewic, Renassance Man ;)
 
so the "item" (dog) you create for your purpose is the "Object" , but you say it is of class "dog", i thought eyes were a class, legs were the class...

are you saying the object is what i end up with from this senario...

I take the "Body" item of class "Car" and add the "Legs" item of class "Dog" and I end up with an "Object" which is a "Car with dogs legs"...

i then use the methods of each class to change the colour of the card body or move the legs of the dog class.

the item i am controlling is the object

or am i now just going mad! bark raving mad :p

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
-->so the "item" (dog) you create for your purpose is the "Object" , but you say it is of class "dog", i thought eyes were a class, legs were the class...

These things are components of the dog. They are separate objects that put together make up the class dog. They are separate classes/objects in their own right.

-->I take the "Body" item of class "Car" and add the "Legs" item of class "Dog" and I end up with an "Object" which is a "Car with dogs legs"...

Sort of. The body item is its own class/object.

Class CAR:
Has a body
Has 4 wheels
Has a steering wheel, etc #Each their own objects of their own class

My Car: [my $awesome_car=new Car();]
Gets all the things from the car class, but specific things.
I have a red body # which can be changed
I exist now to be manipulated, acted upon.

You're getting close.

About me at Flying Roman.com lyric meaning and discussion
Prelewic, Renassance Man ;)
 
so the leg is an object of class dog, and the leg is of class leg, and of the object dog, but i could have an object dog of class dog with legs of class chair, which is the legs class of an object chair.

ending up with an oject dog of class dog with chair legs.

WTF - I'm going mad, no actually i think i've already gone!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I can't seem to get this concept across.
For all objects you want to use there has to be a design of that object. Imagine a drawing of the car and that is the class. You don't buy the drawing, you buy a car (as do many other people).
The car manufacturer (perl if you like) creates cars from the design. Every one is identical.
If you want a different car you need a different drawing (design or class). Just because you have a different one, doesn't mean that you have to ditch the first one.
You can have many elements to this car design too and each one might be re-usable. You might use exactly the same door mirrors on the second design as you do the first so you simply use the mirrors from the first in the design for the second.

Every object you want to use has to have a design which is the class, therefore you always have both.
A class without an object instance is a drawing of a car with no car.
The car cannot exist without a design.


Trojan.
 
-->so the leg is an object of class dog, and the leg is of class leg, and of the object dog, but i could have an object dog of class dog with legs of class chair, which is the legs class of an object chair.

You could, I suppose, but you wouldnt.

## THIS IS MY DOG CLASS
package Dog;

sub new {
$dog->{right_leg}=new DogLeg();# Give dog some dog legs
# Give dog some dog fur
# give dog a tail

# give dog a name
my $name=shift;
$self->{name}=$name;
}

sub new_name {
my $new_name=@_;
$self->{name}=$new_name;
}

# Dont care how Leg is implemented, just need to know how to # inertact with it.

package main;
my $doggy=new Dog('Spanky');
$doggy->new_name('Rover');
 
Sorry, forgot to mention -

package main;
my $doggy=new Dog('Spanky'); # $doggy is an object instance of Class Dog
$doggy->new_name('Rover')

 
so the object is the container / the finished product, it is made up of it's own class, which is what defines that object, which can be made up of classes from other object classes.


i think i do understand you.

just finding it hard going bare with me :)


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
What's with the $self-> ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
This business about the leg being an object of class dog is rubbish. Sorry but it has to be said because it causes confusion.
You could have a class called "dog" and a class called "leg" and you could create an instance of the class called "dog" which IS a dog and it could contain FOUR instances of the class "leg" which ARE LEGS!.
The object instance is, by definition, an object of that class.
If you drew a car and manufactured it you would NOT end up with a door. You would end up with a CAR.
car = car
dog = dog
leg = leg
dog DOES NOT EQUAL leg.

Does that make it clearer?

Wow, I think I'm getting tired here!
:-(


Trojan.
 
--> This business about the leg being an object of class dog is rubbish. Sorry but it has to be said because it causes confusion.
You could have a class called "dog" and a class called "leg" and you could create an instance of the class called "dog" which IS a dog and it could contain FOUR instances of the class "leg" which ARE LEGS!.
The object instance is, by definition, an object of that class.
If you drew a car and manufactured it you would NOT end up with a door. You would end up with a CAR.
car = car
dog = dog
leg = leg
dog DOES NOT EQUAL leg.

Does that make it clearer?



I'm trying to get that across as well, maybe not so well. I didnt mean to imply that DogLeg is an object OF class Dog, but that Class DOG would CONTAIN legs, via instantiation WITHIN the Dog Class. I think if you go back you'll see that I used the word CONTAIN often in examples. What I was trying to get through is the idea of contian relationships, ie "HAS A" and that a property of a class is often an object instance of another.
 
wow that's as clear as mud

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Sorry, I'm loosing track of who understands and who doesn't.
It seemed like such an obvious concept to me but it's taken ages to get across.

I agree with your description and if you read my comments you will see that I was also suggesting a "has a" reslationship as you were but I didn't want to go into details since it's been hard enough getting across the simple class / object relationship.

I think I need a scotch!






Trojan.
 
This thread has probably deteriorated beyond my original intention. I'm not helping cause I'm good at teaching, and am probably using terminolgy I use for myself to help myself understand that isnt helping. Try this:


Also, if you go to Brown Universities CS department website, they have some great material in the lower level courses on this. Its where i pulled most of my examples from that I used above.
 
so when i define an object of class CAR i get a CAR, which is made up of the class car which is all it's bits.

I then i use that classes method to change the attributes. colour etc...

but surely i could also have an object which is made up of parts (classes) of other objects can't i ?

making a hybrid object.

or am I making a hybrid class, which I then define my object as.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Sorry, I meant NOT good at teaching.

And I agree totally trojan ;) Pass the Scotch!
 
--> but surely i could also have an object which is made up of parts (classes) of other objects can't i ?


Yes, exactly, but the opposite ;) You have something that is made up of objects of other classes
 
Your "car" class can use as many objects as it likes.
It could use an array of "door" objects, an array of "wheel" objects and any number of other objects.
It is still a car class and you would still create car objects using it but if you wanted to re-use the "wheel" objects in say a "motorbike" class then GREAT! You just got one of the main points of OO design! Code re-use! :)

Phew!
More scotch please! :)


Trojan.
 
I'm determined:

Classes -
Car
Wheel
Engine
Bumper

Objects:
Object My Awesome red Car -
Has 4 Wheel Objects
Has a Gas Engine Object
Has a Plastic Bumper Object

Object My old green junker:
Has 3 Wheel Objects # 1 fell of
Doesnot have a engine
Has a Steel Bumper Object

Maybe simplify:
Class is the .pm file as in Car.pm, Wheel.pm, etc.

Object is actually using that .pm file -
my $car_i_am_instantiating=new Car();

#Then within Car.pm class, there is the code to put the wheels on, add the bumper, paint it, etc...
 
I didn't find the scotch but I did find the Special Brew!
Much happier now. :)

1DMF, do you understand now do you think?


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top