微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

管理具有相互引用的 ID 的对象? 中介模式?

如何解决管理具有相互引用的 ID 的对象? 中介模式?

我有一个网络类,它存储有关对象的信息,这些对象也可能相互引用。对象也有继承自 Object 超类的子类(不在示例代码中)。

public class Network { 
   private Map<String Id,Object> idToObject; //registers all objects and their id
   private Map<Object,Object> objectToObject;
   private Map<ObjectSubclass> objectSubclas; //list of objects of one of the subclasses
}

public class Object{ 
   private String id; //required
   private Object object2; //optional
   private int relativePosition; //optional based on object2 
} 

对象有几种可能的引用关系和嵌套(未在示例代码中全部显示)。网络 idToObject 映射必须跟踪它持有的对象以及几个列表。对象直接相互引用是不是很糟糕?

实现接口或中介模式会更好(不确定这是不是中介模式?),如下所示:

public interface IdContainer
{
    String getId();
    void setId(String id);
    Set<IdRefContainer> getReference();

    Network getNetwork();
}

public interface IdRefContainer //For Objects which can reference an Object
{
    String getIdRef();
    void linkTo(IdContainer idc,int relativePosition);
    void unlink();
    
    int relativePosition();
    
    Network getNetwork();
}



public class Object implements IdContainer{ ...}
    
public class ObjectSubclass implements IdRefContainer{ ...}

public class Network { 
   private Map<String Id,IdContainer> idToObject; //registers all objects and their id
   private Map<IdContainer,IdContainer> objectToObject;
   private Map<ObjectSubclass> objectSubclas; //list of objects of one of the subclasses
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。