samedi 13 août 2022

Use of Director and Builder Patterns for Complex Objects

I think I am getting confused between Directors and Builders. I need to build a series of different types of massages. Each message type is built from segments, however one segment type can be used in multiple message types. The input object for all messages and segments is the same.

Here is what I have been working on (simplified):

public class AdtA28Director implements AdtDirector {
    private static final String MESSAGE_TYPE = "A28";
    private Patient patient;

    public AdtA28Director(Builder builder) {
        this.patient = builder.patient;
    }
    
     public String toString(){
            return new StringBuilder("AdtBuilder:Type:Patient:").append("A28").append(":").
            append(patient).toString();
        }

    static class Builder {
        private Patient patient;
        private ADT_A05 adtMessage;
        
        public Builder(Patient patient) {
            this.patient = patient;
        }

        public Message build() throws HL7Exception, IOException {
            String currentDateTimeString = getCurrentTimeStamp();
            adtMessage = new ADT_A05();
            adtMessage.initQuickstart("ADT", MESSAGE_TYPE, "P");
            createMshSegment(currentDateTimeString);
            createEvnSegment(currentDateTimeString);
            return adtMessage;
        }
        
        private void createMshSegment(String currentDateTimeString) throws DataTypeException {
            MSH mshSegment = _adtMessage.getMSH();
            mshSegment.getFieldSeparator().setValue("|");
            mshSegment.getEncodingCharacters().setValue("^~\\&");
            mshSegment.getSendingApplication().getNamespaceID().setValue("Our System");
            mshSegment.getSendingFacility().getNamespaceID().setValue("Our Facility");
            mshSegment.getReceivingApplication().getNamespaceID().setValue("Their Remote System");
            mshSegment.getReceivingFacility().getNamespaceID().setValue("Their Remote Facility");
            mshSegment.getDateTimeOfMessage().getTimeOfAnEvent().setValue(currentDateTimeString);
            mshSegment.getMessageControlID().setValue(getSequenceNumber());
            mshSegment.getVersionID().getVersionID().setValue("2.4");
        }

        private void createEvnSegment(String currentDateTimeString) throws DataTypeException {
            EVN evn = _adtMessage.getEVN();
            evn.getEventTypeCode().setValue("A28");
            evn.getRecordedDateTime().getTimeOfAnEvent().setValue(currentDateTimeString);
        }
}

Using this I can

Message adtMessage = new AdtA28Director.Builder(patient).build();

Some message ojject types will require (or similar):

Message adtMessage = new AdtA01Director.Builder(patient).visit(visit).build();

I am not really sure that I am using the builder pattern correctly. Is this a reasonable approach - can it be improved? I am planning to move the creation of the MSH and ENV segments to a separate class(s). There are perhaps 20 different message types and maybe a similar number of segments that I have to manage so I am trying to build a very clear and understandable (scaleable) pattern.

Any guidance / corrections etc appreciated.

Aucun commentaire:

Enregistrer un commentaire