Home » Core Java » Abstract Factory Design Pattern

Abstract Factory Design Pattern

Abstract Factory Design Pattern ::Abstract Factory design pattern is also called as factory of factories. This type of design pattern comes under creational design pattern as this pattern provides way to select a factory and then create an instance of related classes of that factory.
 
Real Time Example :: I am going to have two factories where one factory will have VideoPlayer and its implementation classes and another factory will have AudioPlayer and and its implementation classes.Based on user input a factory will be selected and then an instance will be created of related classed of that factory.

 

UML Diagram ::

 

 

Step 1 :
Create an Interface VideoPlayer .java and declare method playVideo()

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

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

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

Step 5 :
Create an Interface AudioPlayer.java and declare method playSong()

Step 6 :
Create a class WinAmpPlayer .java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 7 :
Create a class MP3Player.java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 8 :
Create a class JetAudioHDPlayer.java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 9:
Create an abstract class AbstractFactory.java which has two abstract methods getVideoPlayer() and getAudioPlayer() which returns VideoPlayer and AudioPlayer type instances respectively.

Step 10 :
Create a class VideoPlayerFactory .java which extends AbstractFactory class and returns corresponding VideoPlayer type instance based on user input.

Step 12 :
create a class AudioPlayerFactory.java which extends AbstractFactory class and returns corresponding AudioPlayer type instance based on user input.

Step 13 :
Create class SelectFactory.java which has public method getFactory() that returns corresponding VideoPlayerFactory or AudioPlayerFactory type instance based on user input.

Step 14 :
Create class AbstractFactoryDesignPatternDemo.java and select factory and then corresponding class object of that factory.

Output ::
Playing your favourite video using VLC MediaPlayer
Playing your favourite video using Windows MediaPlayer
Playing your favourite video using Adobe Flash Player

Playing your favourite song using WinAmp Player
Playing your favourite song using MP3 Player
Playing your favourite song using JetAudioHD Player


Leave a comment

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

one × three =