Tutorial
Menus

MVC Pattern - MVC Design Patterns

Factory Pattern

Rating: 3.0/5 (1 vote cast)

Level   : Intermediate
Author : Arunkumar S
Download Source : FactoryPatternSample.zip

Factory Design Pattern

Factory pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and in Factory Pattern an interface is responsible for creating the object but the sub classes decides which class to instantiate. It is like the interface instantiate the appropriate sub-class depending upon the data passed. Here in this article we will understand how we can create an Factory Pattern in Java.

Factory means nothing but simply creating object

Now in the example that we are looking at for explaining the Factory Design Pattern is we are creating an object of MP3Player and MP4Player without  direct calling the corresponding classes .

PlayerFactory .java
package com.javaorigin.sample.pattern.factory;

public class PlayerFactory {

    public static Player getPlayer(String file) {
        if (file.endsWith(".mp3")) {
            return new MP3Player();
        } else if (file.endsWith(".mp4")) {
            return new MP4Player();
        }
        return null;
    }
}

now if you see this class, here we will be passing an argument to the getPlayer function based on the argument passed we return the corresponding  Payer object of the class (MP3Player or MP4Player)

Player .java
package com.javaorigin.sample.pattern.factory;

public interface Player {
    void play(String file);
    void stop();
}


Here we have created an Interface for Player. This interface will be implemented by the MP3Player and MP4Player . Now creating mp3player and mp4Player implemented with player interface

MP3Player .java        
package com.javaorigin.sample.pattern.factory;

public class MP3Player implements Player{

@Override
public void play(String file) {
System.out.println("Start Mp3 Playing...."+file);

}

@Override
public void stop() {
System.out.println("Stop Mp3 Playing....");
}

}

MP4Player .java
  package com.javaorigin.sample.pattern.factory;

public class MP4Player implements Player{

@Override
public void play(String file) {
System.out.println("Start Mp4 Playing...."+file);

}

@Override
public void stop() {
System.out.println("Stop Mp4 Playing....");

}

}

Factory Method Client Code:

MainPlayer .java

package com.javaorigin.sample.pattern.factory;

public class MainPlayer {

public void playSong(String file) {
Player player = PlayerFactory.getPlayer(file);
if (player != null) {
player.play(file);
} else {
System.out.println("No Plugin found for " + file);
}
}

public static void main(String[] args) {
(new MainPlayer()).playSong("ARRahman-TamilaTamil.mp4") ;
}

}


Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework.

 Parallel class hierarchies often require objects from one hierarchy to be able to create appropriate objects from another.

some popular factory pattern examples :

     1. SocketFactor
     2. LogFactory
     3. XMLFactory


1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
FactoryPattern124/Jul/2009 01:07 PM by arun
Javaorigin.com contact@javaorigin.com