preloader

A design patterns are well-proved solution for solving the specific problem/task.

Design Pattern

  • COURSES

    03 Month

  • DURATION

    01 Hours

Different type of Design patterns :

  • Singleton design pattern
  • Factory design pattern

Singleton Design Pattern

The Singleton pattern is a design pattern that make sure Class has only one
instance and provides a global point of access to that instance.

key Feature of Singleton pattern:

  • Singleton pattern uses a private constructor and
    a static method to ensure one single instance throughout the class
  • it provide global access by get instance method
  • Singleton pattern control access to share across the system such as db connection
    or object configuration
  • it is used to implementation caching ,logging.

Singleton Class

Public class Singleton {
            private static Singleton instance;
            //private constructor
            private Singleton() {}
            public static Singleton getInstance() {
                if (instance == null) {
                    instance = new Singleton();
                }
                return instance;
            }
        
            public void Show() {
                System.out.println("verify Singleton");
            }
        }

Here are the main points of Above code :

  • Singleton class have private constructor ,which prevent it from being
    instantialized from outside the class
  • The getInstance() method is public static method that return only one
    instance of Singleton class.
  • the instance variable is private and static that means it is accessible
    with getInstance() method only.
  • if instance is null it will create new instance of sinleton otherwise return the instance it self
Public class Singleton main() {
            public static void main(String[] args) {
                Singleton object = Singleton.getInstance();
                object.show(); //it will print value of show method with single instance
            }
        }

Factory Design Pattern

The factory pattern is design pattern that provides a way to create objects
without specifying the exact class of object that will be created.

key Feature of Factory Pattern :

  • it is independent of exact class ,so code is more flexible and easier to change.
  • it allows the encapsulation
  • make the code reusable
package factoryPattern;


            interface Ivechile {
                void drive();
            }
            
            class Car implements Ivechile {
                public void drive() {
                    System.out.println("Driving a Car");
                }
            }
            class Truck implements Ivechile {
                public void drive() {
                    System.out.println("Driving a Truck");
                }
            }
            class Bus implements Ivechile {
                public void drive() {
                    System.out.println("Driving a Bus");
                }
            }
            
            class VechileFactory {
                public Ivechile getIvechile(String vechileType) {
                    if (vechileType.equalsIgnoreCase("car")) {
                        return new Car();
                    } else if (vechileType.equalsIgnoreCase("bus")) {
                        return new Bus();
                    } else if (vechileType.equalsIgnoreCase("truck")) {
                        return new Bus();
                    } else {
                        return null;
                    }
                }
            }
            public class VechileFactoryMain {
                public static void main(String[] args) {
                    VechileFactory vc = new VechileFactory();
                    Ivechile vci = vc.getIvechile("car");
                    vci.drive();
                    Ivechile vci1 = vc.getIvechile("bus");
                    vci1.drive();
                    Ivechile vci2 = vc.getIvechile("truck");
                    vci2.drive();
            
                }
            }

Related Course

course thumb

Basic Java

All basic level question for java covered in this section

Visit
course thumb

OOP Concept

Inheritance,Polimorphism,Abstraction and Encapsulation

Visit
course thumb

Multithreading

All multi-threading level questions such as thread ,process,synchronization.

Visit