

|
|
To me Java means a way of living with technology.
1. Java-Thread-Deadlock
2. Wizard Framework idea using Java Swing API
3. Java Thread Safety and design case study, with example
4. Event Handling using Java code
5. In-Memory Image creation example code using Java Technology
6. Java ThreadPoolExecutor example
7. Java Thread and using Wait and Notify example
8. Thread and Synchronization with example
9. Java and RMI Tutorial with example code
10. Java Comparator API example
11. Drag and Drop functionality implementation using Java Technology
12. Java Interview Questions
I have been working on Projects on Java Technology for past 10 years,
and have been following and going through many features those
are getting included in newer versions of Java and many
Open source Frameworks getting created and listed in many sites with varying
areas to talk about.
Like for MVC pattern, we have Struts, Spring MVC and
many more.
For persistence layer, there are ORM tools/Frameworks
like Hibernate, Oracle's Toplink and many more.
I have created this site to talk about some of the specific
technical challenges I had faced or facing in my daily
time spent on technology.
I think some of us might be benifited by this techno-Knowledge
sharing pages on this site.
This article will try to explore Command Pattern
and its usage in a scenario, and some of the comparison
and benifits discussed.
We as software programmer might have come across sometimes
where we need to work on a growing switch-case/if-elseif-else
type of code whereby we have to actually check for the type
of function/call embed in an input parameter/argument supplied.
For example let us take a case study where we have to perform
CRUD operation on some table in database.
By CRUD I mean, Create-Read-Update-Delete operation based on
values from some value object.
So most probably I shall obviously think of writing a service
implementation, where there will be three if-else-if condition
and on the basis of these conditions, there will be separate
calls for working on Create, Read, Update, Delete operations.
But just think, what will happen when I have to try out
CRUDMA, here M stands for Merge, A stands for Add, then
I have to write two more Else-IF conditions and this may grow
if in future I have to keep on adding more such functionalities.
So in this scenario, I can think of these operations as Commands
and can leverage GOF Command Pattern, and try to see how it can
reduce code duplicates, change and mantainance of existing code,
extensibility and many more.
For example :
This example is using the if-else-if
ExampleService.java
package example;
public class ExampleService {
public void execute(String opName, Object valueObject) {
if(opName.equalsIgnoreCase("CREATE")) {
//do something with this type of
//operation and use the valueObject
//for Data.
} else if(opName.equalsIgnoreCase("READ")) {
//same here
} else if(opName.equalsIgnoreCase("UPDATE")) {
} else if(opName.equalsIgnoreCase("DELETE")) {
}
}
}
Let us revisit this example with a different angle with
a pattern/better approach :
In this new approach I havn't used if-else-if statements,
instead pass on this task to the client code to decide
which command it is going to call for any specific request
from the requestor.
So for each CRUD operation, there will be a command, for example
for Create operation, there is a CreateCommand, and this is
implementing a common Command interface as shown below:
package example;
public interface Command {
public Object execute(Object obj) throws Exception;
}
package example;
public class CreateCommand implements Command {
@Override
public Object execute(Object obj) throws Exception {
System.out.println("inside create command");
return null;
}
}
Now this create command will be executed by a common task
executor, such as follows:
package example;
public interface Task {
public Object execute(Command cmd, Object task)
throws Exception;
}
package example;
public class TaskExecutor implements Task {
@Override
public Object execute(Command cmd, Object task)
throws Exception {
return cmd.execute(task);
}
}
In order to test this example, I have written a
test client as follows:
package example.test;
import example.Command;
import example.CreateCommand;
import example.Task;
import example.TaskExecutor;
import example.vo.CreateVO;
public class TestCommand {
/**
* @param args
*/
public static void main(String[] args) {
Command createCommand = new CreateCommand();
CreateVO createVo = new CreateVO();
try {
new TaskExecutor().execute(createCommand, createVo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
If anything missed out , please let me know at
techienjoy at yahoo . com
References :
Tags: java comparator reflection
Tags: java example drag n drop
Tags: Java Interview Questions
Tags: java rmi tutorial stub skeleton
Tags: Java Thread Deadlock
Tags: Java Thread Design Scenarios
Tags: Java threadpoolexecutor
Tags: Java
DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com.
techienjoy.com is not liable for any negative consequences that may result/arise from
implementing directly/indirectly any information covered in these pages/articles/tutorials.
All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.
In spite of all precautions taken to avoid any typo in these pages, there might be some
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.
|
| 

|