1

I want to design a Crate object. A crate is a wooden box that stores things. I need to be able to keep track of of the following things in regards to this crate:

  • length in inches
  • width in inches
  • height in inches
  • weight in pounds
  • price of crate in dollars

My question

can I store all of the above in the same class? Does it break SRP if I store the quantities in the same class?

An alternative, for example will be to break up the class into 3:

  • CrateDimensions (length, width, height)
  • CrateWeight (weight)
  • CratePrice (price)

My specific use case is as follows: I have various many products that can be put into the crate. Based on those products I have code that creates the crate - sets up initial dimensions and weight of the product inside the crate, and if more product is added, the crate dimensions are adjusted, as well as the weight. Price is something that is not currently added, but will be added later.

  • 1
    No, it doesn't. The class still has a single responsibility: Crate. You might want to separate price out (because the crate's identity isn't the only thing that determines its price). – Robert Harvey Jul 24 at 19:43
  • if more product is added, the crate dimensions are adjusted -- This is a stretchy crate? – Robert Harvey Jul 24 at 19:44
  • If price is for just the crate, it could belong here. If it includes the contents it should be elsewhere. Of course, there are often bulk discounts, etc. that make all this messier. – user949300 Jul 24 at 21:46
  • I have dimension (in an Excel sheet) that show how big a crate is to be when it contains a pump product. On that Excel sheet there is an add-on to length dimension if the crate is to also contain a motor to go with the pump. To deal with this I imagine to have an addMotor method that will effectively increase the crate dimensions. I am not sure how else to handle this. I have to add the add-on number to the length in order to accommodate the crate for the motor, so mabe it has to be a stretchy crate object (?) – Dennis Jul 25 at 13:44
5

There's nothing inherently wrong with your design, although as @RobertHarvey points out in the comments, you might want to treat price separately, as that can depend on a lot of things besides the crate itself. That's dependent on your business rules in regards to price, though.

Your Crate class does have one responsibility - describing a crate. Dimensions and weight are pretty integral for describing a physical object, especially when your use case seems to be to use it for containing objects of specified dimensions and weights. From what you've told us, it sounds like a pretty good class.

You might find that you want to extract the set of dimensions into a Dimensions class, which would contain properties for Length, Width, and Height. You could then add some methods / operator overloads to e.g., handle adding two Dimensions together, which I'm guessing you do as you pack items into the crate. Your Crate and Product classes could both use this Dimension class, making your arithmetic a bit less messy.

The use of a Dimensions class doesn't take away from the benefit of having the Crate class hold its own dimensions, whether its as a single property or 3 different ones.

  • Thank you. Can you explain your last paragraph? Are you saying it doesn't matter if Crate holds a Dimension class or if Crate holds three variables length, width, and height individually? – Dennis Jul 25 at 13:49
  • 1
    @Dennis Exactly. How you define the dimensions is irrelevant in deciding whether you should hold the dimensions at all. – mmathis Jul 25 at 13:50
4

Up to now, you've only described attributes, no responsibilities at all. This isn't actually object-oriented, and the SRP isn't applicable. Start by looking at what you want to do in your application, and what roles the crate plays in it. Attributes come later as needed to fulfill these roles. If you find that the Crate object's only responsibility is to hold attribute values for some algorithms to work with, then that's its responsibility, and SRP isn't violated.

  • Excellent point. As described, the Crate is a property-bag, or structure, not a class (yet). – Graham Jul 25 at 12:56
  • Crate seems more like a value object to me - to hold dimensions and weight. There must be another class somewhere in my design with methods like addProduct. Product can be pump, motor, etc that are part of my Product class. I am thinking I will need another class to do operations of adding product to Crate. I don't know if those operations can be part of the Crate object itself. – Dennis Jul 25 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.