Scope, Performance and Object

R

Robert Mazur

Question about scope and performance.

Let's say I have a JSP that displays an invoice using XML data
that it needs to fetch (snippet code sample at bottom).

I have 3 object to hold data:
BillTo.class
ShipTo.class
LineItems.class

I have a "container" Invoice.class that instantiates the 3 object classes
and receives all the SAX calls from the parser, ultimately building the invoice:
Invoice.class
- has a BillTo object
- has a ShipTo object
- has a LineItems object
- can figure out what to do with XML elements it receives

And I have a parser class that fetches/receives and parses the invoice XML:
XMLParser.class
- creates a local Invoice.class. XMLParser steps through the elements
(SAX) and passes them to the local Invoice.class object to processing/storage.


So my flow goes like this:

1) JSP creates XMLParser, passing in URL of XML location

2) XMLParser fetches XML and creates its own Invoice.class.

3) XMLParser steps through elements (SAX) and hands Invoice.class
the XML elements, where they subsequently get stuffed into the 3 objects
(BillTo,ShipTo,LineItems) that reside inside Invoice.class.

3) Invoice.class now has the 3 object stuffed with data the JSP wants.
----
OK, I now want to get the data out. The JSP has an instance of XMLParser
whom has an Invoice.class object. The JSP ultimately wants the
3 objects inside Invoice.class.

Currently I have it so that XMLParser has a method:
public Invoice getInvoiceObject(){ ....

.....and the Invoice class has its instances of BillTo, ShipTo,LineItems
public. This allows me to say from the JSP:

<!-- this is a JSP -->
Your invoice is below:<p>
<%
XMLParser xmlparser = new XMLParser();
xmlparser.parseData(strXMLInvoiceData); //already have the XML
//xmlparser now stuffed its local Invoice full of data
Invoice invoice1 = xmlparser.getInvoiceObject();
xmlparser = null;
out.println("The billing contact is: " + invoice1.billto.getContact());
%>

-----
This seemed ok to me until I had to keep the 3 objects (BillTo,ShipTo,LineItems)
inside Invoice.class "public".

QUESTIONS:
1) I could keep the 3 object inside Invoice local, and use methods like:
//inside the Invoice.class right now
public BillTo getBillTo(); //returns BillTo object

...but then wouldn't two BillTo object instances be created?
One instance inside Invoice, the other inside the JSP?

2) What happens with scope? Meaning, if the JSP above is called by 2 clients
simultaneously with the BillTo being public, can the 2nd client be altering
the data of the first client's BillTo object...because BillTo is public?

3) Or, is this pretty much ok as is?

--------
Here it is in a code snippet:

public class Invoice {

public BillTo billto; //object of billing data
public ShipTo shipto; //object of shipping data
public LineItems lineitems; //object of line items

/**
* this class now has methods which accept
* xml elements from XMLParser and stuff them into
* its 3 local objects above
*/

// ....etc.
}


========= then in a JSP ========

<!-- this is a JSP -->
Your invoice is below:<p>
<%
XMLParser xmlparser = new XMLParser(); //xmlparser has Invoice instance
xmlparser.parseData(strXMLInvoiceData); //pass in valid XML
//xmlparser has now stuffed its
//local Invoice full of data

Invoice invoice1 = xmlparser.getInvoiceObject();
xmlparser = null;
out.println("The billing contact is: " + invoice1.billto.getContact());
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top