java - Right way to implement encapsulation -
i have following class scheme.
national
have arraylist
of zone
, zone
arraylist
of region
, region
have arraylist
of person
.
so have next questions:
1) can "push" person trough national , zone add in region?
for example:
national national = new national(); .... national.addperson(person); // every level has own addperson method
or
national.getzone(i).getregion(i).addperson(person);
what right way in oop?
2) can make method return every person in level?
i mean example:
zone zone = new zone(); ... zone.getpersons(); //return arraylist persons of every region in zone.
this goes against encapsulation?
3) next(), hasnext(), first() methods every level, can iterate in particular level.
that's all. encapsulation , oop in general, can't figure out right do, , wrong.
thanks.
given class diagram, person
must member of region
, is, national
or zone
cannot directly contain person
objects not contained in region
under it.
in case method matching signature addperson(person p)
can occur in region
. indicated, have do
national.getzone(i).getregion(j).addperson(person);
there no logical reason have, say,
national#addperson(person p)
because national
has no information on zone
, region
person belongs to.
however, add convenience methods national
, zone
to, call it, "push" person down, if provide necessary data.
national#addperson(zoneid zid, regionid rid, person) { ...
for zoneid
, regionid
substitute whatever key type use lookup zone , region within national
.
zone#addperson(regionid rid, person) { ...
as above regionid
.
this start, you'll have think through issues such happens when zoneid
and/or regionid
not found on addperson(...)
call. in design viable option throwing exception because don't have enough information @ point instantiate zone
or region
.
edit:
if indicated in comment, person
object contains enough information identify zone
, region
, convenience methods in national
, zone
can required lookup , delegate next addperson(person p)
in chain.
Comments
Post a Comment