algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Generating Random String

* @param length The length of the random string needed
* @return Random String of specified length
public static String randomString(int length){
java.util.UUID uuid=java.util.UUID.randomUUID();
String randomString=uuid.toString();
return randomString.substring(0, length);


Note: Works with Java 5+

String reversal using recursion

public class A {
public static void main(String[] args) {
String temp = "TESTING";
temp = getString(temp);

public static String getString(String asTemp) {
if (asTemp.length() == 0)
return asTemp;
else {
return getString(asTemp.substring(1, asTemp.length()))
+ asTemp.substring(0, 1);

Floating Point Magic

Try this and see the result

import java.util.*;
public class A {
public static void main(String args[]) {
double a1 = 0.10;
double a2 = 0.80;
double a3 = 4.00;

System.out.println("a1+a2+a3 :" + (a1 + a2 + a3));
System.out.println("a1+a3+a2 :" + (a1 + a3 + a2));
System.out.println("a2+a1+a3 :" + (a2 + a1 + a3));
System.out.println("a2+a3+a1 :" + (a2 + a3 + a1));
System.out.println("a3+a1+a2 :" + (a3 + a1 + a2));
System.out.println("a3+a2+a1 :" + (a3 + a2 + a1));


Command Design Pattern

The Command Design Patterns main goal is to decouple the invoker from the Receiver.

The invoker is the one which invokes an action on the Receiver, and the receiver is the one on which the command is executed.


// The Receiver 
package Command;

public class Movie {

public static void start() {
System.out.println("Start Movie");


public static void stop() {
System.out.println("Movie Stopped");


//Play Command
public class PlayMovieCommand implements CommandIntf {

public void execute() {


//Stop Command
public class StopMovieCommand implements CommandIntf {

public void execute() {



//The Invoker
package Command;

public class MoviePlayer {
static CommandIntf play;

static CommandIntf stop;

public static void setPlayCmd(CommandIntf playMovCmd) {
play = playMovCmd;

public static void setStopCmd(CommandIntf stopMovCmd) {
stop = stopMovCmd;

public static void playMovie() {


public static void stopMovie() {


Singleton Design Pattern

package singleton;

public class Singleton {
    private static Singleton aSingleton;

    private Singleton() {

    static Singleton getInstance() {
        synchronized (Singleton.class) {
            if (Singleton.aSingleton == null) {
                  Singleton.aSingleton = new Singleton();
        return Singleton.aSingleton;

  • only one instance of a class across threads
  • global point of access 

Decorator Design Pattern (Wrapper)



public abstract class DrinkDecorator extends Drink {
public Drink drink;



public abstract class Drink { 
    String description;

    int cost;

    abstract int getCost();

    String getDesc() {

        return this.description;




public class Vodka extends Drink {

    String description = " Vodka ";

    int cost = 100;


    int getCost() {

        return this.cost;



OrangeJuice & like wise LemonJuice

public class OrangeJuice extends DrinkDecorator {

    String description = " fresh orenge juice ";

    int cost = 5;

    public OrangeJuice(Drink myVodka) {

        this.drink = myVodka;



    int getCost() {

        return this.cost + this.drink.getCost();



Client Code

//Make Vodka with orange

        //make vodka

        Drink myVodka = new Vodka();

        //mix orenge and vodka

        DrinkDecorator orengeVodka= new OrangeJuice(myVodka);

        //mix lime juie in the orengeVodka

        DrinkDecorator LimeOrengeVodka= new LimeJuice(orengeVodka);

        //the cost of the final vodka should be that of lime

        //orenge and vodka cost


Observer Design Pattern

This pattern is used in publisher-consumer kind of situations


This pattern is used where a change in data which is observable by one or many observers,The pattern is such that no matter what kind of Observers ATMObserver , OnlineAcObsever)  come the Observable or the subject(BankAccount) has no impact.So tomorrow if there is a need to add MobileObserver for BankAccount, that can just be added without any changes and implementing AccountObserver.

The Subject or Observable concrete class,in this case a bank account which is observable.The Observers OnlineACObserver ATMACObserver register with this class and get updates about the bank account.

public class BankAccount implements SubjectObservable {
public ArrayList observers = new ArrayList();

String balance;

String account;

public void deregisterObserver(AccountObserver o) {

public void notifyObservers() {
for (int i = 0; i < this.observers.size(); i++) {
AccountObserver observer = (AccountObserver) this.observers.get(i);
observer.update("ACCOUNTNUM", "BALANCE < 200");

public void registerObserver(AccountObserver o) {

public void setBalance(String balance) {
this.balance = balance;

The Concrete implementation of Observer take the case of ATMACObserver this observer registers with the BankAccount and gets invoked by the Subject when there is a change or update in the Subject.

public class ATMObserver implements AccountObserver {

String message;

SubjectObservable bankAccount;

public ATMObserver(SubjectObservable bankAccount) {
this.bankAccount = bankAccount;

public void update(String accountNumber, String balance) {
this.message = "welcome to ATM " + accountNumber + " " + balance;

The client code can be as follows.

class Client {
public static void main(String[] args) {
// creating observable Bank Account
SubjectObservable bankAccount = new BankAccount();
// Observer 1 onlin
OnlineAcObserver online = new OnlineAcObserver(bankAccount);
// Observer 2 ATM
ATMObserver atm = new ATMObserver(bankAccount);
//update account values which updates the observer

Factory Method Design Pattern

This is one of the Creational Design Pattern,The use of this pattern is to make the classes loosely coupled.
Client Code which calls creation of computer and who only knows which object to create ,In this case It asks to create an Apple Computer and Give it using ComputerFactoryIF.
Computer aComputer = ComputerFactoryIF.createComputer("apple");

The ComputerFactoryIF the interface of all the concrete factories checks as to which type of Computer is to be created and delegates to the appropriate ComputerFactory in this case AppleComputerFac

public class ComputerFactoryIF {

public static Computer createComputer(String string) {
Computer aComp = null;
if (string.equals("apple"))
aComp = AppleComputerFac.createComputer();
else if (string.equals("dell"))
aComp = DellComputerFac.createComputer();
return aComp;

There After the corresponding factory creates an instance of the computer and returns(In this case AppleComputer Object).

public class AppleComputerFac extends ComputerFactoryIF {
public static Computer createComputer() {
return new AppleComputer();

public class AppleComputer extends Computer {
AppleComputer() { = "AppleComputer";


Search 24 Bytes