Win a copy of Spring in Action (5th edition) this week in the Spring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Product and Inventory class design  RSS feed

 
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read a few articles with the same sentiment, which is to avoid getters and setters:
https://dev.to/scottshipp/avoid-getters-and-setters-whenever-possible-c8m

I need to create two simple Product and Inventory classes.

My idea with the Product class is to have the following class structure and to only expose one public update method:



How do I go about creating the update method? Do I:

1. Create multiple overridden methods which take the productId as the first parameter, and then provide the updated field as the second parameter?
2. Create multiple update methods? But that is just setters with another name.
3. Do something else entirely?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it so important you don't expose getters?

I prefer Joshua Blochs stance.. getters are fine but if you return something that could be mutated make an immutable copy.

At any rate why does the product object need an update method at at?

Won't this product data be backed by a database table or similar? Or is this not a Web application?
 
Marshal
Posts: 61715
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem with getXXX methods. If they allow a mutable reference type to escape your object, that is prone to manipulation elsewhere and that can be harmful. I think that link you showed is exaggerating a bit. So you are going to fill up the Car with Fuel?That will have to produce a WrongFuelException when you find you have a PetrolEngine object
Yes, you do have to be careful that getXXX methods don't expose implementation details. But a Car object should show how much fuel it has left and what sort it is. Consider what information you would want from a product. You would want a stock number, a description, and a price. It is reasonable to provide those via getXXX() methods.
And, you don't have any mutable fields. You have two Strings, which can be safely shared, one primitive field which is the wrong type (double) and one primitive field which should probably be in a different class.
Don't use double arithmetic for money, including prices. Use BigDecimal. But apart from that, there is nothing wrong with returning those fields. Nobody is going to be able to change the price from elsewhere, never mind what they do to the returned value. Or denominate the price in cents and use integer arithmetic. Like that, again you can't do any harm with a getPrice() method.

Why have you got a quantity field? I may be mistaken, but am not sure that belongs in this class. An object doesn't usually know how many “brothers and sisters” it has. I would have thought you have an inventory or store class which will hold several products and amounts, but I may be mistaken. Maybe there should be an intermediate class between Product and Inventory, encapsulating one single product and quantity. That surrounding class can have getXXX() methods to return the price of its product, etc.
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beeimbolo Paul: Why is it so important you don't expose getters?
Primarily to expand my knowledge base.

Campbell Ritchie:
All valid points, thank you.

The requirement given to me, includes the quantity inside the Product class. I know it makes no sense. I will clarify that point, as I am being asked to create a solution with a broken requirement.

For argument's sake, is my idea of an overloaded update method way off the mark here, or would you simply use setters? Is that the right tool for this job?

Also, I'm trying to expand my skill set here, so getters and setters doesn't expose me to any new, or more "professional/industry standard", way of thinking.
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:. . . The requirement given to me, includes the quantity inside the Product class.

I think you have been given an over‑specified assignment, something I don't like. It deprives you of the opportunity to design the app, and may even allow the teacher to introduce errors into your work,

I know it makes no sense.

Well done realising that

I will clarify that point, as I am being asked to create a solution with a broken requirement.

Good idea, but it is usually impossible to change an assignment after it has been issued.

For argument's sake, is my idea of an overloaded update method way off the mark here, or would you simply use setters? Is that the right tool for this job? . .

Don't know. What if you have sell() and buyIntoStock() methods or similar? You would of course need a getNumberInstock() or similar method.
 
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to comment on something that is already bin covered, but keeping track of your products really really dosn't make sence.
By doing so you would have to update the quantity of every object everytime a new product is created.
My guess is that the teachers intention was to make product a parrent and there should be a third class, shopItem or something, extending product.
I'm not agreeing with this design, just pointing out...
 
Daniel Demesmaecker
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it as inventory at the top, which contains products (like tv or computer) and thoose would have childern and grandchildern by for example brand and model.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:For argument's sake, is my idea of an overloaded update method way off the mark here, or would you simply use setters? Is that the right tool for this job?


What exactly is that "job" though? How would multiple update methods contribute to completing this job?

Andy Hunt's Rule #1: Always consider context.

In what context will this Product object be used and how would an update method be involved in that use? Until you give a better idea of the context, it's difficult to tell what kind of issues there actually are with your proposed design choice. Show us some more code, preferably, code that shows how you intend to use Product and the Product.update() method/s you are thinking of providing.
 
Daniel Demesmaecker
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on his class design. I'm with Ritchie, I would not use a single update method, you should be able to update a single property so I would provide getters and setters.

Now that I see that product has a price, strike my whole remark from before, if product was a parrent, it wouldn't make sence to keep the price there, since not everything is the same price
How about instead of only posting the code also post the requirements, I really think there must be some form of misinterpretation
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . you should be able to update a single property so I would provide getters and setters. . . .

That is of course the book I am going to win here, so I can have two copies Go to that forum and thwart my evil plan by winning a copy yourself.
But are you ever going to change its description, or its code number (=ISBN)? Maybe you are not even allowed to change the price (it has $54.99 written on the back cover). So the only field you can change is the number in stock. I would prefer to change that via sell() or buyIntoStock() methods.
 
Wanna see my flashlight? How about this tiny ad?
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!