Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on “this” object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different objects, usually written in the same line.
For example we can build a rectangle class using using the Method Chaining for setters. You can see the setters are a little bit different than regular setters:
class ChainedRectangle
{
protected int x;
public ChainedRectangle setX(int x) { this.x = x; return this; }
protected int y;
public ChainedRectangle setY(int y) { this.y = y; return this; }
protected int width;
public ChainedRectangle setWidth(int width) { this.width = width; return this; }
protected int height;
public ChainedRectangle setHeight(int height) { this.height = height; return this; }
protected String color;
public ChainedRectangle setColor(String color) { this.color = color; return this; }
}
Then if we need to build a rectangle we can write a single line for setting all the required values:
new Rectangle().setX(10).setY(10).setWidth(13).setHeight(15).setColor("red");
Along with autocomplete option in most of todays IDEs method chaining might provide a suggestive way of doing some actions in less code.
Read the rest of this entry »
I was starting today to look on different approaches and techniques that are used in scalable web or non web applications. One technique used in many large systems is simply called “memory cache”. It means that data are cached in memory so the data will not be queried again.
Cache and memory cache exists for a long time; even the hardware parts link hard disks cd units have some sort of memory cache.
Why memory cache becomes so important when we talk about web applications? It’s simple. Because web applications happens to fulfill thousands of requests simultaneously. Or sometimes they have to. It’s obvious that keeping the data into memory and reuse it the next time you need it it will improve the performance. And it looks very simple. At the first view simple hashmap would do the job, unless…
There a few facts we need to be consider in real world:
Using Google Guice in web applications can raise some issues. First of all when we talk about web applications we talk about servlets and we talk about managed environments. The creation of servlets in not controlled by us, when we write the application, it is controlled by the web application container. This generates some problems:
- Because we can not control the servlet creation we can not use Guice to inject servlet members in the classic way.
- Bootstrapping: we need a mechanism to bootstrap Guice into the application. The best way is to do it at start up before any http request will be invoked.
Read the rest of this entry »
I use windows and I hate when I have to switch on another computer, or when I have to do any change related to windows especially changing the environment variables. I like to download the tool, framework, … I use as a zip and then to do minimal operations to install it. The thing I hate the most is setting environment variables, and path in that small dialog window. I have all my java applications and frameworks in one single directory so the configuration depends only on the thing I hate most: thats right, environment variables.
Read the rest of this entry »
What?
There are various reasons when we have to wrap java exceptions. Often those reasons are called: Checked Exceptions. According to the definition there are 2 types of exceptions: Read the rest of this entry »
I get tones of exceptions in my code because I tend to use Generics in the old-fashioned way. I have my reasons for that. And I have tons of warnings like those:
04 Aug
Posted by: admin in: Java, java snippets, java web apps
There are two options to store parameters in web.xml:
- context parameters – available to the entire scope of the web application
- init parameters – available in the context of a servlet or filter in the web application
There are several options to handle the Java properties files:
The properties files can be stored in the classpath. This way they can be put inside jar files and it’s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn’t work: Read the rest of this entry »
30 May
Posted by: admin in: Design Patterns, Object Oriented Design
The visitor pattern can be used on structures of objects which implements a specific interface defining a method called accept. In practice, in many cases, the structures are already created and we have to visit structures of already created objects. Changing hierarchies of classes for adding a new method is not a viable solution.
We need somehow to extend the structure of objects for accepting the visitors without changing them. A way of doing it would be to add a wrapper for the hierarchy classes which accepts visitors and duplicates the structure.
20 May
Posted by: admin in: Eclipse
I’ve installed the eclipse plugin for bazaar. For me Bazaar is installed in C:\Java(don’t ask why I put it there, because I don’t know).
Read the rest of this entry »