Friday, July 13, 2012

Strategy Pattern

Most of the time after the has been developed. We will be asked for adding new features for the existing software. In such cases, we have to spend more time to change the previously written codes and to change some logic as well. This will be a more time consuming process rather than designing a new software. We all know that object oriented designs are re-usable. But when we are designing something we have to put more concentration. Because our design should capable of catering newly coming requirements as well. If there is a possibility to change the behaviors at run-time we can use strategy pattern.

An example for Strategy Pattern

Lets say, we have a vehicle factory which produces different types of vehicles. Company is currently producing cars with patrol engine behavior and diesel engine behavior. Imagine they are looking forward to produce cars with some other engine behaviors as well. Let's say they need to produce the same type of a car with some other engine. This will be a huge problem if the behavior of engine is bound to the vehicle itself as an abstract function. Solution is as follows,

#ifndef VEHICLE_H
#define VEHICLE_H
#include "Engine.h"

class Vehicle

    enum Type
    virtual ~Vehicle();
    Engine* getEngine() const;
    virtual void setEngine(Engine* val);
    void startEngine();
    virtual Type getType();

        Type type_;

    Engine* engine_;
#endif //VEHICLE_H


#include "Vehicle.h"

Vehicle::Vehicle() : type_(UNDEFINED), engine_(NULL)


Engine* Vehicle::getEngine() const
    return engine_;

    delete engine_;

void Vehicle::setEngine( Engine* val )
    engine_ = val;

void Vehicle::startEngine()
    _ASSERT(engine_ != NULL);

Vehicle::Type Vehicle::getType()
    return type_;


#ifndef CAR_H
#define CAR_H

#include "Vehicle.h"

class Car : public Vehicle
    virtual ~Car();
    Vehicle::Type getType();

#endif //CAR_H


#include "Car.h"

    type_ = Vehicle::CAR;


Vehicle::Type Car::getType()
    return type_;


#ifndef ENGINE_H
#define ENGINE_H

class Engine
    virtual void start()=0;
    virtual ~Engine(){}
#endif //ENGINE_H



#include "Engine.h"

class DieselEngine : public Engine
    void start();


#include "DieselEngine.h"
#include << iostream >>





void DieselEngine::start()
    std::cout<<"Starting Diesel engine ....";



#include "Engine.h"

class PatrolEngine : public Engine
    virtual ~PatrolEngine();
    void start();


#include "PatrolEngine.h"
#include << iostream >>





void PatrolEngine::start()
std::cout<<"Starting Patrol engine ....";


    Vehicle* v1 = new Car();
    v1->setEngine(new DieselEngine());

    Vehicle* v2 = new Car();
    v2->setEngine(new PatrolEngine());

Output  :

 Starting Diesel engine ....
 Starting Patrol engine ....

Download Visual Studio 2008 Solution :  Strategy Pattern Sample in C++

Friday, May 13, 2011

Dynamic code generation in java

Following code helps to generate a particular java class dynamically from our code.

First create an Interface called "Base" which is as follows,

public interface Base{
           public void run();


Next step is creating the implementation class of the Base interface from our code which is named as "C".


public class Invoker {

