Design patterns in java (Part1)

      Java offers solutions for software design. These design patterns provides great solutions in efficient manner. These are easy to maintain. Java’s design patterns are listed below.

  • 1.       Singleton pattern
  • 2.       Factory pattern
  • 3.       Observer pattern
  • 4.       Decorator pattern
  • 5.       Strategy pattern

Each and every pattern is detailed explained as follows.

1.Singleton pattern:

              Single instance is created with the global scope. The instance can be visible for entire project.

Steps:

  • ·       First,create a singleton class. Develop a singleton instance code as private,static and final type.
  • ·       Just return the instance from a public member function getInsta().
  • ·       Secondly, a public class SingletonEg is written with main function.
  • ·       s1,s2 are the two instances developed from Singleton class.
  • ·       Check whether both are true.
  • ·       Yes, both are equal because these two are same singleton objects.

class Singleton { 

    private static final Singleton insta = new Singleton(); 

    private Singleton() {} 

    public static Singleton getInsta() { 

        return insta; 

    } 

} 

public class SingletonEg { 

    public static void main(String[] args) { 

        Singleton s1 = Singleton.getInsta(); 

        Singleton s2 = Singleton.getInsta(); 

        if (s1 == s2) { 

            System.out.println("Both singleton instances are same"); 

        } 

    } 

} 

The output is….

C:\raji\blog>javac SingletonEg.java

C:\raji\blog>java SingletonEg.java

Both singleton instances are same

Next is factory pattern.

2.Factory pattern:

              Creating an interface with objects and function. Let it can be overridden by sub class.

Steps:

  • ·       Write an interface with void function.
  • ·       Add two concrete classes implementing the same interface.
  • ·       Develop a Factory to generate object of concrete class based on given information
  • ·       Let the fruit class object uses the fruitfactory function.
  • ·       Interface: fruit
  • ·       Concrete classes: Apple and Orange.
  • ·       Factory class: fruitFactory
  • ·       Main class :FactoryPatternEg

Program:

interface fruit {

    void color();

}

class Apple implements fruit {

    @Override

    public void color() {

        System.out.println("Apple is red colour.");

    }

}

class Orange implements fruit {

    @Override

    public void color() {

        System.out.println("Orange is orange colour.");

    }

}

 class fruitFactory {

    public fruit getFruit(String fruitType) {

        if (fruitType == null) {

            return null;

        }

        if (fruitType.equalsIgnoreCase("APPLE")) {

            return new Apple();

        } else if (fruitType.equalsIgnoreCase("ORANGE")) {

            return new Orange();

        }

        return null;

    }

}

 public class FactoryPatternEg {

    public static void main(String[] args) {

        fruitFactory ff1 = new fruitFactory();

        // Get an object of apple and call its color method

        fruit f1 = ff1.getFruit("APPLE");

        f1.color();

        // Get an object of Orange and call its color method

        fruit f2 = ff1.getFruit("ORANGE");

        f2.color();

    }

}

Compile and run the program in command prompt. you will get the output.

C:\raji\blog>javac FactoryPatternEg.java

 C:\raji\blog>java FactoryPatternEg

Apple is red colour.

Orange is orange colour.

These are the first two design patterns in java. Next blog post shows you the remaining Design patterns.

No comments:

Post a Comment