dimanche 22 janvier 2023

Decorator pattern limitations

I have a usecase where I am trying to use decorator pattern but not sure if it is the right way of using pattern. I have 2 message publisher class. First publisher takes json as input and publish message Second publisher takes bytes as input and publish message

Currently these 2 are separate classes

public class A {
    publish(Json input);
}

public class B {
    publish(byte[] input);
}

I want to decorate B with A instead of creating a separate class. Basically user would give input as Json and it would be converted to compressed bytes and published to a sink. But problem what i can think here is, while I can decorate B with A, I can't decorate A with B, reason being json can be converted to byte stream but all byte stream can't be converted to json format, and doing so would throw exception.

All decorator classes inherit same parent interface, so one can dcorate classes in any possible combination. So for decorator pattern to work properly, all of decorators must be compatible with each others irrespective of orders in which they are applied. Is this understanding correct or am I missing something in decorator pattern.

Aucun commentaire:

Enregistrer un commentaire