[Newbie] design question

I

idaku2

Hi all

Suppose I have class ShoppingCart which has one method called buy(),
and class Buyer who has one reference to ShoppingCart... Can I also
have method buy() in class Buyer, which will then called
ShoppingCard.buy(), and also do some other stuff? Is this legal
design pattern, have methods with same name?

Thanks in advance.
 
D

Daniel Nogradi

Suppose I have class ShoppingCart which has one method called buy(),
and class Buyer who has one reference to ShoppingCart... Can I also
have method buy() in class Buyer, which will then called
ShoppingCard.buy(), and also do some other stuff? Is this legal
design pattern, have methods with same name?

Yes, something like this is perfectly fine.

class ShoppingCart( object ):
def buy( self ):
print 'buy in shoppingcart'

class Buyer( object ):
def __init__( self, cart ):
self.cart = cart

def buy( self ):
print 'buy in buyer'
self.cart.buy( )
print 'some extra stuff'

acart = ShoppingCart( )
abuyer = Buyer( cart=acart )
abuyer.buy( )


HTH,
Daniel
 
B

Bjoern Schliessmann

Suppose I have class ShoppingCart which has one method called
buy(), and class Buyer who has one reference to ShoppingCart...
Can I also have method buy() in class Buyer, which will then
called ShoppingCard.buy(), and also do some other stuff? Is this
legal design pattern, have methods with same name?

In principle, this is legal.

But OTOH, how could a ShoppingCart "buy" something? In my world,
Buyers "buy" when using ShoppingCarts.

Regards,


Björn
 
I

idaku2

In principle, this is legal.

But OTOH, how could a ShoppingCart "buy" something? In my world,
Buyers "buy" when using ShoppingCarts.

Yes, I don't know either. I got this assignment for my homework, and
in UML diagram class ShoppingCart has method buy(), and the class
Buyer doesn't have any methods, only attribute ShoppingCart, and I
must simulate/implement online shopping. In my world buyers buy too,
just wanna check with somebody with more experience. :)

Thank you both.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Yes, I don't know either. I got this assignment for my homework, and
in UML diagram class ShoppingCart has method buy(), and the class
Buyer doesn't have any methods, only attribute ShoppingCart, and I
must simulate/implement online shopping. In my world buyers buy too,
just wanna check with somebody with more experience. :)

It's alas pretty common to see OO taught by persons who'd rather do some
other job - preferably not related to computers.
 
G

Gabriel Genellina

En Sun, 13 May 2007 11:40:16 -0300, Bruno Desthuilliers
It's alas pretty common to see OO taught by persons who'd rather do some
other job - preferably not related to computers.

If I had to name my worst class at university, it was the first one about
OO. The teacher really had no idea of what he was talking about - and you
didn't have to be a genius to notice it. After a few weeks I moved on to
an alternate class. Next semester there was a different teacher.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top