/decorator

Simple project demonstrating the decorator design pattern. For this one I used the design pattern to create a house which can be decorated with furnatures.

Primary LanguageJavaApache License 2.0Apache-2.0

DECORATOR

Code

Simple project representing the decorator design pattern. For this one i used the design pattern to create a house which can be decorated with furnatures.

Resume

The ability to do this:

 @Test
    void shouldReturnHouseWithBathtubAndTVValue(){
        House house = new Bathtub(new TV(new SmallHouse(1000.0f)));
        assertEquals(1250.0f, house.getValue());
    }

Definition

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.

image

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new Decorator class that wraps the original class.

What problems can it solve?

  • Responsibilities should be added to (and removed from) an object dynamically at run-time.
  • A flexible alternative to subclassing for extending functionality should be provided.

When using subclassing, different subclasses extend a class in different ways. But an extension is bound to the class at compile-time and can't be changed at run-time

Source: Wikipedia | Refactoring Guru