Lesson Overview and Goals

Hello, welcome back! Today, we will decode the fundamentals of Revising Basic Design PatternsComposition! A vital component of software design patterns, composition aids us in creating complex classes using simpler ones. Our journey today includes understanding the concept of composition, its value in software development, and how to practically implement it in C#.

Garnering Clarity on the Composition Design Pattern

To kick-start our exploration, let's understand Composition. In object-oriented programming (OOP), composition allows a class to include other classes, paving the way for the creation of complex systems out of simpler components. For instance, when building a car, we bring together independent pieces like the Engine, Wheels, and Seats — a perfect reflection of composition in everyday life. Note that in composition, should the parent object (the car) be destroyed, the child objects (the components) also cease to exist.

Acing the Composition Design in C#

Now, let's translate the theory into a C# code application. Transforming the previously mentioned car example, a Car class in C# is created by making objects of the Engine, Wheels, and Seats classes. The Car class owns these child objects; their existence is dependent on the Car.

using System;

class Engine
{
    public void Start()
    {
        Console.WriteLine("Engine starts");  // Engine start message
    }

    public void Stop()
    {
        Console.WriteLine("Engine stops");   // Engine stop message
    }
}

class Wheels
{
    public void Rotate()
    {
        Console.WriteLine("Wheels rotate");  // Wheel rotation message
    }
}

class Seats
{
    public void Adjust(string position)
    {
        Console.WriteLine("Seats adjusted to position " + position); // Seat adjustment message
    }
}

class Car
{
    private readonly Engine _engine;
    private readonly Wheels _wheels;
    private readonly Seats _seats;

    public Car()
    {
        _engine = new Engine();
        _wheels = new Wheels();
        _seats = new Seats();
    }

    public void Start()
    {
        _engine.Start();  // Call to start engine
        _seats.Adjust("upright");  // Adjust seat position
        _wheels.Rotate();  // Get wheels rolling
    }

    static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.Start();  // Begin car functions
    }
}

// Prints:
// Engine starts
// Seats adjusted to position upright
// Wheels rotate

In the above code, the Car class encapsulates Engine, Wheels, and Seats objects, which are independent but part of the Car class, forming a composition pattern.

In the composition model, components like Engine, Wheels, and Seats can exist independently. These classes are not subclasses of Car but are simply used by the Car to achieve its functionality. This is an important feature of composition because it allows for reuse of these components in other contexts. For instance, the Engine class could be used in other vehicles like Truck or Boat without needing to inherit from a specific Car class.

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