Composite Pattern

In this unit, we will delve into the Composite pattern, a pivotal structural design pattern that facilitates treating individual objects and compositions of objects in a unified manner. This pattern is particularly useful when you need to represent part-whole hierarchies, and you want to interact with those hierarchies in a consistent manner.

What You'll Learn

In this lesson, you will master:

  • The core concept of the Composite pattern.
  • How to implement the Composite pattern using a real-world example involving employees in a company.
  • The significance and benefits of using the Composite pattern in software development.

Let's explore the Composite pattern through a practical example.

Implementing the Composite Pattern

Our example will focus on an organizational structure where we have different types of employees, such as Developers and Managers, and we want to group them within a company directory. This example will help you understand how to manage a collection of objects in a tree structure.

Step 1: Define the Component Interface

First, we define the Employee interface, which declares a method for showing employee details.

interface Employee {
    fun showEmployeeDetails()
}

In this example, Employee is the component interface that declares the showEmployeeDetails method. All concrete employee classes will implement this interface.

Step 2: Implement the Leaf Components
Step 3: Create the Composite

The CompanyDirectory class will implement the Employee interface and represent the composite structure that contains a list of employees.

class CompanyDirectory(val directoryName: String) : Employee {
    private val employeeList: MutableList<Employee> = mutableListOf()

    override fun showEmployeeDetails() {
        println("Company Directory: $directoryName")
        for (emp in employeeList) {
            emp.showEmployeeDetails()
        }
    }

    fun addEmployee(emp: Employee) {
        employeeList.add(emp)
    }

    fun removeEmployee(emp: Employee) {
        employeeList.remove(emp)
    }
}

The CompanyDirectory class acts as a composite by maintaining a list of Employee objects and implementing the showEmployeeDetails method to display the details of all employees in the directory.

Step 4: Using the Composite

Here's how you can use the CompanyDirectory composite along with Developer and Manager leaf nodes.

fun main() {
    val dev1 = Developer(100, "John Doe", "Pro Developer")
    val dev2 = Developer(101, "Jane Smith", "Entry Developer")
    val man1 = Manager(200, "Bob Brown", "Senior Manager")

    val directory = CompanyDirectory("Engineering Department")
    directory.addEmployee(dev1)
    directory.addEmployee(dev2)
    directory.addEmployee(man1)

    directory.showEmployeeDetails()
}

When running the main function, the expected output is:

Company Directory: Engineering Department
100 John Doe Pro Developer
101 Jane Smith Entry Developer
200 Bob Brown Senior Manager

In this example, we create instances of Developer and Manager and add them to the CompanyDirectory. When we call the showEmployeeDetails method on CompanyDirectory, it displays the details of all employees in the directory.

Why It Matters

The Composite pattern is significant in software development for various reasons:

  • Uniformity: It allows you to treat both individual objects and composites uniformly. This simplifies the way you interact with part-whole hierarchies.
  • Flexibility: It makes it easy to add new types of components, such as new types of employees, without changing the existing code.
  • Maintainability: It organizes hierarchies in a structured manner, making the code easier to maintain and extend.

By mastering the Composite pattern, you can design more flexible and maintainable systems that can handle complex hierarchical structures with ease. This lesson prepares you to create and manage composite objects effectively, enhancing your ability to build scalable and organized software.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal