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

Mobile Message Organizer

Got a basic requirement that i think every mobile should have, all the mail clients have this facility and i dont see any big difficulties on this other than the storage problem which is not a problem at all as we got GB of spaces in our memory cards. Well i am talking about the organizing the messages in Inbox , categorization of messages. Suppose i want to store all the messages from one of my friend say A in a folder called Personal, my existing mobile device Nokai E71 doesn't have this feature inbuilt (Ofcourse if you want you can make a folder and move the messages manually but here i am talking about the idea of automating this procedure). Only thing we need to do is , we have to store the rules in separate location , rules will be defined by the user and then device will follow these rules.There are no or very less software available which satisfy this requirement for mobile device.this is so simple but basic requirement as per the end user. I am going to make this one ...

Microservices Architecture, A developer's Prespective

Microservices architecture addresses many concern of application development but does not solve all the problems. It has its own advantages and limitations. In order to understand it fully, we need to understand one common architecture pattern in practice, i.e. Monolithic Architecture Monolithic architecture are highly tightly coupled and you can’t change something without impacting other. Look at this building and tell me how easy it is to change Let’s take an example of an e-commerce website that has many components and services i.e. Order service Invoice service Payment services will talk to various payment gateways. User Administration services. Graphical user interface for web, WAP and for various mobile platform like android, iOS, blackberry etc. Database services, possibly more than one database will be involved. Now let’s see what are some common problems associated with it Ever Growing Container size Generally all these components are packaged in one ...