Factory Pattern in java with example
The Factory Pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. It involves defining an interface or an abstract class for creating objects and letting the subclasses decide which class to instantiate. This pattern promotes loose coupling and simplifies object creation.
Here’s a simple example of the Factory Pattern in Java:
Example# 1:
/*
* Author: Zameer Ali Mohil
* */
// Product interface
interface Product {
void create();
}
// ConcreteProductA class
class ConcreteProductA implements Product {
@Override
public void create() {
System.out.println("Creating ProductA");
}
}
// ConcreteProductB class
class ConcreteProductB implements Product {
@Override
public void create() {
System.out.println("Creating ProductB");
}
}
// Creator interface (Factory)
interface Creator {
Product factoryMethod();
}
// ConcreteCreatorA class
class ConcreteCreatorA implements Creator {
@Override
public Product factoryMethod() {
return new ConcreteProductA();
}
}
// ConcreteCreatorB class
class ConcreteCreatorB implements Creator {
@Override
public Product factoryMethod() {
return new ConcreteProductB();
}
}
public class FactoryPatternExample {
public static void main(String[] args) {
// Using the Factory Pattern
// Create an instance of ConcreteCreatorA
Creator creatorA = new ConcreteCreatorA();
// Use the factory method to create a Product
Product productA = creatorA.factoryMethod();
// Use the created product
productA.create();
// Create an instance of ConcreteCreatorB
Creator creatorB = new ConcreteCreatorB();
// Use the factory method to create a different Product
Product productB = creatorB.factoryMethod();
// Use the created product
productB.create();
}
}
In this example:
Product
is an interface representing the product created by the factory.ConcreteProductA
andConcreteProductB
are concrete implementations of theProduct
interface.Creator
is an interface representing the factory (creator) with a factory methodfactoryMethod
that returns aProduct
.ConcreteCreatorA
andConcreteCreatorB
are concrete implementations of theCreator
interface, each providing a specific implementation of the factory method.
When you run the FactoryPatternExample
class, you’ll see output indicating the creation of different products based on the concrete creators:
Creating ProductA
Creating ProductB
This example demonstrates how the Factory Pattern allows you to encapsulate the instantiation of objects, making it easy to introduce new product implementations by creating new concrete creator classes without modifying existing client code.
Example# 2:
Certainly! Let’s consider an example of the Factory Design Pattern for creating different types of mobile interfaces.
/*
* Author: Zameer Ali Mohil
* */
// Mobile Interface interface
interface MobileInterface {
void display();
}
// Concrete implementations of Mobile Interface
class AndroidInterface implements MobileInterface {
@Override
public void display() {
System.out.println("Android Interface");
}
}
class IOSInterface implements MobileInterface {
@Override
public void display() {
System.out.println("iOS Interface");
}
}
class WindowsInterface implements MobileInterface {
@Override
public void display() {
System.out.println("Windows Interface");
}
}
// Mobile Interface Factory interface
interface MobileInterfaceFactory {
MobileInterface createInterface();
}
// Concrete implementations of Mobile Interface Factory
class AndroidInterfaceFactory implements MobileInterfaceFactory {
@Override
public MobileInterface createInterface() {
return new AndroidInterface();
}
}
class IOSInterfaceFactory implements MobileInterfaceFactory {
@Override
public MobileInterface createInterface() {
return new IOSInterface();
}
}
class WindowsInterfaceFactory implements MobileInterfaceFactory {
@Override
public MobileInterface createInterface() {
return new WindowsInterface();
}
}
public class FactoryPatternMobileExample {
public static void main(String[] args) {
// Using the Factory Pattern for creating mobile interfaces
// Create an instance of AndroidInterfaceFactory
MobileInterfaceFactory androidFactory = new AndroidInterfaceFactory();
// Use the factory method to create an Android Interface
MobileInterface androidInterface = androidFactory.createInterface();
// Use the created interface
androidInterface.display();
// Create an instance of IOSInterfaceFactory
MobileInterfaceFactory iosFactory = new IOSInterfaceFactory();
// Use the factory method to create an iOS Interface
MobileInterface iosInterface = iosFactory.createInterface();
// Use the created interface
iosInterface.display();
// Create an instance of WindowsInterfaceFactory
MobileInterfaceFactory windowsFactory = new WindowsInterfaceFactory();
// Use the factory method to create a Windows Interface
MobileInterface windowsInterface = windowsFactory.createInterface();
// Use the created interface
windowsInterface.display();
}
}
In this example:
- The
MobileInterface
interface represents the product created by the factory. - Concrete implementations (
AndroidInterface
,IOSInterface
,WindowsInterface
) are specific mobile interfaces that implement theMobileInterface
interface. - The
MobileInterfaceFactory
interface represents the factory (creator) with a factory methodcreateInterface
that returns aMobileInterface
. - Concrete implementations of
MobileInterfaceFactory
(AndroidInterfaceFactory
,IOSInterfaceFactory
,WindowsInterfaceFactory
) provide specific implementations of the factory method.
When you run the FactoryPatternMobileExample
class, you’ll see output indicating the creation of different mobile interfaces based on the concrete mobile interface factories:
Android Interface
iOS Interface
Windows Interface
This example demonstrates how the Factory Pattern allows you to encapsulate the creation of different types of mobile interfaces, making it easy to introduce new interfaces by creating new concrete interface factories without modifying existing client code.