Friday, July 29
Essential for a J2EE Programmer
check this outhttp://www-128.ibm.com/developerworks/java/library/j-javares.html?ca=drs-#author
Dips at 4:30 PM
Friday, July 22
Annotate-tate-tate
Annotation - extra information associated with a particular point in a documentAs of Java release 5.0(Tiger) , the platform has a general purpose annotation (also known as metadata) facility that permits you to define and use your own annotation types. The facility consists of a
>> syntax for declaring annotation types - declare an annotation type by preceding @ to normal interface declaration
>> syntax for annotating declarations - annotation is a special kind of modifier and so can be used anywhere that a modifier is used.
It generally preceds any declaration (class, method, variable, parameter etc) and consists of the @ tag + annotation name + element , value in ()
>> APIs for reading annotations - Class, Method api has annotation information which can be read with help of reflection.
>> a class file representation for annotations - generate support documents based on annotations
>> and an annotation processing tool.
Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.
Annotation types :
>> Marker Annotation : An annotation type with no elements :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface Test { }
Declaration :
@Test public class TimeTravel { ... }
>> Single member annotation :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface Copyright {
int value(); // this variable name must be value
}
Declaration :
@Copyright("My Copyright")public class MyClass{...}
>> Custom annotation :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date(); default "[unimplemented]";
}
Declaration :
@RequestForEnhancement(id = 2868724,synopsis = "Manage Entitlements", engineer = "Mr. X", date = "4/1/3007" )
public static void manage() { ... }
Note that the annotation type declaration is itself annotated. Such annotations are called meta-annotations. The first (@Retention(RetentionPolicy.RUNTIME)) indicates that annotations with this type are to be retained by the VM so they can be read reflectively at run-time. The second (@Target(ElementType.METHOD)) indicates that this annotation type can be used to annotate only method declarations.
The defined annotations can be read, processed through the api (for simple processing) or by APT.
Possible usages for annotations :
>> Generate config files from annotated data in the code , so as to keep data at one place and avoid maitainence hassles.
>> Generate XML representation of data objects
>> Generate automatic boilerplate code using annotations.
Boilerplate - In information technology, a boilerplate is a unit of writing that can be reused over and over without change.
By extension, the idea is sometimes applied to reusable programming as in "boilerplate code." .. like a text template.
Useful Links :
http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
http://www.java2s.com/ExampleCode/Language-Basics/Annotation.htm
http://www.javaspecialists.co.za/archive/Issue097.html
http://javaboutique.internet.com/tutorials/annotation/index-2.html
Dips at 3:30 PM
Monday, July 18
Decorate away...
Decorator Design Pattern :
Motive :
Attach additional responsibilities to "an existing object" dynamically (and not the entire class )
This pattern can be used instead of subclassing (which we generally do to extend functionality).
So the thumbrules are :
>> Decorator has the same interface as the object that it wants to 'decorate'.
eg : Decorator class implements component interface.
>> Decorator contains (wraps ) the base object within itself. (also referred to as Wrapper)
eg : Decorator class has an instance of the component object.
This way it can modify the behaviour of the component object, and it can be called as required (dynamic) i.e call ConcreteDecoratorB(Component a)
This can also be very effectively used in situations where modification by extending class behaviour is impractical, as in having too many possibilities of sub-classes , or base class is hidden or unavailable.
Eg : Provide filtering behaviour for Httprequest object.
The decorator HttpServletRequestWrapper will implement HttpServletRequest . Suppose it modifies the getParamaterValues operation to filter blanks.
The filter would then call it as follows :
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter( new MyRequestWrapper((HttpServletRequest) request), response);
}
This shows that within the bounds of defined behaviour (ie : method signature of doFilter method) , we can modify the behaviour of the underlying object using the Decorator pattern.
This article is simplistic enough in its approach , to make you totally understand Decorator pattern : Link
Dips at 6:06 PM