I am Ph.D. student in Computer Science at King Abdullah University of Science & Technology (KAUST).
Wednesday, November 5, 2008
All about scope
Page
Session
Application
If you have a request scope, it will only run for the request that is made on that same moment.
If you have a page scope, the parameters and values will run for that page the whole time until the server is restarted.
If you have a session scope, all the values will be kept for until the clients session is destroyed.
And least the application scope, if you apply the application scope, the values will be kept for the entire application, until the server is restarted.
Tuesday, August 26, 2008
Chain of Responsibility Pattern
The classic Chain of Responsibility(CoR) pattern defined by Gang of Four(GoF) in Design Patterns:
"Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it."
Following Figure illustrates the class diagram.
Figure 1. CoR class diagram
A typical object structure might look like following Figure .
Figure 2. CoR object structure
From the above illustrations, we can summarize that:
- Multiple handlers may be able to handle a request
- Only one handler actually handles the request
- The requester knows only a reference to one handler
- The requester doesn't know how many handlers are able to handle its request
- The requester doesn't know which handler handled its request
- The requester doesn't have any control over the handlers
- The handlers could be specified dynamically
- Changing the handlers list will not affect the requester's code
1. Microsoft Windows global hook framework
2. Java servlet filter framework
Details are explained by Michael Xinsheng Huang, JavaWorld.com, 08/16/04
Monday, August 18, 2008
Difference between Association, Aggregation and composition
Association:
1. A relationship between 2 objects that is not whole-part i.e.
A contains a reference of B and uses B through the reference
2. Generally implemented as a public property of A,
where B may be set in to A by an external party (say Spring)
Dependency:
1. A week association, where A doesnt contain a reference of B but uses B
2. Generally implemented as method parameter in A
Composition:
1. A strong whole-part relationship, i.e. B (child) has no existence
outside A (parent)
2. A creates and contains B but B can not contain A
2. B must not be visible outside of A since if A dies, B must die
Aggregation:
1. A week whole-part relationship, i.e. B (child) may exist exist
outside A (parent)
2. A contains B but B can not contain A
3. B must be visible outside of A since B may still exist when A dies
4. Generally 1-many relationship
With Real Life Examples:
Accosiation:
1. Create a folder called "Links"
2. create a shortcut inside this folder and link it to www.yahoo.com
3. create another shortcut instide this folder and link it to www.google.com
4. Ask your friend to do the same on another machine using same links
(www.yahoo.com and www.google.com)
5. Delete the "Links" folder, and open your browser to check if
www.yahoo.com and www.google.com still exist or not ;)
Briefly, Association is a relationship where all the objects have different lifecycles.
There is no owner.
Composition:
1. Open a new Document name it as test.txt
2. Write this sentence in iside this document "This is a composition".
3. Save the document.
4. Now, delete this document.
This is what is called composition, you can't move the sentence "This is a
composition" from the document because its lifecycle is linked to the
parent (the document)
Aggregation:
1. Create a file called file.txt
2. make a simple application to open the file.txt (rw), but don't program
it close the connection.
3. Run an instance of this application (it should work ok and can open the file for rw)
4. Keep the first instance, and run another instance of this application
(In theory it should complain that it can't open the file in rw mode
because it is already used by other application).
5. Close the 2 instances (make sure you close the connection).
From the above application, we knew that the Application and the file has a
separate lifecycles, however this file can be opened only by one
application simuletanously (there is only one parent at the same time,
however, this parent can move the child to another parent or can make
it orphan).
View Details: http://www.artima.com/forums/flat.jsp?forum=17&thread=24715
Wednesday, April 30, 2008
Use globally defined constants
You can use globally defined constants by defining them inside a class and referencing themwithout instantiation.
public class CONSTANT { |
Since the members of the class are defined as "static", there is no need to instantiate the class. To use a constant, simply use CONSTANT.[constant name]
if (myMethod()==CONSTANT.SUCCESS) {
...;
}
else {
...;
}
For details: Java Tips
Set the memory available to the JVM
By default, the JVM will use up to 16Mb of RAM. If your program allocates a lot of memory, you may need to increase this value to give more room to the garbage collector.
When starting the JVM, two parameters can be adjusted to suit your memory needs:
-mx n Sets the maximum size of the memory allocation pool where n is in bytes, appending "m" to n will specified the number in megabytes, for example to set the maximum at 20Mb:
java -mx 20m myApp
. -ms n Sets the startup size of the memory allocation pool, where n is in bytes, appending "m" to n will specified the number in megabytes. The default is 1Mb.
With JDK1.2, that syntax have changed, no space between ms/mx and the value:
java -mx20m myApp
For Details: Java Tips
Pass an integer by reference
Sometimes you may need to pass an integer to be able to change its value. "integer" are always passed by value in
int[] a = new int[1]; |
For details: Java Tips
Make a JAR executable
In the manifest file of a JAR, it is possible to specify the class to be used when the JVM is lauched with the JAR as parameter. The class must have a main().
Try with this simple class:
import |
Then create this manifest file (manifest.mft) with any text editor.
Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .\MyJar.jar
Next, you include the manifest file in the JAR (MyJar.jar) with the MyClass class.
jar cvfm MyJar.jar manifest.mft MyClass.class
Then you are able to start the MyClass.class by double-clicking on the MyJar.jar file (if the JRE is correctly installed) or by typing
java -jar MyJar.jar
On NT, you can also make JARs run from the command-line by setting the PATHEXT environment variable, for example
set PATHEXT=.EXE;.BAT;.CMD;.JAR
Then if you have the jar file MyJar.jar accessible via the PATH environment variable, typing "MyJar" on the DOS command line will invoke "java -jar MyJar.jar" .
For Details: Java Tips