# Decorator

* Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
* Client-specified embellishment of a core object by recursively wrapping it.
* Wrapping a gift, putting it in a box, and wrapping the box.

## Example - Cars

We create few classes and then we can dynamically build cars, without changing the code.

```
interface Car {
    void assemble();
}

class BasicCar implements Car {

    @Override
    public void assemble() {
        System.out.println("Basic car.");
    }
}

class CarDecorator implements Car {

    private Car car;

    public CarDecorator(Car car) {
        this.car = car;
    }

    @Override
    public void assemble() {
        car.assemble();
    }
}

class SportCar extends CarDecorator {

    public SportCar(Car car) {
        super(car);
    }

    @Override
    public void assemble() {
        super.assemble();
        System.out.println("Sport car.");
    }
}

class LuxuryCar extends CarDecorator {

    public LuxuryCar(Car car) {
        super(car);
    }

    @Override
    public void assemble() {
        super.assemble();

        System.out.println("Luxury car.");
    }
}
```

Here is how to build Mustang and LaFerrari using the classes wrapping.

```
Car mustang = new SportCar(new BasicCar());
mustang.assemble();

System.out.println();

Car laFerrari = new SportCar(new LuxuryCar(new BasicCar()));
laFerrari.assemble();
```

The code prints out the following and we can observe how car was assembled.

```
Basic car.
Sport car.

Basic car.
Luxury car.
Sport car.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/structural-patterns/decorator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
