Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

Monday, October 6, 2008

Merging state and command pattern with enum and interface

A post just for the fun :-) I would like to describe how to simply merge the state and command design patterns while using enum and interface in java.

We have for instance a class having a changing state :
public MyClass {
protected IState currentState = new MyState.INIT;
...... // other instance variables
...... // some methods

public void myMethod() {
if(aCondition)
currentState = currentState.nextState();
else
currentState = currentState.previousState();
currentState.execAction();
}
}


We can define the interface IState as follow :

public Interface IState {
public void execAction();
public void undoAction();
public IState nextState();
public IState previousState();
}
A dummy state for the fun :

public enum DummyState implements IState {
NIL ; // only one state

public void execAction(){}; // nothing by default
public void undoAction(){}; // nothing by default
public IEtat nextState(){
return this;
};

public IState previousState(){
return this;
};

}

And then a state MyState :
public enum MyState implements IState {
INIT {
public IState nextState(){ return IN_PROGRESS;}
public IState previousState() { return INIT;}
},
IN_PROGRESS {
public IState nextState(){ return TERMINATED;}
public IState previousState() { return INIT;}
},
SUSPENDED {
public IState nextState(){ return IN_PROGRESS;}
public IState previousState() { return IN_PROGRESS;}
},
TERMINATED {
public void execAction(){
Action anAction = new Action();
anAction.exec();
actionsDone.add(anAction);
};
public IState nextState(){ return ARCHIVED;}
public IState previousState() { return IN_PROGRESS;}

},
ARCHIVED {
public IState nextState(){ return DummyState.NIL;}
public IState previousState() { return TERMINATED;}
}


Vector actionsDone = new Vector();

public void execAction(){}; // nothing by default
public void undoAction(){}; // nothing by default

public IState nextState(){
return this;
};
public IEtat previousState(){
return this;
};
}

And finally the Action :

public Class Action extends Command {
public exec() {
//something to do
}
}

Sunday, September 28, 2008

PureMVC web framework

When developping a web application, we all know that we have several parts to focus on ; presentation flow management, business rules development, data persistence, and so on... It is generally called N-Tiers architecture, N being function of the complexity of the application.
J5EE for instance aims to cover all the scope of web application concerns.
For the ones who don't want a all in one framework, or who develop with other languages than Java, it exists many "little" frameworks which focus on a particular part of the web application development problematics. It is the case especially of Struts, or Wickets, or Spring, etc...
This is in this context that the PureMVC framework occurs ; its scope is only to manage the presentation logics, ie how the developper has to design his application to manage views, actions, controls, ..., in the web pages. It is clearly a replacement of Struts and it is based uppon clear explained design patterns of the Gang of 4 : proxy, médiator, command, facade, observer ; five design patterns for building simple and well structured web applications from the presentation point of view.
The "plus" of the techno is that it has been derived for many languages : Php, Ruby, Java, Flex, ... We may even consider now developping an entire application in different languages (for instance Php and Java) with the same underluing concepts... It could be the case in a SOA approach, eg a web portal (public access) in Php which would focus on referencing, and in java/gwt for the business part of the application (private access).
Another interesting view is that it is ,by construction (it is only based uppon the use of design patterns, not libraries), compatible with other frameworks like Spring or Restlets...
For the best, I would like to make notice that PureMVC has been derived for GWT, ie indirectly for Javascript :-).
So let imagine the following global architecture :)
  • GWT for the graphical part
  • PureMVC4GWT for controls and UI logics
  • Restlets for ressource access on the server(s)
  • OSGI for managing component efficiently
  • DB4O for storing data in database
Is it not a nice proposal? ;-)

Sunday, August 31, 2008

GWT 1.5 is out

GWT 1.5 is now officially out and improves greatly GWT capabilities and features. We can say from now that GWT reached a mature state which invites you to build professional applications with.
It is too long to enumerate all the features included in this version, but the main ones are the following:
  • Java 5 language support
  • Performance optimizations
  • Easier JavaScript interop
  • Prettier widgets
  • Accessibility(ARIA compliant)
Have fun trying GWT for your web developments!