Builder Design Pattern (simple way)

This design pattern allows us to create an object with the advantage that the same construction process can create different representations of that object. Here is the simplest implementation of this design pattern.

In a next tutorial we will explain the other way of doing the same thing, with more flexibility but with a more complex implementation.

So, let’s start creating a car!

 

The final result:

builder

Singleton Design Pattern

With his design patttern we allways get the same instance, as you can see next.

  1. The Singleton class:

 

2. The main class:

 

3. The result:

singleton

 

UML
singleton

Reverse String/array recursively

Here is an example how to recursively invert the order of a string or an array in Java.

The output is:

output

Notice that in the string method we have twice the iterations that we have in the array method, because we go through all positions in the string.

If you know other ways of doing it, please feel free to comment it out.

Thanks.

Hibernate / JPA 2 Persistence Annotations Tutorial

from Pocket http://ift.tt/1FvqQ7Z

This Hibernate (or JPA 2) Persistence Annotations Tutorial contains overview of all important annotations which you may need while annotating your java POJOs to make them act as persistent JPA entities.

You think you know everything about CDI events… Think again!

from Pocket http://ift.tt/1poOQBY

CDI events are one of the shiniest feature in the CDI specification. They are a easy to understand and use and are a straight forward implementation of the Observer Design Pattern.

EJB 3.1 @LocalBean vs no annotation

from Pocket http://ift.tt/1sRGAJO

The rules are (from memory): Bean has a @LocalBean annotation -> bean has a no-interface view Bean has a @Local annotation -> bean has a local view Bean has a @Remote annotation -> bean has a remote view Bean has no view annotations, but directly implements an interface which has a @Local annotatio

Decoupling event producers and event consumers in Java EE 6 using CDI and JMS

In this post I will show you how to decouple event producer from the event consumers (aka async events).

  1. you must configure your application server (here the glassfish settings):
  2. call the EJB method sendEvent() to send the event (from a servlet or from a web page):
  3. the EJB fires the event anotaded with @InForeground qualifier.
  4. An observer method handle the event and send it to the JMS Queue with the help of a message producer:
  5. Now, in another (background) thread, the onMessage method handle the message and fires a new event (which wraps the received event) now annotated with @InBackground qualifier:
  6. And finally, if you have a method that consumes events annotated with @InBackground qualifier, then you receive the event and process it accordingly to your business needs.

The event object it’s just a POJO like this one:

You can get the full code from github. Thank you!

Feel free to comment.

JAVA Dynamic Binding – Another Example

In this post I will show you another interesting example of Java Dynamic Binding…

dynamicBindingExample
dynamicBindingExample

Did you expect such behavior? Feel free to comment. Thanks.

Cómo crear una API REST usando Node.JS

from Pocket http://ift.tt/1i8gtba

En esta entrada explicaremos como crear un Web Server que sirva una API RESTful usando para ello la tecnología de Node.js y todo el JavaScript que podamos, incluyendo la base de datos (MongoDB) y el plugin para conectarnos a ella y mapear los modelos (Mongoose).