Tuesday, March 25, 2008
SINGLETON vs STATIC
| The Singleton pattern is useful when the object itself needs to keep some state.If no such thing is needed, a static method can do the job as well. A purely static class can save state as well. Static variables can be used to store state.A static block can be used to initialize state. Static methods can be used to access or modify the state.AFAIK, the only think a purely static class cannot do is to force parameterization of the initialization of the class, since any method call can trigger the static initialization block. Such forced parameterization can be provided in the singleton by only providingan .instance() method with the desired arguments in the parameter list. the Singleton pattern in that a Singleton class is supposed to have one (and only one) *instance*, while a "static class" is never instanciated (or, at least, there is no point in instanciating it). a class full of static methods can't be polymorphic, but a singleton can. The Singleton pattern has several advantages over the "static class" pattern. First, a singleton can extend classes and implement interfaces, while a static class cannot (well, it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously whilea static class is generally initialized when it is first loaded. A sinlgeton class can be extended and it's methods overidden. Perhaps the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. For instance, assume you have a Configuration class that holds some global configs. Methods that use this configuration information may be defined as: public void doSomething(Configuration config) {...} Labels: Singletone, static |
Sunday, March 23, 2008
What is the difference between Apache struts and jakarta struts?
| Both apache struts and jakarta struts are one and the same.Struts was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000.However after gaining prominence in the J2EE community through its evolution as a Jakarta Project, in early 2004 it become an official Apache Project.Thus there is no difference between them. Labels: Apache struts, jakarta struts |
Saturday, March 22, 2008
Why is it not advisable to catch type “Exception”?
| Exception handling in Java is polymorphic in nature. For example if you catch type Exception in your code then it can catch or throw its descendent types like IOException as well. So if you catch the type Exception before the type IOException then the type Exception block will catch the entire exceptions and type IOException block is never reached. In order to catch the type IOException and handle it differently to type Exception, IOException should be caught first (remember that you can’t have a bigger basket above a smaller basket). The diagram below is an example for illustration only. In practice it is not recommended to catch type “Exception”. We should only catch specific subtypes of the Exception class. Having a bigger basket (i.e. Exception) will hide or cause problems. Since the RunTimeException is a subtype of Exception, catching the type Exception will catch all the run time exceptions (like NullpointerException, ArrayIndexOut-OfBounds-Exception) as well. Labels: Exception, exceptions |
Friday, March 14, 2008
Spring IOC and its benefits
| What are the different types of IOC (dependency injection) ? There are three types of dependency injection: Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters. Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods). Interface Injection (e.g. Avalon): Injection is done through an interface. Note: Spring supports only Constructor and Setter Injection What are the benefits of IOC (Dependency Injection)? Benefits of IOC (Dependency Injection) are as follows: Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration. Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test. Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own. IOC containers support eager instantiation and lazy loading of services. Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc. |
Thursday, March 13, 2008
What's New in Java 1.5?
| Sun has recently "upgraded" the Java language to include many new features such as Generics, Type-Safe Enumerations, Automatic Boxing and Unboxing, Annotations, For/In loops, and Static Imports. How will this affect the way you teach APCS? Immediately, it will have a minimal effect, but as you need to expand the scope of your APCS course to encompass the new features it might be difficult to find time. I'm going to discuss the current effect 1.5 has on APCS as well as possible future implications it may have on your course. for more detail read..http://www.cs.indiana.edu/classes/jett/sstamm/ Labels: java 1.5 |