[Newbie] design question

I

idaku2

Hi all!

Suppose I have this two classes:

class ShoppingCart {
addBook(Book book){}
removeBook(Book book) {}
getPrice()
buy()
....
}
and

class Buyer {
private ShoppingCart cart;
spendMoney()
buy();
....
}

When shoppingCart's buy() is called, cart must remove all of it's
books, and the caller gets the price of the books. What is the proper
design here: should buyer know in which order class ShoppingCart works
(cart.getPrice, cart.buy() in which cart remove all the books), or
should buyer call cart.buy, cart.getPrice (order again), and then
buyer would remove all the books through for loop, or should I use
somethin like this
cart.buy(Buyer buyer), and inside cart.buy cart calls
buyer.spendMoney()?

Thanks in advance
 
T

TobiMc3

Hi all!

Suppose I have this two classes:

class ShoppingCart {
addBook(Book book){}
removeBook(Book book) {}
getPrice()
buy()
...}

and

class Buyer {
private ShoppingCart cart;
spendMoney()
buy();
...

}

When shoppingCart's buy() is called, cart must remove all of it's
books, and the caller gets the price of the books. What is the proper
design here: should buyer know in which order class ShoppingCart works
(cart.getPrice, cart.buy() in which cart remove all the books), or
should buyer call cart.buy, cart.getPrice (order again), and then
buyer would remove all the books through for loop, or should I use
somethin like this
cart.buy(Buyer buyer), and inside cart.buy cart calls
buyer.spendMoney()?

Thanks in advance

I'd say, that it really depends on what your are designing overall. I
mean, the ideal situation is that the classes know as little about the
internals of the other as possible, and that they are loosely coupled.

Tobi
 
V

visionset

Hi all!

Suppose I have this two classes:

class ShoppingCart {
addBook(Book book){}
removeBook(Book book) {}
getPrice()
buy()
...
}
and

class Buyer {
private ShoppingCart cart;
spendMoney()
buy();
...
}

When shoppingCart's buy() is called, cart must remove all of it's
books, and the caller gets the price of the books. What is the proper
design here: should buyer know in which order class ShoppingCart works
(cart.getPrice, cart.buy() in which cart remove all the books), or
should buyer call cart.buy, cart.getPrice (order again), and then
buyer would remove all the books through for loop,

Yes this one
Then remove the buy() method from the cart, it doesn't buy, it removes items
you said.
But really as mentioned a fuller requirements spec is required to make these
kind of design decisions.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top