Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, November 13, 2007

Singleton Vs Static

Singleton, MVC and Factory patterns are the design patterns I use very often. I also use static classes, more often than Singleton, but static classes are not design patterns. For every implementation of Singleton, one can come out with an alternate implementation with static classes. Lot of critics argue that Singleton is not the right way to program, and it is an anti-pattern. Singleton patterns can be considered as substitutions for global variables, and they are most useful when you want to use the same instance all over your program. Using one object ( say of type Class A) to manage instances of a different class (say Class B) , and providing all functionalities using the objects of Class B is obviously a better coding practice. This is a better replacement of Singleton pattern, compared with static classes. Here is an interesting discussing I'd with Cybereal at #java@irc.freenode.net. Cybereal explains why Singleton is a bad wrt to dependency injection.


(12:03:11 AM) codeshepherd: cybereal .. Aren't Singleton the best suitable implementations for Database connection pooling ?
(12:03:43 AM) codeshepherd: In a more general way... Singletons are best suitable to manager global variables..
(12:03:53 AM) cybereal: singletons *ARE* global variables, basically
(12:03:56 AM) cybereal: and that's why they are bad
(12:04:24 AM) cybereal: codeshepherd: you shouldn't need to use a singleton to use the same resource across a program
(12:04:50 AM) cybereal: but because people are lazy about typing and passing on dependency information to instantiated objects, you find they will lean towards singletons to do little more than save some keystrokes
(12:05:01 AM) cybereal: It's really hard to justify it for any other reason
(12:05:41 AM) cybereal: codeshepherd: despite being an anti-pattern, it does get used frequently so you'll still need to know how to deal with it, and sometimes to work around them
(12:06:02 AM) cybereal: at least in java if you really need to you can bury a singleton in a separate classloader instance
(12:06:13 AM) codeshepherd: So how will you implement a Database connection pool? Just a static class ?
(12:06:30 AM) cybereal: no, a regular class that you pass around to all the new instances of other classes that need to know about it
(12:06:44 AM) cybereal: like handing out invitations to a party
(12:06:58 AM) cybereal: you don't hand out houses with those invitations, and people aren't expected to figure out the address themselves
(12:07:06 AM) codeshepherd: then.. some dumb guy who comes tomorrow will start creating instances of the regular class..
(12:07:13 AM) cybereal: it won't matter
(12:07:24 AM) cybereal: but even so, if he's dumb he's gonna do dumb things :)
(12:07:34 AM) cybereal: you shouldn't write bad code just to try and protect from dumb coders
(12:07:54 AM) cybereal: but the point is you should be able to have more than one in the same vm, and not have them hurt each other
(12:07:56 AM) codeshepherd: It is really a debatable issue..
(12:08:54 AM) cybereal: codeshepherd: you probably won't really see it this way until it eventually comes back to bite you in the ass as it has me :)
(12:09:08 AM) codeshepherd: I still agree with the fact that.. you let one class (A) provide all functions.. and another class manage instances of Class A.. .. that looks more clean..
(12:09:33 AM) cybereal: you're making a mistake of thinking a class should ever be in charge of anything
(12:09:37 AM) cybereal: INSTANCES of classes should
(12:09:43 AM) cybereal: classes are blue prints
(12:09:54 AM) codeshepherd: hmm.. ok....
cybereal cybereal
(12:10:19 AM) codeshepherd: interesting argument.. thanks a lot cybereal
(12:10:28 AM) cybereal: nothing stops you, as I already described, from sharing a single instance of a class across your whole app
(12:10:50 AM) cybereal: you get what you want from a singleton without the limiting factors, and all it takes is a little longer parameter list in constructors :)
(12:11:18 AM) codeshepherd: ok.. I guess I will realize it better when it bites me back..
(12:11:24 AM) cybereal: hehe ok

Tuesday, November 06, 2007

Red Black Tree

TreeSet is an implementation of Red-Black Tree in Java. It implements the Set and Collection interface. Here is a short yet excellent video on adding new nodes to TreeSet.

Wednesday, October 31, 2007

Interactive Shell

Interactive Shells for some popular languages.

IPython ==> Python (supports readline)
IRB ==> Ruby (supports readline)
php -a ==> PHP
BeanShell ==> Java (interpretor for compiled language?? )
SquareFree Shell ==> JavaScript

Installing readline library should help you auto complete and browse through history.

Wednesday, October 24, 2007

Java Bean Shell

Java Bean Shell is a handy tool for Java programmers to test APIs, regular expressions, and other small code snippets. In particular if you are working on Client-Server architecture, it takes long time for you to test small changes in the back end. Though Java is compiled language, the interpreter tool in BeanShell will let you interpret the code. It can be downloaded from http://www.beanshell.org/ .

Demo

//to start Console
$ java -classpath ./bsh-2.0b4.jar bsh.Console
//to start Interpreter
$ java -classpath ./bsh-2.0b4.jar bsh.Interpreter
bsh % import java.io.Decoder;
bsh % String dir = "%2Fhome%2Fcodeshepherd";
bsh % System.out.println(dir);
%2Fhome%2Fcodeshepherd
bsh % System.out.println(Decoder.decode(dir));
/home/codeshepherd


The Interpreter does not support history and coloring. But the Console does. But the output from Console is thrown in the parent shell.

Thursday, October 04, 2007

Wife and Girl Friend

If
Family:Package
GirlFriend:Private
Wife: ?
Adultery: ?
Diverse: ?
OpenMarriage: ?

Choices
1)final
2)static
3)Public casting for Private
4)Abstract
5)Interface
6)BCEL


(03:18:29 PM) codeshepherd: if private is for girlfriend , then what is for wife ?
(03:18:46 PM) ***cybereal: hides his "jib" whatever that is...
(03:18:51 PM) Fanook: codeshepherd: static
(03:18:57 PM) cybereal: final
(03:19:18 PM) cybereal: unless you're in an open marriage, then it's abstract
(03:19:22 PM) codeshepherd: Fanook: static does not suit... finay may be ..
(03:19:36 PM) cybereal: Or if you're homeless it's transient...
(03:20:06 PM) Fanook: cheeser: hmmm, how would one implement a divorce then? :)
(03:20:22 PM) cybereal: Fanook: bytecode manipulation!
(03:20:26 PM) Fanook: hehe
(03:20:28 PM) cheeser: that's research i'll leave to others. P^)=
(03:22:08 PM) codeshepherd: adultery = ?
(03:22:21 PM) freeone3000: adultery is achieved through bcel.
(03:22:43 PM) freeone3000: Or those who put things public that should be private.
(03:22:57 PM) codeshepherd: hehe :)
(03:24:52 PM) cybereal: these metaphors would be more fun if java allowed multiple inheritence

Tuesday, September 25, 2007

SpringMVC + Hibernate + GridSphere + Apache Tomcat

I've successfully integrated GridSphere and SpringMVC. I've been using a lot of GridSphere Tag Libraries in my JSPs. Now I've to rewrite them using Spring specific tag libraries. In my opinion the GridSphere specific tag libraries are much easier to use compared to Spring tag libraries. The following link provides detailed explanation for integrating SpringMVC with GridSphere. http://docs.gridsphere.org/display/gs30/GridSphere+3.0+and+Spring+Portlet+MVC . Now I've to explore some templating Engines, and Acegi Tag libraries.