Skip to main content

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 = 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

Popular posts from this blog

Indian Education System : Let's shape it

Good advice is always certain to be ignored, but that's no reason not to give it.                                                                                        By Agatha Christie   This is one of the things that I wanted to write from long back and per my opinion it should be matter of at most important for any educated person of India. Today in this article I would like  focus on reminding people about the importance of education and educational departments  no matter those are government related or privately held. Whatever we are to...

The Bourne Betrayal | Book Review

Novel by Eric Van Lustbader and Robert Ludlum I like all Robert Ludlum’s novels including those which are written by Evan Lastbadder. To me his novels have taken fiction to the next level. During my way back to Hyderabad from my last summer trip to hometown I bought paperback version of “The Bourne Betryal”. This novel was full of Lastbadder’s style of writing than Robert Ludlum’s one.  I took almost 6 months to complete it. This novel has something different to offer actually. Plot is exciting but the story is not very accelerating. Jason bourn and Martin Lindros, When martin Lindros decided to come back in the field operations with the aim to destroy Fadi and When Martin is out the track , Jason is the only help possible in the situation. Story takes you through various struggle of Jason to bring Martin back home. There are few things where author has not even paid any attention for example how does an ordinary Pakistani Waiter will have that much of information   Towa...

Google and Facebook's Data Center

I think Capacity wise Facebook is the good place to start as no other website has experienced kind of volume and traffic that Facebook has witnessed in the recent past. As you have not specified whether You want to know more about the infrastructure, security or you are interested in their operating model, staff size and remote site management? I would go with the general details. Facebook has multiple data centers around the world and the reason behind this is a common knowledge i.e huge volume of the data that FB process on daily basis. Facebook is currently the world’s most popular web site, with more than 1 trillion page views each month, according to metrics from Google’s DoubleClick service. Facebook currently accounts for about 9 percent of all Internet traffic, slightly more than Google, according to HitWise. This is the first data center of the Facebook in Prineville FB realized that they need many data centers in different locations to support the performance demand of...