Component based instead of inheritance based Inventory

Hey all,

Been a while since I posted, busy bee.

But now I have design question relating to inventory system design, of which I currently can see 2 options.

1. Inheritance based approach

The fairly stock standard approach where there is a base item class and from that subclasses are inherited and interfaces applied for additional functionality.

public class Item
{
int id;
string name;
}


public class Food : Item, IConsumable
{
public void Consume()
{
}
}

2. ‘Attribute’ component based approach

Now this is something I am very interested in trying as it seems to be a very modular way of implementing things with a singular ‘Item’ class and then many different sub attribute classes that act as additional components to the base item class.

A very simple mock-up, I put together in about 5 mins :

public class Item
    {
        string name;

        int id;

        List<ItemAttribute> attributes;

        public bool HasAttribute<TItemAttribute>() where TItemAttribute : ItemAttribute
        {
            for (int a = 0; a < attributes.Count; a++)
            {
                if (attributes[a] is TItemAttribute)
                {
                    return true;
                }
            }
            return false;
        }

        public void AddAttribute<TItemAttribute>() where TItemAttribute : ItemAttribute
        {
            if (!HasAttribute<TItemAttribute>())
            {
                TItemAttribute newAttribute = new ItemAttribute() as TItemAttribute;
                attributes.Add(newAttribute);
            }
        }

        public TItemAttribute GetAttribute<TItemAttribute>() where TItemAttribute : ItemAttribute
        {
            for(int a=0; a<attributes.Count; a++)
            {
                if (attributes[a] is TItemAttribute)
                {
                    return attributes[a] as TItemAttribute;
                }
            }
            return null;
        }

Then each attribute can be added and called as required as well as their corresponding functionality.

Pros

  • Appears to be a more modular design with less codependancy.
  • Ease of addition and removal of ‘components’

Cons

  • Unsure of speed of operations (I’m not sure it is that relevant)
  • Difficulty of serialization

Would love to hear any thoughts/suggestions on this.

(Sorry it is such a rough post, had to make it quick)

  • Myhi

James, have you looked into the Decorator Pattern? It looks similar to what you’re trying to do. The second method is better than the first, IMO, but I’d think it’d expand(badly), quickly, once you get into sorting functionality in items.

landon91235 just beat me to the punch about the Decorator pattern. You’ve essentially implemented it in your example code, but identifying the pattern explicitly will make it easier for others to comprehend quickly, and it can serve as a reminder if you step away from the code for a while.

This includes adding and removing at runtime, which is more difficult when properties are hard-coded into inherited subclasses in inheritance trees.

I agree that speed of operations usually isn’t relevant with inventory. It shouldn’t be difficult to serialize, either. Inherited classes have their own serialization issues anyway.

A component-based approach looks like the better tool for the job.

1 Like

Thanks for the insight guys, I have used the decorator pattern before just I am never good with the real technical names of things, often find I am using patterns without even realising :stuck_out_tongue: . Just was comparing the options here, just because I see a lot of people treat the hard written inheritance and interface tree still as a viable option and I think that made me think ‘something must be wrong with this seemingly better way to do things’. But I’m not 100% sure I would call it a decorator pattern, unless I’m just getting two things mixed up that are essentially the same.

After using the Service-Locator pattern, a design supposedly to ‘replace’ singletons, it is basically similar in it’s component based structure.