Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern to add attributes to objects dynamically

Consider that we have a Car object. The acceleration and breaking features are implemented using strategy pattern. But what if we want to introduce nitro gas feature to an existing car object ? What is the design pattern that I can use ?

I want to add the nitro feature(Attribute) after creating the car object.

like image 864
Prasad Weera Avatar asked Nov 20 '12 09:11

Prasad Weera


2 Answers

You can check the Decorator pattern, it can be used to dynamically add functionality to an existing object.

like image 197
SomeWittyUsername Avatar answered Nov 14 '22 07:11

SomeWittyUsername


Decorator pattern can add different functionalities to objects dynamically. But these functionalities have to be implemented in a Concrete Decorator. The developer can decide what functionalities to add at run time.

like image 28
Jeewantha Avatar answered Nov 14 '22 05:11

Jeewantha