jeudi 29 septembre 2016

Build an object having nested entities.Design Pattern

I am using Java 1.6

Scenario: I have an entity structure called 'pnr' which looks like this:

<pnr>
    <outbound>
        <travellers>
            <person>
                <name></name>
                <address></address>
            </person>
            <person>
                <name></name>
                <address></address>
            </person>
        </travellers>
        <segments>
            <segment>
                <from></from>
                <to></to>
                <date></date>
                <flight-details>
                    <flight-id></flight-id>
                </flight-details>
            </segment>

            <segment>
                <from></from>
                <to></to>
                <date></date>
                <flight-details>
                    <flight-id></flight-id>
                </flight-details>
            </segment>
        </segments>
    </outbound>

    <inbound>
        ...
    </inbound>
</pnr>

I want to build an object whose structure looks like the above XML. I start with an object of class "Pnr" which looks like this:

class Pnr{
    Outbound outbound;
    Inbound inbound;
}

//Outbound
class Outbound{
    ...
}

//Inbound
class Inbound{
    ...
}

The nesting of entities follows and each entity is represented by a class.

Please suggest a design pattern to build the nested Pnr object.

I have gone through the builder pattern. But in my case: 1) We dont have too many parameters which we are passing to any of the constructors. 2) All the time we want the whole Pnr object to be built. So we dont require a number of constructors with different parameter list.

But I have to handle building of the nested entities effectively. Should I still go with builder pattern ?

If not please suggest which design pattern will be suitable for such a case.

Thank you!

Aucun commentaire:

Enregistrer un commentaire