Friday, November 7, 2008
How to extend the Base Class
Public class BasePanelClass()
{
private List components;
private List labels;
Protected void createFields()
{
// create component add it to the panel
addComponent(component1, label1);
.
.
addComponent(component2, label2);
.
.
.
}
/**
* This method adds component and label to some collection.
* Lets say draw() method will later use this collection to iterate on all components
* and put them configured layout on the panel to show
*/
Private void addComponent(component, label)
{
labels.add(label);
components.add(component);}
}
}
Now I can solve this thing by many different approaches.
i) From createFields() I can call some new protected method say addNewField(). This call will be made from point where this new field should be added. It will have empty default implementation in base class. There will be child class for this scenario say Child1PanelClass which will override this method to add required field.
ii) I can make use of flag to decide whether to or not to add required field. This even doesn't need subclassing.
Both of these approaches may serve the need and even second approach may seem like least work required and job done. Because when it comes to some very old applications where people already having nightmares maintaining them everyone wants to keep changes in code as minimum as possible.
However this is not good idea to do that. Adding addNewField() method to base class is totally irrelevant to other flows. What if some other flow later needed to add some other field in same panel. With these 2 approaches we will end up in touching panel base class code. Which may impact other scenarios and certainly will make base class messy.
So what we need to do is go back to basics of inheritance. Base class should represent things which are common to all childs. There should not be dependency downwards from base class to child class either by some empty protected method which very specific to some child class or by some conditional checks which needs to be maintained every time there is change in some child class's needs. This definitely violates dependency inversion principle.
Better way to do that can be like this. We have createFields() method in base class which adds up components to some collection. Order of the components in collection determines order of the corresponding fields on the panel. And our need is to add field somewhere on the panel for specific need.
So in base class we can now add method addComponentAfter().
/**
* Adds given component with label "label" after given index
*/
Private addComponentAfter(index, label, component)
{
labels.add(index + 1, label);
components.add(index + 1, component);
}
I defined new child class for this scenario Child1PanelClass.
Public class Child1PanelClass()
{
@overrides
Protected void createFields()
{
super.createFields();
// create field specific for this need
// add it after index
addComponentAfter(index, label, component);
}
}
So I have overridden createFields() and let my base class add fields in its way. After that added my new field in required order.
With this my base class doesn't need to be changed for any further change in some other flow. Also we have encapsulated the change for this flow and limited it to its own class.
This suffices Encapsulate the Change principle.
There can be question of base class changing order of components. Which may affect child class as it relies on ordering of fields decided by base class. However this dependency is not harmful as child class are always supposed to depend on their base. Which sounds obvious also. This is what called as Dependency Inversion.
To Summarize, There are some principles that should be followed when there is extension required to system. Decision of whether to use flags, some protected methods or subclassing with dependency inversions depends really upon many factors like time availability for the change, future change anticipations, extent of reuse etc. We have to consider all these factors before really deciding with the approach. But I prefer to do change which are following open-closed principle, dependency inversion, encapsulate the change principles. Because what we all need is system which is more stable and at same time easier for extensions.
Monday, June 2, 2008
Chaining the responsibility
Requirement is to route the message to proper backend. Input is payment instruction data. This is about determing based on type of instruction where exactly we have route message for further processing.
For this requirement i could have used simple configuration and based on some parameter i could have decided which backend handler i will delegate to. But some times this logic is complex and dificult to configure. Consider for some group of message types deciding factors may be different than other group. Morever you dont know change in logic and support for new backends.
This was guiding factor in going for chain of responsibility pattern. Here i kept chaining configuration in xml and commons api chaining support executed the chain.
Member of chain are dedicated to respective backends. Which receives same context information from chain executer and decides whether they are able to accept the message or needs to route to next member of the chain.
I can even use one chain member for more than one related backend.
Also having chaining configuration it is easier to modify chain flow.
More on multithreading
Consumer (Runnable)
Thursday, May 29, 2008
Patterns born from design principles
I have seen and coded several design patterns in my application. There are Singeltons when some thing is heavy to initialize or act as connection between 2 systems. There are strategies when i have to attach dynamic behaviour to the class. There are command patterns when i configure pluggble extentions in some xml and code decides which extension to use to serv current request. I have seen chain of responsibility when i have to try one by one of available options and go with first matching.
All these pattern are based on design principles. Actually to follow some design principle to solve current problem we end up in using some pattern. eg. To solve requirement where class wants to behave as flyable and later behave as quackable we have to encapsulate the change and delegate the request to particular implementation at runtime. These design principles combinely forms Strategy pattern.
Now based on different intent and different implementations there are different variations in the pattern. Strategy talks about switching between algorithms dynamically. Where decorator pattern talks about extending behaviour at dynamically. Both uses delegation but their intent is different. Both have change encapsulated in respective implementation. But the intent is totally different.
Strategy pattern Usage:
When requirement is to vary behaviour of the class dynamically we cant go for inheritance as it is tied to instance type. Strategy pattern is about separating dynamic behaviours from the class and proving way of switching between them.
I have used this pattern as follows:
Requirement:
There are 2 rate type trade booking. Real time trade booking and bench mark trade booking. For both booking there are 3 possible flows in which application can come from. All 3 flows have different behaviour for implementation. But most of the code is common for same flow in different rate types. So if I go for inheritance I will need to create 6 subclasses and any change in say flow1 has to be replicated in flow1 of other rate type as well.
Implementation:
Common abstract class for both rate type
Common abstract class for all flow types.
2 Implementation for AbstractTradeBooker
i) RtTradeBooker
ii) BMTradeBooker
3 implementation for AbstractTradeFlow
i) Flow1TradeFlow
…
Now any change in processing which is totally depedent on rate type can go in Implementation of AbstractTradeBooker and any change in some flow will go in particular implementation of AbstractTradeFlow.
There will be abstract method or may be constructor defined in AbstractTradeBooker to set instance of AbstractTradeFlow.
Also we can use template design pattern inside these abstract base classes which will define generic steps for processing and let implementation override and change implementation of specific step.
So here we have encapsulated part that is changing inside different trade flows.
Second requirement:
Consider your web application can forward request to some other application when user clicks on some link. Consider there are 2 ways of authentication of user to the new application. One can be using cookies and other using web services.
Now to decide which strategy to be used for authenticating user to this application depends upon some user level flag maintained by parent application. Based on this flag we have to decide dynamically how we will be authenticating this user to requested application.
This can be solved by having 2 implementation of the AuthenticationService. CookieBasedAuthentication and WebServiceBasedAuthenticationService and decide which instance based on the user flag at runtime.
Tuesday, May 27, 2008
Clustering
Facts about web application deployed on clustered environment.
There is one instance of ServletContext, Servlet per JVM.
There is only one instance of HttpSession in distributed application for given user.
Attributes inside session implementing Serializable interface are replicated to different JVM’s.
So interesting thing is how and when session is propogated and
other things that are dependent on this behaviour.
Like how HttpSessionActivationListener comes into picture with all this. What does it mean by session activation and session passivation.
We will see how weblogic goes about implementing this.
Weblogic has the term called Sticky Sessions. This means weblogic takes care that all request of user during session active time are transferred to same instance of the server.
So when does propagation of session occurs.
Session replication happens always when you set attributes to session using setAttribute() method. Whenever attribute is set to using this method container replicates delta in session to the secondary server. So even though Weblogic uses sticky session technique when the instance to which user session is stick is failed container forward request to secondary session where user sessions replica is already maintained.
More details for this technique are available at: http://edocs.bea.com/wls/docs90/cluster/failover.html
So now about HttpSessionActivationListener. This is binding interface that attributes stored in session should implement if they want to be called back by container in case of session activation or passivation. Containers migrate sessions to different JVM's or persists them into database. Attributes implementing this interface will be called by container when it performs these activities.
Multithreading in real world
Mostly in these processing ordering doesn’t matter. So instead of processing sequentially we can make use of threads to process them (virtually) parallel.
Consider there is system which processes user scheduled actions by web application. It is commonly known as job scheduler.
Requirement here is that user triggers some activity from UI application. When these triggers should be executed and all that logic depends on scheduling policy for that trigger. So there should be one monitoring thread which will be checking when to execute that trigger based on this scheduling policy. Now this execution can take some time for execution which is completely unknown to executer. Also there can be more than one job ready for execution at that time. So it is necessary for executer to execute this job in separate thread and return back it its actual monitoring work.
So main multithreading concepts used by executer. Executer uses threadpool. Threadpool has some configured number of threads configured which keeps monitoring some jobqueue. Whenever thread finds some job in the queue it removes it from queue and owns it for its execution. Then it continues polling.
There can be other approach to this implementation. Like wait-notify approach. Thread will wait for some condition and then will be notified to all by main monitoring thread.
Thread pool size is configured based on load of processing. If there are more messages to be processed simultaneously to reduce response time then we can increase the thread pool size. Thing to remember is that thread creation is heavy process and should be minimized by reusing the thread.
This wait-notify or task queue approaches works when triggering and triggered both thread are running in same JVM. Mostly the case is where these triggering threads are in different JVMs. E.g. Web application with which user is interacting is one thread of process running in container. Actions triggered by this user interaction may cause job monitor to execute some job in some other JVM. This communication can be easily handled by database. User interaction with the screen will create entry in database and background job scheduler will get triggered from this database entry.
Main problem is when we want to notify immediately to job scheduler to execute some critical job. You can’t use wait-notify or job queue approach. One way to do this is to use Oracle signals. Concept is almost same as that of wait-notify only difference is in wait-notify we wait on object locks which are bounded to its JVM but in this case we can wait-notify at database level so spanning more than one JVM’s.
Sunday, May 11, 2008
ECLIPSE RAP
Eclise RAP
Eclipse as many people knows is about defining bundles (plugins) and extending the system. With OSGI it goes one step further and allows to add bundles to system at run time. This is what adds value to its architecture. You can add bundle to system, you can stop it, you can redeploy it and many more. This means no restart. This will sound familiar to J2EE developer. Containers supports similar kind of feutures. But difference with OSGI you can redeploy changes in class files without restart which otherwise may need restart on J2EE container.
Eclipse Rich client Ajax platform is platform which allows developers to create rich ajax ui application using their knowledge SWT development and (J2EE) servlet technologies.
Actually j2ee details is mostly encapsulated by underlying RAP framework bundles.
So thing here is that you can develop application working on AJAX, coded in JAVA SWT and runs in browser.
Commucation of SWT code to html is done by underlying RWT framework and qooxdoo framework. Developer actually codes in RWT which has same interface as of SWT and qooxdoo is javascript library which handles user events on browser and communicates with widgets on server side.
I just went into code to find how it serves the request. This is what i understood about them.
It started with url : http://localhost:8080/rapdemo/rap.
Where I have deployed one web application with context path “rapdemo”. And one servlet mapped to path /rap.
Rap application has lauch configuration file where we can specify servlet name.
Eclipse-equinox bridge receives request from client on server. This is also one bundle which is like ActionServlet in struts as it receives http request and then passes control to eclipse bundles. Deployed war file has required bundles. Based on alias “rap” in request control is transferred to proper UI with default entrypoint.
RWTDelegate is the servlet which receives request for rap applications. This servlet is mapped to path “/rap”. This servlet transfers control to RWTLifeCycleServiceHandlerSync. Here you can find more on RWTLifeCycle: http://wiki.eclipse.org/WidgetToolkit. This is about lifecycle thread that is started for this web application. This is the thread that serves user requests.
"Render: is where the markup or javascript assignments are created, which will be sent to the browser. During the actual rendering no changes of the widget-tree will happen - neither creation or addition of new widgets nor changing of attributes of existing ones. The AJaX-rendering-mode only creates assignments of those widget-attributes that were changed during the request-processing. This results in a minimal amount of information that needs to be exchanged between server and client."

