April 18, 2010

Which pattern is this?

I've been working in a component with some singularities, one of them is his incremental behavior. To make it clear: When a feature is almost complete (or completed), some other features arise.

This is really annoying if you have implemented everything for an specific use (never do this, I've learned in the bad way) if needs to be changed to work with the new stuff.

One of the things that I needed to do is to create/fill/validate different objects; I started thinking: "This is easy, let's do a mapper for each one of them". So far so good, then another type of object to work with.... shit, create more mappers, modify logic...

I started thinking "I'm doing this over and over, why am I so stupid?, use interfaces, builders, factories, simplify the problem... moron". I realized that the validation/filling... logic was the same, so the things that are different  needed to be isolated: the mappers (I don't know the real name).

With a little of work, I did this:
public interface Initializer<Source,Target>{
     // initializes the Target object using the Source as data provider 
     Target initializeThis(Source source, Target reference);
}
Then my object creator is:
public class ObjectCreator<Car> implements Creator{
    public Car prepareObject(){
        Info info= readInfo();
        Initializer init = InitializerFactory.newInitializer(Car.class,Info.class);
        return init.initializeThis(new Car(),info);        
    }
}
The logic doesn't changes, just use the proper Creator, and that's it. But I don't really know if this is a pattern, or an anti-pattern, or whatever.

April 14, 2010

Hace un año

Hace exactamente un año, sufrí junto a mi hermano un accidente que hubiese sido fatal de no haber contado con las personas indicadas en el momento dado. Sobreviví no gracias a Dios o algun tipo de voluntad divina, fue gracias a mi hermano y a la gente que lo ayudo. Doy gracias a ellos y elijo no recordar a aquellos que nos ignoraron.

Tambien expreso mi profundo respeto por la familia Chavarria, que perdió a su único hijo en este incidente, ojala esto nos recuerde el valor de la vida.

April 13, 2010

Jakarta HttpClient , POST request

This library contains a set of very handy utilities for HTTP interaction, in this occasion, I'll show you how to create a POST request:

final String url = "http://localhost:8080/eco/management"
public void sendRequest(Map<String, String> parameters) throws DigestException,   HttpException, IOException {
        println("Sending request to : " + url);
        HttpClient httpClient = new HttpClient();
        PostMethod method = new PostMethod(url);

        for (Entry<String,String> entry : parameters.entrySet()) {
            method.addParameter(entry.getKey(), entry.getValue());
        }
        final int status = httpClient.executeMethod(method);

        if (status == HttpStatus.SC_OK) {
            println("Request successfully processed");
            println("Response : " + new String(method.getResponseBody()));
        }
    }