IMPLEMENTATION OF BUILDER PATTERN

 

 

As explained Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. In this project, the user interface is designed using the Builder Pattern. Creation of user interface fields (representation) is separated from the handling of user inputs and communicating with the server(complex operations).

 

The following explains builder pattern using “Add Nurse Details” user interface as an example. Builder pattern for “Add Nurse Details” involves three classes and another class which is common for all the user interfaces in the project:

·        AddNurseDetailsView.

·        AddNurseDetailsPanel.

·        AddNurseDetailsDialog.

·        UIBuilder.

 

AddNurseDetailsView: It’s an abstract class which provides necessary abstract methods for the “Add Nurse Details” screen.  AddNurseDetailsDialog communicates with this abstract class to retrieve the information entered by the user. Any class which needs to provide the field representation for AddNurseDetails screen should extend from this abstract class. If there is a need for a different look and feel for this AddNurseDetails screen, a new sub class of AddNurseDetailsView should be created and no other code needs to be changed.

 

 

public abstract class AddNurseDetailsView extends JPanel {

    public abstract Nurse getNurseDetails();

}

 

AddNurseDetailsPanel: This is a concrete class which extends from AddNurseDetailsView and provides the necessary user interface field representations. It provides the implementation for getNurseDetails( ) method by retrieving the user interface field values and creates a Nurse object and returns it to the caller.

 

public class AddNurseDetailsPanel extends AddNurseDetailsView {

    public AddNurseDetailsPanel() {

        createUI();

    }

 

    public Nurse getNurseDetails() {

        Nurse nurse = new Nurse();

        nurse.fName = checkString(fNameField, "First Name");

        nurse.lName = checkString(lNameField, "Last Name");

        nurse.address1 = checkString(address1Field, "Address1");

        nurse.address2 = address2Field.getText();

        nurse.city = checkString(cityField, "City");

        nurse.state = checkString(stateField, "State");

        nurse.zip = checkString(zipField, "Zip");

        nurse.tel = checkString(telField, "Telephone");

        nurse.startTime = (String)startTimeField.getSelectedItem();

        nurse.endTime = (String)endTimeField.getSelectedItem();

        return nurse;

    }

       private String checkString(JTextField field, String name) {

      String value = field.getText().trim();

        if( value.length()<= 0 ) {

          throw new IllegalStateException("Please enter the "+name);

        }

        return value;

    }

 

    private void createUI() {

     

      fNameLabel.setText("First Name:");

      fNameLabel.setBounds(new Rectangle(42, 54, 84, 20));

      fNameField.setBounds(new Rectangle(139, 54, 160, 21));

     

      lNameLabel.setText("Last Name:");

      lNameLabel.setBounds(new Rectangle(45, 101, 68, 17));

      lNameField.setBounds(new Rectangle(139, 96, 160, 21));

     

      address1Label.setText("Address1:");

      address1Label.setBounds(new Rectangle(45, 138, 65, 21));

      address1Field.setBounds(new Rectangle(139, 137, 160, 21));

     

      address2Label.setText("Address2:");

      address2Label.setBounds(new Rectangle(46, 180, 75, 16));

      address2Field.setBounds(new Rectangle(138, 178, 160, 21));

     

      cityLabel.setText("City:");

      cityLabel.setBounds(new Rectangle(46, 215, 90, 23));

      cityField.setBounds(new Rectangle(138, 215, 160, 21));

     

      stateLabel.setText("State:");

      stateLabel.setBounds(new Rectangle(46, 250, 90, 23));

      stateField.setBounds(new Rectangle(138, 250, 160, 21));

     

      zipLabel.setText("Zip:");

      zipLabel.setBounds(new Rectangle(46, 285, 90, 23));

      zipField.setBounds(new Rectangle(138, 285, 160, 21));

 

      telLabel.setText("Telephone:");

      telLabel.setBounds(new Rectangle(46, 320, 90, 23));

      telField.setBounds(new Rectangle(138, 320, 160, 21));

      

      startTimeLabel.setText("Start Time:");

      startTimeLabel.setBounds(new Rectangle(46, 355, 90, 23));

      startTimeField.setBounds(new Rectangle(138, 355, 160, 21));

     

      endTimeLabel.setText("End Time:");

      endTimeLabel.setBounds(new Rectangle(46, 390, 90, 23));

      endTimeField.setBounds(new Rectangle(138, 390, 160, 21));

 

        setLayout(null);

        add(fNameLabel);

        add(fNameField);

        add(lNameLabel);

        add(lNameField);

        add(address1Label);

        add(address1Field);

        add(address2Label);

        add(address2Field);

        add(cityLabel);

        add(cityField);

        add(stateLabel);

        add(stateField);

        add(zipLabel);

        add(zipField);

        add(telLabel);

        add(telField);

        add(startTimeLabel);

        add(startTimeField);

        add(endTimeLabel);

        add(endTimeField);

    }

