Sunday, January 30, 2011
Command Pattern
Why this pattern has name as 'Command' ? Because someone gives a command and not care how..who will do it...
Friday, January 28, 2011
Thursday, January 27, 2011
Visitor Pattern
Why - As per wikipedia it is "is a way of separating an algorithm from an object structure it operates on".
Lets discuss with an example
- object structure - e.g. getters and setters of a class with attributes interest, principle.
(i.e. getInterest(), getPrinciple()). Lets say the class name having these getters and setters is 'Amount'.
- algorithm to calculate the interest in a class InterestCalculator. While getting the interest from 'Amount', InterestCalculator.calculate needs to be called.
- Amount is Visitable (Visitable is interface which has 'getInterest' method)
class Amount implements Visitable {
getInterest(Visitor visitor) {
visitor.visit(this) ;
}
}
- InterestCalculator is 'Visitor' (Visitor is an interface which have visit method)
class InterestCalculator implements Visitor {
visit(Amount amount) {
// calculation goes here <<<<<<<------------
}
}
- The client (e.g. main method) has to know which visitor (i.e. intetestcalculator) to inject into the accept method.
It invokes it like
Amount a = new Amount() ;
// set values in 'a' object e.g. Principal
a.getInterest(new InterestCalculator)
Lets discuss with an example
- object structure - e.g. getters and setters of a class with attributes interest, principle.
(i.e. getInterest(), getPrinciple()). Lets say the class name having these getters and setters is 'Amount'.
- algorithm to calculate the interest in a class InterestCalculator. While getting the interest from 'Amount', InterestCalculator.calculate needs to be called.
- Amount is Visitable (Visitable is interface which has 'getInterest' method)
class Amount implements Visitable {
getInterest(Visitor visitor) {
visitor.visit(this) ;
}
}
- InterestCalculator is 'Visitor' (Visitor is an interface which have visit method)
class InterestCalculator implements Visitor {
visit(Amount amount) {
// calculation goes here <<<<<<<------------
}
}
- The client (e.g. main method) has to know which visitor (i.e. intetestcalculator) to inject into the accept method.
It invokes it like
Amount a = new Amount() ;
// set values in 'a' object e.g. Principal
a.getInterest(new InterestCalculator)
Monday, March 2, 2009
XQuery
Sometimes when u have a big XML document, you want to iterate through some elements of it. One way of doing it is to parse it and process the needed elements. Another way might be to use XQuery/XPath. You can directly access the needed elements/attributes and their values using XQuery.
I present an example which uses Saxon s9api. Include saxon and saxon's s9api jar in the classpath. Consider this xml string ->
" Abiteboul "
Suppose if we want to know the years of the book (imagine there are 100s of 1000s books in the xml, though this xml shows just 1 book). We can use the follwing java code.
Processor processor = new Processor(false);
ItemTypeFactory itf = new ItemTypeFactory(processor);
ItemType integerType = itf.getAtomicType(new QName("http://www.w3.org/2001/XMLSchema", "integer"));
DocumentBuilder builder = processor.newDocumentBuilder();
builder.setLineNumbering(true);
builder.setWhitespaceStrippingPolicy(WhitespaceStrippingPolicy.ALL);
String str = " Abiteboul ";
StringValue strVal = new StringValue(str.subSequence(0, str.length()));
InputStream bais = new ByteArrayInputStream(str.getBytes());
StreamSource ss = new StreamSource(bais);
XdmNode fileNode = builder.build(ss);
XPathCompiler xpc = processor.newXPathCompiler();
XPathExecutable xqe = xpc.compile("/BOOKS/BOOK/@YEAR");
XPathSelector selector = xqe.load();
selector.setContextItem(fileNode);
XdmValue val = selector.evaluate();
XdmSequenceIterator itr = val.iterator();
while(itr.hasNext()) {
XdmItem xi = itr.next();
XdmNode xn = (XdmNode) xi;
System.out.println(" Node Name " + xn.getNodeName().getLocalName());
System.out.println(" Node Value " + xn.getStringValue());
}
Note - We can read an xml saved in a file too in addition to xml in string variable as in above example.
Saturday, December 6, 2008
JWS
New spec of JWS uses JAXB 2.0 for JAVA/XML binding. The binding info can be provided in form of annotations on java classes or in external binding file. If annotations have this info then it will be used during deployment and run time. But if the binding information is in external file then it will be used only during deployment time and not during execution.
One good feature of this JWS spec is that if some info is not provided on java classes for exposing them as web service then it will assume defaults.
Subscribe to:
Posts (Atom)
