Tag Archives: java

Design Pattern: Command and State together

In this article we will explain two JAVA design patterns:
Command and State.

In the main method we:

  1. first create the light object (aka receiver);
  2. then we check if the light is on or off;
  3. after that we create a light command passing to it the light we’ve just created;
  4. then we create a Remote Control and set the command into it;
  5. in the next step we press the button and we check the light state;
  6. finally, we repeat the process and check the light state again.

So lets start with the  code:

Here we have the client class (the one that have the main method)

 

The Command Design Pattern implementation
Here we define our Command interface

 

This is the remote control class.
Note that in the pressButton method we actually execute the Command passed previously in the set method. Notice that this class has no knowledge at all about the receiver. Instead it just knows that it has a Command and how to interact with it.

 

Here we have the concrete  light command (the implementation of the Command interface). In the execute method we specify what the command really does: in this case with the help of the State Design Pattern we are simply saying to the light to go to the next state.

 

The next section is the State Design Pattern part
This is the Light class. From the Command point of view this is the receiver and from the point of view of the state design pattern this is the object that keeps the current state of the light (also known as a wrapper that will be passed to the concrete states).

 

This is the state interface. Notice that here we have two methods that have to be implemented by the classes that implement the state interface.

 

The next class represents one of the possible state of the light (the concrete state that represents the ON state of the light)

 

This is the other possible state (the concrete state that represents the OFF state of the light)

 

And the results:

 

And that’s it. Feel free to comment.

Thank you!

Working with nested Maps in JAVA

In this post I will show you a simple and easy way of working with JAVA Maps, more precisely, how to add an element (a.k.a value) to a multi nested maps:

That’s it. Simple, isn’t it? 😉

Source code: github

Builder Design Pattern

Here is the “usual” implementation of this design pattern.

 

The result is:
builder2

UML
builder2_uml

 

 

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.

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.

Simple CDI Events Example

In this post I will show you how to produce a CDI event and how to handle it even if it is deployed in another application.

The Event class

A simple JSF Page

This page only shows a simple message from the backing bean (the call to the bean produces the bean’s initialization).

Backing Bean

Here we Inject an event (LoggerEvent) via CDI Inject annotation. Then inside the init method annotated with PostConstruct we fire the event with a new instance of the event. This is the simplest way to fire a CDI event. No matter who are listening to this event will be notified.

A simple Logger Event Handler

This class will listen for events annotated with the keyword @Observes. Because of this, it will execute the method. In this case it will print to the console a log message passed to the constructor of the event instance when it was created: Hello from CDI events as you can see in the next picture.

Result (console result)

cdi events
cdi events

AS you can see, it’s just that simple to create a CDi event and listen to it. This technique uses the Observer design pattern here with the help of @Observes annotation.

Feel free to comment. Download the souce code CDIEvents.

Thanks.

Static Binding vs Dynamic Binding in JAVA