0

I am working on the design for a piece of software that manages items.

The software has multiple product types - each with their own SKU and physical properties, the user dynamically adds these product types.

The software also has items (also dynamically added)- each item belongs to a product type (inheriting its specific properties). When a user adds an item they need to be able to choose the product type, the user can also add additional properties such as if the item is broken, open, or new and other properties.

In my current design I have a class of ProductType, that has fields for all the properties for a product type. I also have a class of item that has fields for the additional properties.

I am confused on how to get an object of class Item to inherit the properties of a specific object of class productType. Any advice would be appreciated. The design is in its first revision, so it can be changed pretty easily.

My first thought would be to store an array of ProductType globally, and then when an item is created use a function to copy the fields. Would this work or is there a better way?

SimoV8
  • 1,382
  • 1
  • 18
  • 32
Reid
  • 4,376
  • 11
  • 43
  • 75
  • You can use simple class hierarchy? public class Item **extends** ProductType { } – Christopher Rivera Aug 14 '15 at 20:28
  • Yes I was thinking about that, but would I just manually add in the field values? – Reid Aug 14 '15 at 20:30
  • well do you have a web site running that can fill those up? maybe you should check out spring mvc and see how you can build easy jsps and forms to fill up your objects – Christopher Rivera Aug 14 '15 at 20:31
  • What if your base class ProductType just had a field for Item? Or a collection of items? If the properties are generic (like key values) why bother with inheritence? If all children of ProductType need an Item object(s) then could youjust add it as a field to ProductType? – Mark Giaconia Aug 14 '15 at 20:31
  • For better help sooner, post an [mcve]. There is not enough information in your question for us to be able to help you. Thanks! – durron597 Aug 14 '15 at 20:31
  • 2
    Sounds like you could use composition instead of inheritance, because an item _has a_ product type (not _is a_ product type). – Mick Mnemonic Aug 14 '15 at 20:31
  • @Reid your `ProductType` class would hold all fields you know will be commonly shared between inheritors. – Timothy Frisch Aug 14 '15 at 20:31
  • @durron597 I can not post code, as I am currently in a design phase. – Reid Aug 14 '15 at 20:34
  • @MickMnemonic Could you provide some explanation, I have never heard of composition. – Reid Aug 14 '15 at 20:35
  • Basically it just means that your item would have a product type attribute (field). [Prefer composition over inheritance](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) and [the Wikipedia article](https://en.m.wikipedia.org/wiki/Composition_over_inheritance) are definitely worth reading. – Mick Mnemonic Aug 14 '15 at 20:41

3 Answers3

2

I think the best solution for your problem is using composition: the type is a property of Item.

public class Item () {
    private final ProductType type;
    // other properties

    public Item(ProductType type) {
        this.type = type;
    }
}
SimoV8
  • 1,382
  • 1
  • 18
  • 32
0

public class Item extends ProductType{}

cadams
  • 1,299
  • 1
  • 11
  • 21
  • Yes but how would I get the values of a specific product type into the item, would I just manually copy? – Reid Aug 14 '15 at 20:31
  • Fields do, but as I understand the contents of fields do not copy when you create a new object. – Reid Aug 14 '15 at 20:36
0

You should not copy the fields, but reference the ProductType. You also should not access the ProductType's fields directly, but only through getter methods, and if you want to "inherit" the fields, you should add delegation methods to your Item class.

public class ProductType {
    private String typeName;
    public ProductType(String typeName) {
        this.typeName = typeName;
    }
    public String getTypeName() {
        return this.typeName;
    }
}

public class Item {
    private ProductType productType;
    private String      itemName;
    public Item(ProductType productType, String itemName) {
        this.productType = productType;
        this.itemName = itemName;
    }
    // Access to ProductType object (optional)
    public ProductType getProductType() {
        return this.productType;
    }
    // Delegated access to ProductType field
    public String getTypeName() {
        return this.productType.getTypeName();
    }
    // Access to Item field
    public String getItemName() {
        return this.itemName;
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247