Sunday, 13 June 2010

Example program on Java Abstract Factory Pattern

Java Abstract Factory Pattern is the collection of factories. We will understand this factory pattern by a simple use case. Look at the below use case diagram for Java Abstract Factory Pattern.

we have created two factories, Subject and Game factory. The Abstract Factory will contain these two factories. Lets have a look at the codes of implementation of this Abstract Factory Pattern.

First we will create Subject Factory
Subject.java
public interface Subject {
 int getMarks();
}


Arts.java
public class Arts implements Subject {

 @Override
 public int getMarks() {
  return 70;
 }
}



Maths.java
public class Maths implements Subject {

 @Override
 public int getMarks() {
  return 80;
 }
}


Science.java
public class Science implements Subject {

 @Override
 public int getMarks() {
  return 90;
 }
}

Next step is to create Game factory:


Game.java
public interface Game {
 void getGameName();
}

Football.java

public class Football implements Game {

 @Override
 public void getGameName() {
  System.out.println("I play Football !!!");
 }
}

Cricket.java

public class Cricket implements Game {

 @Override
 public void getGameName() {
  System.out.println("I play Cricket !!!");
 }
}

Rugby.java

public class Rugby implements Game {

 @Override
 public void getGameName() {
  System.out.println("I play Rugby !!!");
 }
}

Now we will create Abstract class – AbstractStudentFactory.java

AbstractStudentFactory.java

public abstract class AbstractStudentFactory {
 abstract Subject getMarks(String subjectName);
 abstract Game getGameName(String gameName);
}

Two other Factory Classes
SubjectFactory.java



public class SubjectFactory extends AbstractStudentFactory {

   @Override
   public Subject getMarks(String subjectName){
  
      if(subjectName == null){
         return null;
      } 
     
      if(subjectName.equalsIgnoreCase("Maths")){
         return new Maths();
        
      }else if(subjectName.equalsIgnoreCase("Science")){
         return new Science();
        
      }else if(subjectName.equalsIgnoreCase("Arts")){
         return new Arts();
      }
     
      return null;
   }
  
   @Override
   Game getGameName(String gameName) {
      return null;
   }
}

GameFactory.java

package com.tech2money.abstractfactory;

public class GameFactory extends AbstractStudentFactory {

   @Override
   public Game getGameName(String gameName){
  
      if(gameName == null){
         return null;
      } 
     
      if(gameName.equalsIgnoreCase("Football")){
         return new Football();
        
      }else if(gameName.equalsIgnoreCase("Cricket")){
         return new Cricket();
        
      }else if(gameName.equalsIgnoreCase("Rugby")){
         return new Rugby();
      }
     
      return null;
   }
  
   @Override
   Subject getMarks(String subjectName) {
      return null;
   }
}

Now we will create Factory creator class – Factory.java

package com.tech2money.abstractfactory;

public class Factory {
   public static AbstractStudentFactory getFactory(String type){
  
      if(type.equalsIgnoreCase("Subject")){
         return new SubjectFactory();
        
      }else if(type.equalsIgnoreCase("Game")){
         return new GameFactory();
      }
      return null;
   }
}

AbstractFactoryImpl.java

public class AbstractFactoryImpl {
 public static void main(String[] args) {

  AbstractStudentFactory subjectFactory = Factory.getFactory("Subject");

  Subject sub1 = subjectFactory.getMarks("Maths");
  System.out.println(sub1.getMarks());
 
  Subject sub2 = subjectFactory.getMarks("Science");
  System.out.println(sub2.getMarks());

  Subject sub3 = subjectFactory.getMarks("Arts");
  System.out.println(sub3.getMarks());

  AbstractStudentFactory gameFactory = Factory.getFactory("Game");

  Game game1 = gameFactory.getGameName("Football");
  game1.getGameName();
 
  Game game2 = gameFactory.getGameName("Cricket");
  game2.getGameName();
 
  Game game3 = gameFactory.getGameName("Rugby");
  game3.getGameName();
 }
}


No comments:

Post a Comment