Decorator Pattern in Kotlin

Decorator Pattern

In this lesson, we'll step into the world of the Decorator pattern. The Decorator pattern is a structural design pattern that allows you to dynamically add behavior to an object without altering its structure. This pattern is particularly useful when you want to add responsibilities to objects without subclassing.

What You'll Learn

In this lesson, you will master:

  • The fundamental concept of the Decorator pattern.
  • How to implement the Decorator pattern with clear, practical examples.
  • The significance of the Decorator pattern in the software design landscape.

Let's see the Decorator Pattern in action through a practical example.

Implementing the Decorator Pattern

To understand the Decorator pattern, we'll use an example involving different types of cars and decorations (or features) that can be dynamically added to them. In our example, the objective is to create customizable cars with dynamic features such as a sports car and a luxury car.

  • Component (Car Interface): This interface defines the core functionality.
  • Concrete Component (Basic Car): This is the class that we want to dynamically add features to.
  • Decorator: This abstract class wraps a Car object and implements the Car interface.
  • Concrete Decorators (Sports Car, Luxury Car): These classes extend the Decorator class to add specific features to the Car.

Step 1: Define the Component Interface

First, we define the Car interface, which declares the core method for assembling a car.

interface Car {
    fun assemble(): String
}

In this example, Car is the component interface that declares the assemble method. All decorator classes will implement this interface.

Step 2: Implement the Concrete Component

Next, we create the BasicCar class that implements the Car interface.

class BasicCar : Car {
    override fun assemble(): String {
        return "Basic Car"
    }
}

Here, BasicCar provides the base functionality by implementing the assemble method to return the string "Basic Car."

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