Home » Core Java » Factory Design Pattern

Factory Design Pattern

Factory Design Pattern :: Factory Design Pattern comes under creation design pattern.As the name suggests,basically using these design pattern you can create an instance of class from a factory of related classes.

Real Time Example :: I am going to have a factory of MediaPlayer classes and each class has the functionality to play song.Based on user input ,user will select a particular type media player and will play song using that media player only.

 

UML Diagram ::

 

 

Step 1 :
Create an Interface MediaPlayer.java and declare method playSong()


Step 2 :
Create a class VlcMediaPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 3 :
Create a class WindowsMediaPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 4 :
Create a class AdobeFlashPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 5 :
Create a class MediaPlayerFactory.java and expose a public method which accepts an input and create instance of the corresponding class based on input.

Step 6 :
Create Main class FactoryDesignPatternDemo.java and create MediaPlayerFactory instance. Pass input tho the public method of MediaPlayerFactory class and get corresponding instance.


Output ::


Leave a comment

Your email address will not be published. Required fields are marked *

three × 5 =