    JTextField fNameField = new JTextField();

    JLabel fNameLabel = new JLabel();

    JLabel lNameLabel = new JLabel();

    JTextField lNameField = new JTextField();

    JLabel address1Label = new JLabel();

    JTextField address1Field = new JTextField();

    JLabel address2Label = new JLabel();

    JTextField address2Field = new JTextField();

    JLabel cityLabel = new JLabel();

    JTextField cityField = new JTextField();

    JLabel stateLabel = new JLabel();

    JTextField stateField = new JTextField();

    JLabel zipLabel = new JLabel();

    JTextField zipField = new JTextField();

    JLabel telLabel = new JLabel();

    JTextField telField = new JTextField();

    JLabel startTimeLabel = new JLabel();

    JComboBox startTimeField = new JComboBox();

    JLabel endTimeLabel = new JLabel();

    JComboBox endTimeField = new JComboBox();

}

 

 

AddNurseDetailsDialog: This is the dialog class which is actually shown to the user. It always refers user interface fields representation by AddNurseDetailsView abstract class. It does not refer AddNurseDetailsPanel directly. createUI( ) method of AddNurseDetailsDialog class invokes createAddNurseDetailsPanel( ) method of the UIBuilder to get the “Add Nurse Details” fields representation.

 

public class AddNurseDetailsDialog extends AbstractDialog {

    public AddNurseDetailsDialog(JFrame owner) {

        super(owner);

        createUI();

    }

 

    private void createUI() {

        this.setTitle("Add Nurse Details");

        getContentPane().setLayout(new BorderLayout());

 

        cancelButton.setBounds(new Rectangle(206, 261, 78, 24));

        cancelButton.setText("Cancel");

        okButton.setBounds(new Rectangle(114, 262, 78, 24));

        okButton.setText("Ok");

 

        jPanel2.add(okButton);

        jPanel2.add(cancelButton);

        view = UIBuilder.createAddNurseDetailsPanel();

        this.getContentPane().add(view, java.awt.BorderLayout.CENTER);

        this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        this.setSize(400, 400);

 

        okButton.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

            if( addNurse() ) {

              dispose();

            }

          }

        });

 

        cancelButton.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

              dispose();

          }

        });

    }

 

    private boolean addNurse() {

      try {

        Nurse nurse = view.getNurseDetails();

        BiddingClient.getBiddingFacade().placeNurseTimeForBid(nurse);

        return true;

      } catch(Exception e) {

        errMsg = e.getMessage();

        JOptionPane.showMessageDialog(this, errMsg, "Warning", JOptionPane.WARNING_MESSAGE);

        return false;

      }

}

      private AddNurseDetailsView view = null;

}

 

UIBuilder: This is the common class which is used by all the screens in the project. It is responsible for different types of user interface screens.  

 

public class UIBuilder {

 

    public static AddNurseDetailsView createAddNurseDetailsPanel() {

        return new AddNurseDetailsPanel();

    }

   

 

    public static BidOnNurseTimeView createBidOnNurseTimePanel() {

        return new BidOnNurseTimePanel();

    }

 

    public static BidDetailsView createViewBidDetailsPanel() {

        return new BidDetailsPanel();

    }

}

 

 

            All the other screens like “Bid on nurse Times”, “View Bid Details” use the same Builder Pattern.