Monday, February 24, 2014

Strategy Design pattern with simple example

        "In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern, whereby an algorithm's behavior can be selected at run time. Formally speaking, the strategy pattern defines a family of algorithm, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it."this is how Wikipedia defines the strategy pattern, which is not clear for the beginners.



But using following example, anyone can get an idea about what is going on here. look at the following class diagram 





                This is a company that makes toy vehicles for the children. Every toy has its own power source to use which are often using battery and solar power to recharge it self. In some special cases such as introducing new version of an existing toy there is a possibility to change the power option between Solar and battery power. Strategy pattern helps programmer to change the power option within the run time without re-implementing the class. 

Following code shows how we can use strategy pattern for this kind of situation.  

Interface "PowerOption" and Class "BatteryPower" and Class "SolarPower" thet implements the interface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public interface PowerOption {
	public String power();
}

// Class of the toys which are using battery
class BatteryPower implements PowerOption {
	public String power() {

		return "Is using battery power";
	}

}

//Class of the toys which are using solar power
class SolarPower implements PowerOption{
	public String power() {

		return "Is using solar power";
	}
	
}

Parent class "ToyWehical"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class ToyWehicle {
	// Private variables
	private String name;
	private String suitableFor;
	public PowerOption poweroption;

	// Getters and Setters
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSuitableFor() {
		return suitableFor;
	}

	public void setSuitableFor(String suitableFor) {
		this.suitableFor = suitableFor;
	}
	
	//Method to get power option of the toy
	public String PowerSource()
	{
		return poweroption.power();
	}
	
	//Method to change the power option on runtime
	public void setPowerOption(PowerOption newOption)
	{
		poweroption = newOption;
	}

}

Child Class "ToyCar"

1
2
3
4
5
6
7
8
public class ToyCar extends ToyWehicle {
	public ToyCar() {// Constructor
		super();
		setName("Car");
		setSuitableFor("Boys");
		poweroption = new SolarPower();// set default power option
	}
}

Child Class "ToyCopter"

1
2
3
4
5
6
7
8
public class ToyCopter extends ToyWehicle {
	public ToyCopter() {
		super();
		setName("Helicopter");
		setSuitableFor("boys");
		poweroption = new BatteryPower();
	}
}

Class "Main" which contain main method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ToyWehicle toyota = new ToyCar();
		ToyWehicle mingo = new ToyCopter();

		System.out.println("toyota is a Toy " + toyota.getName() + " which "
				+ toyota.PowerSource());
		System.out.println("mingo is a Toy " + mingo.getName() + " which "
				+ mingo.PowerSource());

		// Change the power option of toyota to battery power
		System.out.println("****new toyota changing its power option***");
		toyota.setPowerOption(new BatteryPower());
		// Check the power option
		System.out.println("new toyota is a Toy " + toyota.getName()
				+ " which " + toyota.PowerSource());
	}

}

Test it and understand the concept behind the codes...

Friday, February 7, 2014

Java Python Binding

Java is a simple and powerful OOP language that is good for GUI programming rather than scientific programming. Python has its core features lays toward the scientific and logical programming rather than GUI application programming.Since python has minimized code complexities programmer don't need to spent time to map codes instead think about the logic. 

But, combination of these two languages can be very much useful. That's why most of the people looking for a better combination of these two languages.(Projects did for combining python and java) .

According to my opinion any programming language achieve its maximum performance when it has its own style rather than combining another to it. So i intended to use that as a principle when i'm doing this experiment (I didn't do any research about the others projects and architect that they used.)   

I mainly target this for the Windows platform.But with a very simple modification this can be used in either Linux or MAC  platforms. 

I used following abstract algorithm to combine the two parts of the application. 

Java part of the application, made request to CMD to run a special python file and get the output of the python application in to the CMD. CMD execute the command and take the output that given by the python script. then Java application takes that output in to it self.

Full project file of the experiment can be downloaded from here