03 Month
01 Hours
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.
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"); } }
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 } }
The factory pattern is design pattern that provides a way to create objects
without
specifying the exact class of object that will be created.
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(); } }
All multi-threading level questions such as thread ,process,synchronization.
Visit