mercredi 3 août 2022

Is it bad practice for a Decorator Class to change the state of the innermost wrapped object

Normally by definition the Decorator Pattern attaches new behaviour to an object by using a chain of decorators on the decorated class. The pattern exploits the chain of decorators to the main decorated class to transform incoming and outgoing data to the decorated class.

Although there is a reference in every decorator class to the immediate successor there is no reference to the actual decorated class object (innermost class). So, there is no easy way that a decorator class could access or modify the decorated class's state (innermost class) unless this information is transferred in the chain of decorators (which is not very convinient). This weird to me because the decorator pattern is considered the dynamic alternative of inheritance which allows this to happen. The subclasses can access and modify the state of the parent classes ( if they are not private ) however in the decorator pattern description this is not conviniently provided.

So the question is : Is it bad practice for a decorator to access the data in the innermost class in the chain ( decorated class )?

Aucun commentaire:

Enregistrer un commentaire