mercredi 27 janvier 2016

When using the builder pattern, is there a way to pass data from one of the objects being used to build to another object?

I have two objects that are put together to create another object in a builder pattern in Java. One is a PayloadData abstract class object, the other is a HeaderData object (not abstract). They inherit from an interface called MessageElement. However, I need the PayloadData object to pass its size (I already have code that creates a variable in Payload that has this stored) to the Header object. How can I do this? PayloadData is an abstract object that is overriden by "Type"Payload (the type of data stored by payload).

HeaderData has a method for calculating overall message size:

    public int messageSize() //This method is finsihed already, but needs to be fixed.
    {

        return PayloadData.elementSize() + 16; //Something here need to be fixed.
    }

But this doesn't seem to work.

PayloadData has this method (which is overridden by the class that inherits from it:

public int elementSize() {
    // TODO Auto-generated method stub
    return 0;
}

The concrete object has this: to override it:

    @Override
public  int elementSize() //This is the one override that I think will be needed.  Need to figure out how to pass this to
//HeaderData, as right now it is not accepting it.
{
    return (48 + 184 * tracksInMessage());

}

I think I need to modify the builder pattern I'm using to let HeaderData access elementSize, but how should I do that?

Remember there is a class that is ultimately built, a Message class in this case, which is assembled from the HeaderData class and the class that inherits from PayloadData, and there is a builder class and a class that uses the builder class to create Message objects.

Here is the pattern I followed by the way, I replaced Burger with PayloadData and replaced ColdDrink with HeaderData but made it a concrete object:

http://ift.tt/1yOY7tn

Aucun commentaire:

Enregistrer un commentaire