Friday, July 31, 2009

Factory Pattern, Visitor pattern, Aspect oriented programming and spring framework

Today i read wikipedia articles about visitor pattern and factory pattern...

Factory pattern is useful when u don't know the type of object that you are invoking..
You separate the logic of identifying the object type and invoking the desired method on that object type in factory class.

Basically term factory method means any method that creates objects. Whenever u wish to invoke the object, u create the factory class object and pass the information to this class which figures out and invoke appropriate object.

Visitor pattern:
Suppose there are four components in the system and all of them have common method like print or run or act... so instead of doing a implementation of common method in each of the subclass, a new interface is defined which declare common method for each component. A new class which extend to this interface implements the component specific functionality in class code. This way we separate the code of common method from sub classes.

Aspect oriented programming:
The piece of code on wiki gives a wonderful insight into this. Suppose u have bank transaction code. Here a method transfer money from one account to another. For sake of security and system services like logging and database transaction, a lot of checks will be introduced in the code. This is bad because in future if logging functionality changes or security changes then transaction code will have to be changed. This leads to cross cutting concerns. cross cutting means the logging module is cutting across other module so it creates problems. To avoid this aspectJ should be used. In this all the cross cutting code is specified in a separate file as aspect. This file defined the join point and point cuts which helps in separating code.

Spring framework:
Powerful framework with lot of features. Yet to understand it's role in j2ee applications fully.

SJ

Reading and writing files in java

I always forget the exact syntax of lines in java for reading and writing files...

Reading files:
Convert the file to inputstream or outstream and then read or write to the file using streamReader or streamWriter...

Create the fileReader from the file and read that fileReader using bufferedReader

Use scanner to read file by line by line...

Thursday, July 30, 2009

Communication between JSP and Servlets

getServletConfig().getServletContext().getRequestDispatcher(”jspfilepathtoforward”).forward(request, response)

Deep copy vs Shallow copy

If the object is having any pointer then deep copy is preferred because in case of shallow copy, if pointer is changed or object is destroyed then dangling pointer problem may arise.

Wonderful post on pass by reference and pass by value confusion of Java
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Values of reference is passed by address and reference itself is passed by value.

Abstraction vs Interfaces

Abstraction: Default implementation of set of methods.
Compulsive use of extends variable and overriding of default functionality
Better to be used in application framework where a default implementation is required in case component does not implement their own. Like event handling service, Messaging service etc..
If variables changes often then use abstraction

Interfaces: Abstract methods and final variable
Provide conceptual structure to whole application.
Allow Multiple inheritance
If methods are changed frequently then use interfaces.

For example: Interfaces/ Need for multiple inheritance

Two kind of people: actor / director ..
for each of them we define one interface but now what if there is third kind for people who are both actors and directors....

In such scenario we need multiple inheritance...

Need fr abstraction:

Abstraction in big application framework to define default handling.


Inheritance vs. Abstraction
When functionality is to be imposed on unrelated classes then use interfaces...
When functionality is to be used on related classes then use abstraction..

Related classes: GenericList singlelinklist doublelinklist... all have the similar properties and methods...

Unrelated Classes: some function in interface which should be implemented by many component in the system.

Sunday, July 19, 2009

Tricky to see which process is using which port

http://www.mydigitallife.info/2008/12/03/how-to-check-and-identify-which-application-is-listening-or-opening-port-80-and-443-on-windows/

First run netstat -o -n -a
then find the process id and use this process id to find out process name and details...

Friday, July 17, 2009

To setup netbeans on Ubuntu

http://www.javadesign.info/SystemsHardware/OS/Ubuntu/install-netbeans-on-ubuntu

This page describes things wonderfully...
Hope this helps...