Java: Singleton Pattern

In most cases, instances of objects are created, which result in several objects being created from the same class. Using the Singleton pattern, we can ensure that only a single object of this class is created during the lifetime of the application.

Step 1: Create a Singleton Class

public class SingletonClass
{
      //Create singleton object
      private static SingletonClass singletonInstance = new SingletonClass();
      //Create private constructor so this class cannot be instantiated
      private SingletonClass() {}
      //Get an instance of this class
      public static SingletonClass getInstance() 
     {
          return singletonInstance;
     }
      public void message() {          System.out.println("Singleton Instance");
     }
}

Singleton Demo:

public class SingletonDemo {
     public static void main (String[] args) {
          //The constructor SingleObject() is not available
          //SingletonClass mySingleton= new SingletonClass();

          //Correct Way
          SingletonClass mySingleton= SingletonClass.getInstance();

          //show the message
          mySingleton.showMessage(
     }
}
Advertisements

Java: Abstract Factory Pattern

AbstractFactory Pattern is a sort of super factory pattern. In this example we will create a factory of objects where each factory creates items just as in the regular Factory pattern.

/*
 Class: ItemType.java
 */
 public interface ItemType{
   void doSomething();
 }

Item1.java

/*
 Class: Item1.java
 */
public class Item1 implements Factory{

@Override
public void doSomething(){
     System.out.println("My Item 1.");
   }
}

Item2.java

/*
 Class: Item2.java
 */
 public class Item2 implements Factory{

@Override
public void doSomething(){
     System.out.println("My Item 2.");
   }
}

Item3.java

/*
 Class: Item3.java
 */
public class Item3 implements Factory{

@Override
public void doSomething(){
      System.out.println("My Item 3.");
   }
}

Create another interface

 

ItemProperty.java

/*
Class: ItemProperty.java
*/

public interface ItemProperty{
    void fillProperty();
}

Create concrete classes:

public class myProperty1 implements ItemProperty{

   @Override
   public void fillProperty(){
       System.out.println("Inside myProperty1 fillProperty");
   }
}

More classes:

public class myProperty2 implements ItemProperty{

   @Override
   public void fillProperty(){
       System.out.println("Inside myProperty2 fillProperty");
   }
}

Create the Abstract Factory

public abstract class AbstractFactory{
     abstract ItemProperty getProperty (String someProperty);
     abstract ItemType getItemType (String someItemType);
}

 

ItemTypeFactory.java

/*

Class: ItemTypeFactory.java

*/
public class ItemTypeFactory extends AbstractFactory{
  public ItemType getItem(String itemType){
     it(itemType == null) {
        return null;
      }

      if(itemType.equalsIgnoreCase("Item1")){
         return new Item1();
      }else if(itemType.equalsIgnoreCase("Item2")){
         return new Item2();
      }else if(itemType.equalsIgnoreCase("Item3")){
         return new Item3();
      }
  return null;
  }
@Override
ItemProperty getProperty (String someProperty) {
      return null;
   }
}


Create the ItemProperty factory

ItemPropertyFactory.java

public class ItemPropertyFactory extends AbstractFactory { 

@Override
 public Shape getItemType(String itemType){ 

      return null;

   }

@Override
 Property2 getItemProperty(String ItemProperty) { 

      if(ItemProperty == null){
         return null;
} if(ItemProperty.equalsIgnoreCase("ONE")){ 
return new myProperty1();
 } else if(ItemProperty.equalsIgnoreCase("TWO")){ 
return new myProperty2();
 } else if(ItemProperty.equalsIgnoreCase("THREE")){ 
         return new myProperty3();
      }
      return null;
   }
} 

FactoryProducer.java

public class FactoryProducer{

   public static AbstractFactory getFactory (String choice) {

      if (choice.equalsIgnoreCase("Type"){
           return new ItemTypeFactory();

      }else if (choice.equalsIgnoreCase("Property"){
          return new ItemPropertyFactory();

      }
   }
}

AbstractFactoryPatternDemo

public class AbstractFactoryPatternDemo{

    public static void main(String[] args) {
      AbstractFactory itemtypeFactory = FactoryProducer.getFactory("Type");
      ItemType someItem1 = itemtypeFactory.getItem("Item1");
      someItem1.doSomething();
      ItemType someItem2 = itemtypeFactory.getItem("Item2");
      someItem2.doSomething();

      ItemType someItem3 = itemtypeFactory.getItem("Item3");
      someItem3.doSomething();

      AbstractFactory itempropertyfactory = FactoryProducer.getFactory("property");
      ItemProperty someProperty1 = itempropertyfactory.getItemProperty("ONE");
      someProperty1.fillProperty();
   }
}

Java: Factory Pattern

Factory Pattern is one of the most common design patterns. In this example we will create a Factory interface, and concrete classes to implement this interface.

/*
 Class: ItemType.java
 */
 public interface ItemType{
   void doSomething();
 }

Item1.java

/*
 Class: Item1.java
 */
public class Item1 implements Factory{

@Override

public void doSomething(){

  System.out.println("My Item 1.");

}

}

Item2.java

/*
 Class: Item2.java
 */
 public class Item2 implements Factory{

@Override

public void doSomething(){

  System.out.println("My Item 2.");

}

}

 

Item3.java

/*
 Class: Item3.java
 */
 public class Item3 implements Factory{

@Override

public void doSomething(){

  System.out.println("My Item 3.");

}

}

 

ItemFactory.java

/*

Class: ItemFactory.java

*/

public class ItemFactory{

  public ItemType getItem(String itemType){

     it(itemType == null) {
        return null;
      }

      if(itemType.equalsIgnoreCase("Item1")){
         return new Item1();
      }else if(itemType.equalsIgnoreCase("Item2")){
         return new Item2();
      }else if(itemType.equalsIgnoreCase("Item3")){
         return new Item3();
      }
  return null;
  }

}
FactoryPatternDemo


public class FactoryPatternDemo{

    public static void main(String[] args) {

      ItemFactory itemFactory = new ItemFactory();

      ItemType someItem1 = itemFactory.getItem("Item1");

      someItem1.doSomething();

      ItemType someItem2 = itemFactory.getItem("Item2");

      someItem2.doSomething();

      ItemType someItem3 = itemFactory.getItem("Item3");

      someItem3.doSomething();

   }

}