I have to construct set of messages with in my program. All the messages have the same message header and the message body is different based on message type. According to my knowledge the builder pattern is a good candidate, but when I am using it I have a feeling that I am not eliminating the coupling between the client and the message builder because I have to pass or set data to the message builder before the director creates the message. These are dynamic data at run time.
Following is the structure of a message
<Header Potion><Data Potion Based on Different Message Types>
Lets say the structure of my warning message is like this
<Header Potion><Warning Description : warning icon : warning code>
This is my Message Class
private String header = "GENERIC HEADER";
private String dataPotion ="";
Following is my WarningBuilder
private String warningDescription;
private Image warningIcon;
private int warningCode;
@Override
public void createDataPotion() {
message.setDataPotion(warningDescription + ":" +
warningIcon + ":" + warningCode);
}
So my questions are
- Who has the responsibility to set the data to the builder ? Client or the Director class ?
- Is this a good approach to construct messages ? If not what are the design patterns that I can use ?
- Can I use Decorator Pattern here ? Because all the message types have the same header potion and only the message body differ from one another ?
- I have a feeling that my director is not doing anything but to call the createDataPotion() method in builder class. According to my knowledge the director should hold the logic of the object creation.In that case director should be doing the message composition but then I have to create many director methods (like createWarningMessage(), createSomeOtherMessage() etc.)
Aucun commentaire:
Enregistrer un commentaire