Composite Design Pattern, what it is?

Imagine wandering through a detailed virtual world: cities bustling with NPCs, forests with trees of varying sizes, and inventory bags filled with unique items. How do game developers keep track of such complex, hierarchical structures? The hero of our tale is the Composite Pattern. Let’s embark on a journey to understand its magic and see it in action within Unity using C#.

Unveiling the Composite Pattern

The Composite Pattern is about treating individual objects and their compositions in a unified manner. Picture a single tree leaf and an entire forest. With the Composite Pattern, both can experience a gust of wind equally.

abstract class Component
{
    public abstract void Operation();
}

class Leaf : Component
{
    public override void Operation()
    {
        // Specific operation for leaf
    }
}

class Composite : Component
{
    private List<Component> _children = new List<Component>();

    public override void Operation()
    {
        foreach (var child in _children)
        {
            child.Operation();
        }
    }

    public void Add(Component component)
    {
        _children.Add(component);
    }

    public void Remove(Component component)
    {
        _children.Remove(component);
    }
}

Real-World Scenarios in Video Games

Inventory Systems

A gaming backpack can hold individual items or pockets filled with more items. If you’ve ever wanted to calculate the total weight or check the presence of a particular item, the Composite Pattern is your friend.

AI Behavior Trees

Complex AI actions are often the result of behavior trees. Each node might be a simple action or a composite of other behaviors.

Game Levels and Sublevels

Consider a game with multiple levels, where each level has sublevels or stages. Each stage might contain its own challenges, enemies, and rewards.

Dialogue Trees in RPGs

Role-playing games (RPGs) often feature intricate dialogue trees where a player’s choice can lead to various conversation paths. Each path can further branch out based on game progression, player’s attributes, or previous choices.

Skill Trees

Many games offer skill trees where players can unlock and enhance abilities. Some skills may have prerequisites, and branching choices determine a player’s progression.


The Art of Crafting Inventories: Diving Deeper

Inventories are often the backbone of many games, especially role-playing games (RPGs). They hold items that players collect, earn, or buy. At a glance, an inventory system might seem straightforward, but when you consider nested items, such as pouches containing other items or even smaller bags, the complexity can ramp up quickly.

The Essence of Uniformity:

Why is the Composite Pattern a savior here? It’s all about uniformity. Whether you’re dealing with a single potion, a pouch containing multiple potions, or a chest filled with several pouches, each entity in the inventory should respond to common actions like “calculate weight” or “check item count” in the same way.

Imagine the chaos if you had to write separate logic for each type of entity in the inventory! With the Composite Pattern, you have a unified approach, reducing code complexity and potential errors.

abstract class Item
{
    public abstract int GetWeight();
    public abstract int GetItemCount();
}

class SingleItem : Item
{
    private int _weight;

    public SingleItem(int weight)
    {
        _weight = weight;
    }

    public override int GetWeight()
    {
        return _weight;
    }

    public override int GetItemCount()
    {
        return 1;
    }
}

class Container : Item
{
    private List<Item> _items = new List<Item>();

    public override int GetWeight()
    {
        int totalWeight = 0;
        foreach (var item in _items)
        {
            totalWeight += item.GetWeight();
        }
        return totalWeight;
    }

    public override int GetItemCount()
    {
        int totalCount = 0;
        foreach (var item in _items)
        {
            totalCount += item.GetItemCount();
        }
        return totalCount;
    }

    public void AddItem(Item item)
    {
        _items.Add(item);
    }

    public void RemoveItem(Item item)
    {
        _items.Remove(item);
    }
}

// Creating individual items
SingleItem book = new SingleItem(1); // A book with weight of 1
SingleItem pen = new SingleItem(1); // A pen with weight of 1
SingleItem notebook = new SingleItem(2); // A notebook with weight of 2

// Creating a container and adding items
Container box = new Container();
box.AddItem(pen);
box.AddItem(notebook);

// Displaying weight and count for the box
Debug.Log($"Box weight: {box.GetWeight()}");
Debug.Log($"Box item count: {box.GetItemCount()}");

// Creating another container and adding items and previous container
Container bigBox = new Container();
SingleItem chair = new SingleItem(10); // A chair with weight of 10
bigBox.AddItem(chair);
bigBox.AddItem(box);

// Displaying weight and count for the big box
Debug.Log($"Big box weight: {bigBox.GetWeight()}");
Debug.Log($"Big box item count: {bigBox.GetItemCount()}");

// Removing an item from the big box and displaying updated weight and count
bigBox.RemoveItem(chair);
Debug.Log($"Big box weight after removing chair: {bigBox.GetWeight()}");
Debug.Log($"Big box item count after removing chair: {bigBox.GetItemCount()}");

By extending our inventory with a GetItemCount method, we further demonstrate the power of the Composite Pattern. Regardless of whether we’re querying a single item or a container, the interface remains consistent, and the logic is encapsulated within each class.

This complete code snippet represents a use-case of the composite pattern to manage a hierarchy of items, both individual (SingleItem) and grouped (Container). The code shows how one can add items to a container, nest containers, and remove items from them while still being able to easily compute total weight and count. The comments should help provide context throughout the source code.


A Glimpse into the Future: Crafting UI Trees

Imagine a user interface (UI) where you have folders, and inside those folders, you have items or even subfolders. Sounds similar to our inventory, right? This is yet another perfect scenario for the Composite Pattern.

Stay tuned for our next post where we’ll dive deep into creating a UI tree element using the Composite Pattern, making the management and interaction of UI elements a breeze in Unity with C#!