  public static void main(String[] argv) throws Exception {

         String code = "public class C implements Base {\n"
                         + "   public void run() {\n"
                         + "      System.out.println(\"I am the new class\");\n"    
                         + "   }}";

         Class classB = Class.forName("C");
         Base b = (Base)classB.newInstance();         ;

public static void createClass(String code,String file) throws Exception {
               OutputStream os =
                         new FileOutputStream(new File(file));
               Process p = Runtime.getRuntime().
               exec("javac -classpath . "+file);

Wednesday, December 1, 2010

Recovering accidentaly removed PATH environment variable in ubuntu

By using following instructions you can overcome if you accidentally removed PATH environment variable from /etc/bash.bashrc file

first step:

Open a terminal and type following command

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

second step:

Remove accidentally added thing from the bash.bashrc file.

Sunday, August 29, 2010

Struts 2 Mapping a bulk of entries in a JSP to an array in an Action Class

To pass selected multiple entries which are in a JSP to the action in order to process all of them rather than processing one by one following sample codes can be used.

In the JSP code following has to be kept. Here it is having only the required fields to handle the this thing. Additional stuff has to be added by yourselves :
fileldValue : value in this will be mapped to a particular index in a "bulk" array which is in the action class here "key" is a long value in a request.

<s:checkbox name="bulk" value="%{#bulk}" fieldValue="{#key}"/>

In the action class there should have an array to map the selected entries in the JSP.

public class BulkHandler extends ActionSupport {

private long[] bulk;

public long[] getBulk() {
        return bulk;

public void setBulk(long[] bulk) {
        this.bulk = bulk;

* @return String which desides the next page
public String ProcessBulk(){

/* here you can process the bulk array which is having the long parameter values of every entries  which were selected and sent to the action from the jsp */

   return  SUCCESS;


Thursday, August 20, 2009

Crazy Microsoft

This is just a simple article. Here I am trying to give some kind of bugs which are not already being solved by the Microsoft developers. It is they are pretty cool. Just try them out by yourself.


An Indian discovered that nobody can create a folder anywhere on the computer with the name of "CON". This is something pretty cool ...and unbelievable... At Microsoft the whole Team, couldn't answer why this happened!


Magic goes here........

1. Open the note pad
2. Simply type "Bush hid the facts" without quotes
3. Save it as what ever you wish
4. Just close it and re-open it

Hope you enjoyed with the article.

Wednesday, August 19, 2009

Shell Scripting

Here I am trying to give kind of a basic idea for people who are interested in shell scripting. Today in computer world Linux is the most famous topic therefor I thought of writing something which is very helpful for Linux users.

What do you need to write Shell Scripts?
Simply you need only the Linux OS and it's bash shell (Available with almost all Linux Distributions. By default bash is default shell for Red Hat Linux Distribution).

What is Linux Shell?
Computer understand the language of 0's and 1's and this is called binary language. Shell is a user program which helps user to interact with the kernel. Simply it takes user's commands in English and if it is a valid command it is passed to the kernel. Commands which were mentioned above are Linux Commands.

What is a Shell Script?
Shell script is a series of commands which are written in a plain text file. Shell script something similar to MS-DOS batch file but it is more powerful than the MS-DOS batch file.

How to write your first Shell Script?
1. Use any editor like 'vim' to write the shell script.
2. After writing the script set the execute permit ion to your
shell script as follows,

chmod 755 your_shell_script_name

Note: This will set read write execute (7) permit ion to owner and for group and other permition is read and execute(5).

3. Here we go...Now the script can be run by using one of following three ways,

Assume your shell script name is "myFirstScript"
2)bash myFirstScript
3)sh myFirstScript

following are some examples for shell scripts.
example 1

prints "Hello World" on the screen.
echo "Hello World"
example 2
Script to print user information who currently login,current date& time
echo "Hello $USER"
echo "Today is ";date
echo "Number of user login :";who | wc -l
echo "Calender"
exit 0
#end of script
example 3
This read your name from the key board and print it.
echo "Your name please :"
read name
echo "Hello $name, lets be friend!"

If else in Shell Scripting
if condition
condition is zero (true - 0)
execute all commands up to else statement
if condition is not true then
execute all commands up to fi
example 4
if [ $# -eq 0 ]
echo "$0 : You must type one integer"
exit 1
if test $1 -gt 0
echo "$1 is a positive number"
echo "$1 is a negative number"
Run the above script as follows,
./script_name integer_number

assuming sript name is "test"
./test 10

output is as follows,
10 is a positive number

Detailed explanation
First script checks whether command line argument is given or not, if not given then it print error message as "./test : You must type one integer". if statement checks whether number of argument ($#) passed to script is not equal (-eq) to 0, if we passed any argument to script then this if statement is false and if no command line argument is given then this if statement is true.

Those are few examples for you to get an idea what shell script is. Hope you would enjoy with note. Finally there are lot more about Shell Scripts. Here hoped to give you a brief introducion.

Sunday, August 16, 2009

Who is responsible for this?

It was a bright sunny day. I got up early in the morning with heaps of hopes. It was my 5th birth day. I thought my father was there to wish me but... it was hopeless he was not there in my room and even in my home. I searched for some thing under my pillow,under my bed and on top of my cup board, for my birth day gift. But it was also hopeless because I could not find anything. Even though it was a bright sunny day . I felt that I was alone. I could remember that my father told me i would bring you a bicycle for your birth day. I don't want a bicycle dad please come to visit us. After few days there was a ceremony in our home and there was a huge black box in our veranda. There were lot of people in our place lot of them ware crying. I could see my mother was also crying near that box. I did not want to cry but.... I had to cry just because others were crying. Then after two days that black box was taken away from my home to a place called cemetery. Since I could not understand what was really happening there I asked from my mother "mother what was going on" so she said me that "your dad left all of us. he never come Just say good bye! to your dad " but I wonder what she was telling. And I didn't say good bye! because I knew for sure If my dad was there definitely he would talk to me. He never wanted hide from me. After four years time I really could understood what was happened to my dad. It's the war who took my father away. It's the war who is responsible for thousand of lives. It's the war who is responsible for millions of refugees. It's the war who took my father away.
This is based on a real story. As Sri Lankans we were lucky to defeat the war. Today all of us breathing freely under one a flag but there are thousands of people who are still suffering because of cruel war. Let's stand against war.