Skip to main content

Posts

Showing posts with the label design Pattern

SingleTon DesignPattern

Singleton pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and with Singleton Pattern we will allow only one instance of the class to be created. Here in this article we will understand how we can create an Singleton class in Java. Singleton Class Code: package designpattern.creational.singleton ; public class MySingleTon { private MySingleTon ( ) { } private static MySingleTon instance = null ; public static synchronized MySingleTon getInstance ( ) { if ( instance == null ) { System . out . println ( "Creating New Instance" ) ; instance = new MySingleTon ( ) ; } else { System . out . println ( "Returning Existing Instance" ) ; } return instance ; } public Object clone ( ) throws CloneNotSupportedException { throw new CloneNotSupportedException ( ) ; } } Code Explanation: W...

Design Patterns

Factory pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and in Factory Pattern an interface is responsible for creating the object but the sub classes decides which class to instantiate. It is like the interface instantiate the appropriate sub-class depending upon the data passed. Here in this article we will understand how we can create an Factory Pattern in Java. Suppose here i am giving a good and easy example.. ICommunicator is the core interface public interface ICommunicator { public ICommunicator getCommunicator(); public void sayCommunicatorName(); } public interface IRelianceCommunicator extends ICommunicator{ } public interface INokiaCommunicator extends ICommunicator{ } The concrete class NokiaCommunicator and RelianceCommunicator is below import org.apache.log4j.Logger; public class NokiaCommunicator implements INokiaCommunicator { public static final Logger log ...