设计模式,以增强Java中现有的数据结构,并在运行时附加附加信息 [英] Design pattern to enhance an existing data structure in Java with additional information at runtime

查看:88
本文介绍了设计模式,以增强Java中现有的数据结构,并在运行时附加附加信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从一个小例子开始:

I will start with a small example:

想象一下有几个实体的应用程序。

Imagine an application with a couple of entities.

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

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

假设我们有一个返回EnityC实例列表的服务方法。在UI中,我们要显示EntityC,但是还向实体添加一些仅与UI相关的附加信息(也许是一个css类)。解决这个问题的一个常见的方法是创建一个可以携带附加信息的包装器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 { ...}

或者可以使用传输对象作为简单的数据结构: p>

or maybe use a transfer object as simple data structure:

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

但是,如果服务返回EnitityA实例列表,我们需要附加什么附加信息到实例图中的所有实体(包括EntityB和EntityC的引用实体实例)?

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?

推荐答案

看看角色对象模式

它描述如何使用不同的东西扩展对象(称为角色) 。
该模式更多关于如何添加Bussiness Logic(这就是为什么它被称为角色),但是这个想法可以帮助你。

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.

这篇关于设计模式,以增强Java中现有的数据结构,并在运行时附加附加信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