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 = Logger.getLogger(NokiaCommunicator.class);
public NokiaCommunicator() {
log.info("Constructor initialized");
}
@Override
public void sayCommunicatorName() {
log.info("I am Nokia Communicator");
}
@Override
public ICommunicator getCommunicator() {
// TODO Auto-generated method stub
return new NokiaCommunicator();
}
}
import org.apache.log4j.Logger;
public class RelianceCommunicator implements IRelianceCommunicator {
public static final Logger log=Logger.getLogger(RelianceCommunicator.class);
public RelianceCommunicator()
{
log.info("Constructor initialized");
}
@Override
public void sayCommunicatorName() {
log.info("I am Reliance Communicator");
}
@Override
public ICommunicator getCommunicator() {
// TODO Auto-generated method stub
return new RelianceCommunicator();
}
}
this is the main method of this program
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Main {
public static final Logger log = Logger.getLogger(Main.class);
public static void main(String[] args) {
BasicConfigurator.configure();
ICommunicator communicator = new RelianceCommunicator();
communicator.sayCommunicatorName();
communicator= new NokiaCommunicator();
communicator.sayCommunicatorName();
}
}
the output of this program is
0 [main] INFO RelianceCommunicator - Constructor initialized
3 [main] INFO RelianceCommunicator - I am Reliance Communicator
4 [main] INFO NokiaCommunicator - Constructor initialized
4 [main] INFO NokiaCommunicator - I am Nokia Communicator
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 = Logger.getLogger(NokiaCommunicator.class);
public NokiaCommunicator() {
log.info("Constructor initialized");
}
@Override
public void sayCommunicatorName() {
log.info("I am Nokia Communicator");
}
@Override
public ICommunicator getCommunicator() {
// TODO Auto-generated method stub
return new NokiaCommunicator();
}
}
import org.apache.log4j.Logger;
public class RelianceCommunicator implements IRelianceCommunicator {
public static final Logger log=Logger.getLogger(RelianceCommunicator.class);
public RelianceCommunicator()
{
log.info("Constructor initialized");
}
@Override
public void sayCommunicatorName() {
log.info("I am Reliance Communicator");
}
@Override
public ICommunicator getCommunicator() {
// TODO Auto-generated method stub
return new RelianceCommunicator();
}
}
this is the main method of this program
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Main {
public static final Logger log = Logger.getLogger(Main.class);
public static void main(String[] args) {
BasicConfigurator.configure();
ICommunicator communicator = new RelianceCommunicator();
communicator.sayCommunicatorName();
communicator= new NokiaCommunicator();
communicator.sayCommunicatorName();
}
}
the output of this program is
0 [main] INFO RelianceCommunicator - Constructor initialized
3 [main] INFO RelianceCommunicator - I am Reliance Communicator
4 [main] INFO NokiaCommunicator - Constructor initialized
4 [main] INFO NokiaCommunicator - I am Nokia Communicator
Comments
Post a Comment