java - Circular references in Nation-Town-Citizens chain -
say have 3 classes, nation, town, , citizen. nation contains towns, , town contains citizens.
public class nation { set<town> towns; } public class town { set<citizen> citizens; } public class citizen { }
sometimes need know nation citizen belongs to. however, above implementation, there no way of knowing that, unless this:
public class nation { set<town> towns; } public class town { set<citizen> citizens; final nation nation; public town (nation nation) { this.nation = nation; } } public class citizen { final town town; public citizen (town town) { this.town = town; } public nation getnation() { homecoming this.town.nation; } }
i'm warned of staying away "circular references." constitute circular reference? , if so, should avoided? , if should avoided, alternative solution?
with original implementation, 2 step process nation of citizen.
for that, need
determine town citizen belongs to, , what nation contains town.so somewhere in main class need next details of nation
nations.get(towns.get(citizens.get(person))) //--> gives nation details
java
No comments:
Post a Comment