Java Reference
In-Depth Information
// Send a SOAP Message to SOAP server
// Send this message to http://www.webserviceX.NET/stockquote.asmx
SOAPMessage soapResponse = soapConnection.call(
createSOAPRequest(STOCK_SYMBOL),
SERVICE_HOST + SERVICE_ENDPOINT);
// Close the connection
soapConnection.close();
} catch (Exception e) {
System.err.println("Fatal error occurred");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String stockSymbol) {
// Construct a new SOAP request message
return null;
}
}
Now take a look at the changes you've made. First, you created a global exception catcher in
the main class to print out any errors that might occur. Using a global exception catcher is
not good practice in general, but it's fine to create the prototype. Next, you use a class called
SOAPConnectionFactory to instantiate a SOAPConnection object.
There are a number of programming patterns at work here. First, note that you do not call the con-
structor for SOAPConnection directly (using the new keyword), but instead ask the factory class to
instantiate this object for you. “Factories” are a common Object‐Oriented Programming pattern
and are used for a couple of reasons. First, it allows the factory object to keep track of all objects it
has instantiated, providing a centralized means to itemize them and access them later. Second, since
factories take care of the actual instantiation of an object, the factory can decide which subclass is
best suited whenever multiple subclasses are available. Indeed, SOAPConnection is an abstract class,
which is why you cannot instantiate it directly (even if you wanted to do so), so the factory can
decide which actual subclass of SOAPConnection is best suited for your needs.
You can see a similar pattern occurring for the instantiation of the SOAPConnectionFactory object.
Again, you are not constructing this object directly using the new keyword, but instead calling the static
newInstance method to instantiate a factory object. The reasoning behind this is also similar: it allows
the internals of the library to keep track of all the factories that have been created and to select the
proper factory subclass to use. In some cases, you'll also see the so‐called “Singleton” pattern at work
here, where the newInstance method—or getOrCreateInstance , which would be a better name in
this case—will create a new factory if no factory exists and return the existing factory if it does exist,
effectively only allowing one single instantiation of this class (one single factory). Both the Factory
and Singleton patterns are heavily used design patterns in Object‐Oriented Programming. Chapter 12
provides more insights on object‐oriented patterns, but it's good to briefly mention them here.
Further on in the code, the call method is called on your SOAPConnection object to send a SOAP
request to http://www.webserviceX.NET/stockquote.asmx , the full endpoint URI. You also need
to pass a SOAPMessage object to send, which you create using another—currently empty—method
you've added: createSOAPRequest . The stock symbol is passed to this method as an argument, and
a constant is added to set the symbol “IBM.”
Search WWH ::




Custom Search