mardi 28 juillet 2015

Builder for Linked list with two types

Here is what I am trying to solve:

I need to model a Route from Location A to Z via many other locations B, C...Y. A location is connected only to the next location via exactly one Road.

So I have two entities: Location and Road

Location implements RouteElement{
   String name;
   BigDecimal lat;
   BigDecimal long;

   RouteElement nextElement; // optional
}

Road implements RoutElement{
   Length howLong;
   Boolean hasTolls;

   RouteElement nextElement; // mandatory
}

And finally,

Route {
   // some properties
   RouteElement startingElement;
}

Now, All these entities are immutable, so once a nextElement is set to null in Location, it can not be "set" unless I create a copy on the first object.

So, I was thinking if I can come up with a RouteBuilder to build a Route gracefully!

Could someone please suggest how to write a Builder for this?

Thanks!

Aucun commentaire:

Enregistrer un commentaire