Rafał Telaczyński Rafał Telaczyński - 3 years ago 178
Java Question

Java - Data Structures

I came across a Java Data Structures task. I'm looking for a confirmation that I understood it correctly and that my approach is right.

The task:
A product can have many different types of attributes, and they can vary between products, e.g. a Bike can have the following attributes: {price, color, size} and the product Kitchen table: {price, width, depth, height}. Product Attributes are organized in a hierarchical group structure, where a Product Attribute Group can contain one or several Product Attributes and/or Product Attribute Groups.
Write the code needed in java to handle the above described products and a print function that prints the data in such a way that it is clear which attributes belong to which product and which attribute group if any.

There was an image attached with notation:


Figure 1: Note that the order is important. In the figure above Attributes 1, Attribute 3 and Attributes 2 all belong to the Product Attributes group.

So my approach would be to create an abstract Product class, and then make two subclasses(Bike, KitchenTable) extending from Product class. Is it correct? Or is it about something entirely different?

Answer Source

Please have a look at the Composite Pattern.

The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. This matches your task to have product attributes that have to be organized in hierarchical groups.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download