Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern to enhance an existing data structure in Java with additional information at runtime

I will start with a small example:

Imagine an application with a couple of entities.

EntityA -1---n-> EntityB -1---n-> EntityC

Let's say we have a service method that returns a list of EnityC instances. In the UI we want to display EntityC but also add some additional information to the entity that is only relevant for the UI (maybe a css class or so). A common way to solve this would be to create a wrapper arround EntityC that can also carry the additional information.

public class EntityCWrapper implements EntityC, AdditionalInfo { ...}

or maybe use a transfer object as simple data structure:

public EntityTO {
  public EntityC entity;
  public AdditionalInfo info;
}

But what if the service returns a list of EnitityA instances and we need to attach AdditionalInfo to all entities in the instance graph (including the referenced entity instances of EntityB and EntityC)?

Does anyone have an idea or can point me to a design pattern suitable in this situation?

like image 912
sprehn Avatar asked Jan 26 '26 13:01

sprehn


1 Answers

Have a look at the Role Object Pattern.

It descripes how an object can be extended with different stuff (called Roles). The pattern is more about how to add Bussiness Logic (that is why it is called roles) but may the idea helps you.

like image 60
Ralph Avatar answered Jan 28 '26 04:01

Ralph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!