Khal
Khal

Reputation: 39

Which class should do the work?

I am building a Sales system for my dissertation and I keep debating with myself in my design, which class should be doing the work?

So from these 3 classes;

Facility Class - This class is a central class to coordinator between the others and the GUI

Product Class - Represents details of the product

Sales Class - Represents the sale of multiple items

When it comes to selling the products, my big question is:

"Which class should calculate the total price". The facility class or the Sales class?

Whats best practice?

Thanks in advance

Upvotes: 1

Views: 73

Answers (3)

morgentau
morgentau

Reputation: 16

My first impulse is to order these classes by their responsibility regarding Model-View-Controller design (because you mention a GUI and an interfacing class to it).

  • Facility - according to your statement it is a controller, it connects the GUI (view) to your data (models).
  • Product - this is a model class, since it holds data and business logic.
  • Sale - this class is connected to multiple Products and multiple Products share the same Sale, because a product can be sold more than once (this is an assumption) and a sale can include multiple products. It is also a model class.

Since the Sale class represents multiple products and you are interested in (e.g.) the sum of the individual prices of these products, this would be the place to store that method.

Upvotes: 0

thst
thst

Reputation: 4602

if taking MVC into account, the Model is Product and Sales, the Controller is Facility and View is not mentioned.

If this should be a properly designed solution, you will add a service layer, that will do the actual work.

So the Model keeps the data, the view shows the data, the controller orchestrates the data between Model and View and a service layer that will do business logic on the data (booking).

There is also a variation of this, where the view has a specific view model class, that will do the preparation of data and collection of data specificially for the view. but for your case, this is not of importance.

Upvotes: 1

DripDrop
DripDrop

Reputation: 1002

I think that, as a general rule, the Facility, or manager class should always act simply as a buffer between the user and the other classes, which leads me to say you should put the total price calculation in the sales class.

The sales class is the one that, in the first place, was designed to handle the sale of multiple items, meaning there should be a method that returns the price of all of the currently carted items combined, and then pass it to the interface class to be displayed.

Upvotes: 0

Related Questions