I have read a parent Class called MyClass
code like this,
public class MyClass extends CompositeObject{
protected Map<String,MyAttibute> attributes = new
HashMap<String,MyAttribute>
.....
}
In MyAttribute Class,
Code like this
public class MyAttibute extends MyObject
{
private MyClass definedOnClass;//point to its parentClass
}
This actually is a circular reference.Which makes trouble when you do deep serlization and equals. And it may not be a good design.How to avoid it? And after fixing it, we can still easily find the parentClass from its attribute.
P.S. I see another two classes design
public class Transaction{
private ChangeManager parentManager;
....
public Transaction(ChangeManager parentManager)
}
public class ChangeManager {
//record transaction when commit
private List<Transaction> transactions = new ArrayList<Transaction>();
Transaction currentTransaction;
....
}
Do you think this kind of design is good? Why?
As you can see the domains these classes are defining on are quite common.
So can anyone share some insight about it? Is it harmful to let Transaction know its ChangeManager and let MyAttributes know its MyClass in their properties? Any comments are welcome. Cons and pros.
put the MyAttibute as a child logically independent of the parent so attr1.equals(attr2) doesn’t involve the respective parents (same with serialization; don’t include it in the stream) and you can keep the definedOnClass property
or you can use a different equals method when testing from MyClass
note that the default serialization of java’s objectstreams can handle circular references