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.
The ability to do this:
@Test
void shouldReturnHouseWithBathtubAndTVValue(){
House house = new Bathtub(new TV(new SmallHouse(1000.0f)));
assertEquals(1250.0f, house.getValue());
}
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.
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.
- 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