0% found this document useful (0 votes)
11 views4 pages

Factory Pattern

The Factory Pattern is a creational design pattern that provides a way to create objects without specifying the exact class of object that will be created. This is done by creating objects through a common interface of a factory class, rather than directly with a constructor. The Factory Pattern allows for new types of products to be added without changing the structure of the client code. It promotes loose coupling between the creation of an object and its use.

Uploaded by

Nourhane Ibrahim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Factory Pattern

The Factory Pattern is a creational design pattern that provides a way to create objects without specifying the exact class of object that will be created. This is done by creating objects through a common interface of a factory class, rather than directly with a constructor. The Factory Pattern allows for new types of products to be added without changing the structure of the client code. It promotes loose coupling between the creation of an object and its use.

Uploaded by

Nourhane Ibrahim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Factory Pattern

 One of the most popular design patterns in Java


 Design patterns: well described solution for solving specific task or problem.
 Advantages: 1- Reusable in multiple project.
2- Define system structure.
3- Clarity to the system architecture.
4- Transparency to the design in the application.
5- Well proved and testified solution.
6- Spend less time to solve a problem.
7- Factory Method is that it can return the same instance multiple
times, or can return a subclass rather than an object of that exact type.
 Disadvantages:
1- Indirection may reduce performance.
2- Limitation (may not be addressed specific issues).

 Types:

 Comes under creational pattern.


 Objectif: Creates an object by calling a method instead of a constructor.
 Demo :
Step 1
Create an interface.
[Link]
public interface Operator {
void pay();
}

Step 2
Create concrete classes implementing the same interface.
[Link]
public class Mtn implements Operator {

@Override
public void pay() {
[Link]("Inside Mtn::pay() method.");
}
}
[Link]
public class Vodaphone implements Operator {

@Override
public void pay() {
[Link]("Inside Vodaphone::pay() method.");
}
}
[Link]
public class Orange implements Operator {

@Override
public void pay() {
[Link]("Inside Orange::pay() method.");
}
}

Step 3
Create a Factory to generate object of concrete class based on given information.
[Link]
public class OperatorFactory {

//use getOperator method to get object of type operator


public Operator getOperator(String OperatorType){
switch (OperatorType)
{
case "MTN":
return new Mtn();
case "Vodaphone":
return new Vodaphone();
case "Orange":
return new Orange();
case:
throw new NotSupportedException ();
}

}
}

Step 4
Use the Factory to get object of concrete class by passing an information such as type.
[Link]
public class FactoryPatternDemo {

public static void main(String[] args) {


OperatorFactory opFactory = new OperatorFactory ();

//get an object of Mtn and call its pay method.


Operator op = [Link]("MTN");

//call pay method of Mtn


[Link]();

//get an object of Vodaphone and call its pay method.


Operator op2 = [Link]("Vodaphone");
[Link]();
}
}

 Best Practice:
 Use the Factory Method when you don’t know beforehand the exact types and
dependencies of the objects : to add a new product type to the app, we’ll only need
to create a new creator subclass and override the factory method in it.
 Use the Factory Method when you want to save system resources by reusing existing
objects instead of rebuilding them each time.

Common questions

Powered by AI

Yes, the Factory Pattern can be used to return the same instance multiple times by maintaining a cached or singleton reference to the created instance and returning this reference upon subsequent requests, instead of creating a new instance every time. This approach conserves resources by reusing existing objects .

To extend the Factory Pattern to support a new type of operator, one would first create a new class that implements the Operator interface, defining the specific behavior for the pay method. Then, the OperatorFactory class must be updated with a new case in the switch statement within the getOperator method to handle the instantiation of this new class when requested .

The Factory Pattern can potentially reduce system performance due to the level of indirection it introduces. This indirection can lead to overhead because every time an object is created through the factory, the system must resolve which specific subclass or instance to instantiate, which adds computational steps compared to direct instantiation .

To implement the Factory Pattern for a new product line, follow these basic steps: 1) Define an interface common to all products (e.g., Operator). 2) Create concrete classes for new product types that implement this interface. 3) Update or create a Factory class (e.g., OperatorFactory) that contains a method to return objects of these concrete classes based on input criteria. 4) Use the factory method in the application to instantiate objects, passing the required data to specify which product is needed .

The Factory Pattern enhances software architecture clarity by providing a structured way to create objects, thus allowing a clean separation of the code that specifies how objects are created and which objects are created. This leads to a more organized system architecture, making it easier to understand and modify .

The primary advantages of using the Factory Pattern include reusability across multiple projects, system structure definition, clarity in system architecture, design transparency, it being a well-tested solution, and reduced time spent on problem-solving .

The Factory Pattern differs from using constructors directly in that it encapsulates object creation logic within a separate entity, the factory. This allows the code to abstract out the instantiation process, which helps in handling complex creation logic, enforcing encapsulation, and easily adapting or extending the object-creation process without modifying the client code, unlike direct constructor calls .

The Factory Pattern offers significant benefits when integrating new system components as it allows adding new product types with minimal changes. The system only requires creating a new subclass and possibly overriding a factory method. This minimizes disruption and promotes scalability by isolating object creation logic .

The Factory Pattern is categorized under creational design patterns. Its main objective in this category is to define the process of creating an object by encapsulating the instantiation logic and using a method instead of a constructor directly .

When implementing the Factory Pattern in a project with performance constraints, the trade-offs include balancing the benefits of enhanced modularity, reusability, and system architecture clarity against the potential performance impacts due to added indirection and complexity in object creation. While these patterns vastly improve code maintenance and scalability, they may introduce latency due to the additional steps involved in resolve object types, which may not be suitable for high-performance applications .

You might also like