Creational Design Pattern

Creational design patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.


Factory Method

1
2
3
4
5
public interface Shape {

    void draw();

}
1
2
3
4
5
6
7
8
9
public class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("In square");

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("In Circle");

    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return getClass().getName() + "Class";
    }

}
1
2
3
4
5
6
7
8
9
public class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("In Rectangle");

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class ShapeFactory {



    public Shape getShape(String shapetype){

        if (shapetype == null) {
            return null;
        }
        else if ( shapetype.equalsIgnoreCase("circle") ) {
            return new Circle();
        }
        else if ( shapetype.equalsIgnoreCase("rectangle") ) {
            return new Rectangle();
        }
        else if ( shapetype.equalsIgnoreCase("Square") ) {
            return new Square();
        }

        return null;

    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class FactoryDemo {

    public static void main(String[] args) {

        ShapeFactory factory = new ShapeFactory();
        Shape circle = factory.getShape("Circle");
        circle.draw();

        Shape rectangle = factory.getShape("rectangle");
        rectangle.draw();


    }

}

Abstract Factory Method


1
2
3
4
5
public interface Color {

    public void fill();

}
1
2
3
4
5
6
7
8
public class Blue implements Color {

    @Override
    public void fill() {
        System.out.println("filling Blue color");
    }

}
1
2
3
4
5
6
7
8
public class Green implements Color {

    @Override
    public void fill() {
        System.out.println("filling Green color");
    }

}
1
2
3
4
5
6
7
8
9
public class Red implements Color {

    @Override
    public void fill() {
        System.out.println("filling red color");

    }

}
1
2
3
4
5
public interface Shape {

    void draw();

}
1
2
3
4
5
6
7
8
9
public class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("In square");

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("In Circle");

    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return getClass().getName() + "Class";
    }

}
1
2
3
4
5
6
7
8
9
public class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("In Rectangle");

    }

}
1
2
3
4
5
6
public abstract class AbstractFactory {

