Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Friday, August 29, 2008

Load jar files and Java classes dynamically

In this post I present a way to load jar files and Java classes dynamically from java code. It can be very useful, if you want to create module, for example.

Design Pattern
The solution build up from three part, the
Loader - loads and uses the jar files and Java class dynamically,
Communication interface(s) - defines the interface, what can be used to communicate the program and the dynamic library,
Dynamic libraries - the Loader loads and uses this libraries through the communication interface(s).

The loader, the communication interface(s) and the dynamic libraries must be separated in different projects, because
  • Loader imports the communication interface(s)
  • Dynamic libraries imports the communication interface(s)
  • Loader can't know anything about the Dynamic libraries
  • Dynamic libraries can't know anything about the Loader



Sample application
As usually, I made a sample application, what can help to you to understand my idea. The sample application is very similar now. The main program can be invocated with two parameter, the first is the name of the jar file to load, the second is the name of the class to load. The application loads the given jar file and class and say hello in two different language, depend on, which jar file is loaded.

Creating the communication interface(s)
There is only one communication interface, what defines only one simple method:



Creating a dynamic library
The sample dynamic library is as simple as the communication interface:


Creating the Loader
Sample application has a main class, what processes the program arguments:


and a class, where the codes are separated, that load the jar file and class dynamically:


Downloads
You can download sample projects here as NetBeans project.

Monday, June 23, 2008

Mediator Design Patter

I present this post an useful adaptation of Mediator Design Pattern. We need often create GUI forms and of course we want to process the information given by users.

Participants of Mediator Design Patter
(based on Wikipedia)

Mediator - defines the interface for communication between Colleague objects.

ConcreteMediator - implements the Mediator interface and coordinates communication between Colleague objects. It is aware of all the Colleagues and their purpose with regards to inter communication.

ConcreteColleague - communicates with other Colleagues through its Mediator



Step 1: Create the Mediator interface
In the 1rst step we create the mediator interface:

1. public interface Mediator {
2. void setData( HashMap<String, Object> data );
3. HashMap<String, Object> getData();
4. }

Import the required classes and our Mediator is done.
Object will communicate over this interface with each other.


Step 2: Create the form
We create a simple GUI frame with two JTextField and two JButton, like this:



Step 3: Create the ConcreteMediator
To create the ConcreteMediator, implement the Mediator interface with our GUI Frame.
Change the
public class MediatorExample extends javax.swing.JFrame {

line to
public class MediatorExample extends javax.swing.JFrame imlements Mediator {

and implement the required methods, declared in Mediator interface:
public void setData(HashMap data) {
textfield_info1.setText((String) data.get("info1"));
textfield_info2.setText((String) data.get("info2"));
}
and
public HashMap getData() {
HashMap data = new HashMap();
data.put("info1", textfield_info1.getText());
data.put("info2", textfield_info2.getText());
return data;
}

Import the required classes and our ConcreteMediator is done (textfield_info1 and textfield_info2 are the name of two JTextField instance variable).


Step 4: Create the ConcreteColleague
In our example the ConcreteColleague has two function:
it can open a new Frame and fill it with initial data throught the Mediator interface
it can process form data throught Mediator interface.
My code:
public class ImportantDataProcessor {

public void showImportantDataDialog() {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("info1", "This information is very important!");
data.put("info2", "And Top Secret!");

MediatorExample frame = new MediatorExample();
frame.setData(data);
frame.setVisible(true);

}

public void processImportantData(Mediator mediator) {
HashMap<String,Object> data = mediator.getData();

String info1 = (String) data.get("info1");
String info2 = (String) data.get("info2");

JOptionPane.showMessageDialog(null, info1 + "\n" + info2, "Important Information",
JOptionPane.INFORMATION_MESSAGE);
}
}

Import the required classes and our ConcreteColleague is done .


Step 5: Process the form
Now we write the onClick() events of the two buttons.
Exit button will close the application:
this.dispose();
and the Process... button will start the form processing:
new ImportantDataProcessor().processImportantData(this);
The "this" word refer to the GUI frame itself, what implements the Mediator interface.


Step 6: Start the application
Create a class, named Main, and put the following code:
public static void main(String[] args) {
new ImportantDataProcessor().showImportantDataDialog();
}


Downloads
You can download sample code here as NetBeans project.