    public abstract Shape getShape(String shapetype);
    public abstract Color getColor(String type);

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class ColorFactory extends AbstractFactory {

    @Override
    public Color getColor(String type){

        if (type ==null) {
            System.out.println("yes i am in");
            return null;
        }
        else if (type.equalsIgnoreCase("Green")) {
            return new Green();
        }
        else if (type.equalsIgnoreCase("Blue")) {
            return new Blue();
        }
        else if (type.equalsIgnoreCase("Red")) {
            return new Red();
        }


        return null;



    }


    @Override
    public Shape getShape(String shapetype) {
        // TODO Auto-generated method stub
        return null;
    }






}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ShapeFactory extends AbstractFactory{

    public Shape getShape(String shapetype){

        if (shapetype == null) {
            return null;
        }
        else if ( shapetype.equalsIgnoreCase("circle") ) {
            return new Circle();
        }
        else if ( shapetype.equalsIgnoreCase("rectangle") ) {
            return new Rectangle();
        }
        else if ( shapetype.equalsIgnoreCase("Square") ) {
            return new Square();
        }

        return null;

    }

    @Override
    public Color getColor(String type) {
        // TODO Auto-generated method stub
        return null;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class FactoryProducer {

    public static AbstractFactory getFactory(String type){

        if (type == null) {
            return null;
        }
        else if (type.equalsIgnoreCase("Shape")) {
            return new ShapeFactory();
        }else if (type.equalsIgnoreCase("Color")) {
            return new ColorFactory();
        }

        return null;

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class AbstractFactoryPatternDemo {

    public static void main(String[] args) {

        AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
        Shape circle  = shapeFactory.getShape("Circle");
        circle.draw();

        AbstractFactory colorFactory = FactoryProducer.getFactory("Color");
        Color red = colorFactory.getColor("Green");
        red.fill();




    }

}

Singleton Pattern


This is how Singleton Class looks like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton(){};

    public static Singleton getInstance(){

        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }

        return uniqueInstance;
    }



}

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton(){};

    public static Singleton getInstance(){

        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }

        return uniqueInstance;
    }



}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Settings {

    Preferences settings = Preferences.getInstance();

    public Settings() {

        settings.setCgpa(3.14);
        settings.setContact("03336363636");
        settings.setEmail("mohsinmahmoodmdl@gmail.com");
        settings.setName("Muhammad Mohsin Mahmood");
    }

    public void showSettings() {

        settings.show();
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Contact {

    Preferences settings = Preferences.getInstance();

    public void showSettings() {

        settings.show();
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Home {

    Preferences settings = Preferences.getInstance();

    public void showSettings() {

        settings.show();
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class SingletonDemo {

    public static void main(String[] args) {


        Settings settings = new Settings();
        settings.showSettings();

        System.out.println("\nHome: Settings");
        System.out.println("================");
        Home home = new Home();
        home.showSettings();

        System.out.println("\nContact: Settings");
        System.out.println("================");
        Contact contact = new Contact();
        contact.showSettings();

    }
}

Prototype Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Person {

    private String name;

    public Person(String name) {

        this.name = name;

    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Car implements Cloneable {

    private Person owner;

    public Car() {

    }

    public void setOwner(String name) {
        Person owner = new Person(name);
        this.owner = owner;
    }

    public Person getOwner() {
        return owner;
    }


    public Object clone() {
        try {
            Car car = (Car) super.clone();
            return car;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Main {

    public static void main(String[] args) throws CloneNotSupportedException {   

        Car honda = new Car();
        honda.setOwner("Mohsin Mahmood");


        Car hondacopy = (Car) honda.clone();



        System.out.println("Name of first owner " + honda.getOwner().getName());
        System.out.println("Name of first owner " + hondacopy.getOwner().getName());        

    }

}

Lab Exam Creational Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public interface AudioPlayer {

    public void play();
    public void forward();
    public void backward();
    public void stop();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public class MP3Player implements AudioPlayer {

    public void play() {

        System.out.println("Running media file from " + getClass().getName());

    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public class JetAudioHDPlayer implements AudioPlayer {

    public void play() {
        System.out.println("Running media file from " + getClass().getName());


    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public class WinAmpPlayer implements AudioPlayer {

    public void play() {
        System.out.println("Running media file from " + getClass().getName());


    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public interface VideoPlayer {

    public void play();
    public void forward();
    public void backward();
    public void stop();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public class VLCMediaPlayer implements VideoPlayer {

    public void play() {
        System.out.println("Running media file from " + getClass().getName());


    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public class WindowsMediaPlayer implements VideoPlayer{

    public void play() {
        System.out.println("Running media file from " + getClass().getName());

    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public class AdobeFlashPlayer implements VideoPlayer {

    public void play() {
        System.out.println("Running media file from " + getClass().getName());

    }

    public void forward() {
        // TODO Auto-generated method stub

    }

    public void backward() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public abstract class AbstractFactory {

    public abstract VideoPlayer getVideoPlayer(String type);
    public abstract AudioPlayer getAudioPlayer(String type);

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public class AudioPlayerFactory extends AbstractFactory {

    public AudioPlayer getAudioPlayer(String type) {
        if (type == null) {
            return null;
        } else if (type.equalsIgnoreCase("MP3Player")) {
            return new MP3Player();
        } else if (type.equalsIgnoreCase("JetAudioHDPlayer")) {
            return new JetAudioHDPlayer();
        } else if (type.equalsIgnoreCase("WinAmpPlayer")) {
            return new WinAmpPlayer();
        }

        return null;
    }

    public VideoPlayer getVideoPlayer(String type) {
        // TODO Auto-generated method stub
        return null;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public class VideoPlayerFactory extends AbstractFactory {

    public VideoPlayer getVideoPlayer(String type) {
        if (type == null) {
            return null;
        } else if (type.equalsIgnoreCase("VLCMediaPlayer")) {
            return new VLCMediaPlayer();
        } else if (type.equalsIgnoreCase("WindowsMediaPlayer")) {
            return new WindowsMediaPlayer();
        } else if (type.equalsIgnoreCase("AdobeFlashPlayer")) {
            return new AdobeFlashPlayer();
        }

        return null;
    }

    public AudioPlayer getAudioPlayer(String type) {
        // TODO Auto-generated method stub
        return null;
    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */
public class FactoryProducer {

    public static AbstractFactory getFactory(String type) {

        if (type == null) {
            return null;
        } else if (type.equalsIgnoreCase("AudioPlayerFactory")) {
            return new AudioPlayerFactory();
        }else if (type.equalsIgnoreCase("VideoPlayerFactory")) {
            return new VideoPlayerFactory();
        }
        return null;

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * 
 * Muhammad Mohsin Mahmood
 * SP16-BSE-060
 * 
 * */

public class MusicPlayerFactoryDemo {

    public static void main(String[] args) {


        //getting Audio Factory 
        AbstractFactory factory = FactoryProducer.getFactory("AudioPlayerFactory");

        // getting "MP3Player" audio player
        AudioPlayer mp3Player = factory.getAudioPlayer("MP3Player");
        mp3Player.play();

        AudioPlayer jetaudio = factory.getAudioPlayer("JetAudioHDPlayer");
        jetaudio.play();

        AudioPlayer winamp = factory.getAudioPlayer("WinAmpPlayer");
        winamp.play();

        System.out.println("\n=======================================\n");

        //Creating Video Factory 
        AbstractFactory factory2 = FactoryProducer.getFactory("VideoPlayerFactory");

        VideoPlayer vlc = factory2.getVideoPlayer("VLCMediaPlayer");
        vlc.play();

        VideoPlayer wmp = factory2.getVideoPlayer("WindowsMediaPlayer");
        wmp.play();

        VideoPlayer flash = factory2.getVideoPlayer("AdobeFlashPlayer");
        flash.play();





    }

}