factory in programming languages
---
explain and format
```<output>
</output>
```
# Factory in Programming Languages
A factory in programming languages is a design pattern that provides an interface for creating objects without specifying their concrete classes. It's one of the most commonly used creational design patterns.
## Key Concepts
```
A factory method creates objects without exposing the instantiation logic to the client.
Instead of direct instantiation with 'new', clients use factory methods to create objects.
Factories allow for loose coupling by abstracting the object creation process.
```
## Examples in Different Languages
### Java
```java
// Product interface
interface Product {
void operation();
}
// Concrete products
class ConcreteProductA implements Product {
@Override
public void operation() {
System.out.println("Operation from Product A");
}
}
class ConcreteProductB implements Product {
@Override
public void operation() {
System.out.println("Operation from Product B");
}
}
// Factory class
class ProductFactory {
public Product createProduct(String type) {
if (type.equals("A")) {
return new ConcreteProductA();
} else if (type.equals("B")) {
return new ConcreteProductB();
}
return null;
}
}
// Client code
public class Client {
public static void main(String[] args) {
ProductFactory factory = new ProductFactory();
Product productA = factory.createProduct("A");
productA.operation();
Product productB = factory.createProduct("B");
productB.operation();
}
}
```
### Python
```python
from abc import ABC, abstractmethod
# Product interface
class Product(ABC):
@abstractmethod
def operation(self):
pass
# Concrete products
class ConcreteProductA(Product):
def operation(self):
return "Operation from Product A"
class ConcreteProductB(Product):
def operation(self):
return "Operation from Product B"
# Factory class
class ProductFactory:
def create_product(self, product_type):
if product_type == "A":
return ConcreteProductA()
elif product_type == "B":
return ConcreteProductB()
return None
# Client code
if __name__ == "__main__":
factory = ProductFactory()
product_a = factory.create_product("A")
print(product_a.operation())
product_b = factory.create_product("B")
print(product_b.operation())
```
### JavaScript
```javascript
// Product interface is implicit in JavaScript
// Concrete products
class ConcreteProductA {
operation() {
return "Operation from Product A";
}
}
class ConcreteProductB {
operation() {
return "Operation from Product B";
}
}
// Factory class
class ProductFactory {
createProduct(type) {
if (type === "A") {
return new ConcreteProductA();
} else if (type === "B") {
return new ConcreteProductB();
}
return null;
}
}
// Client code
const factory = new ProductFactory();
const productA = factory.createProduct("A");
console.log(productA.operation());
const productB = factory.createProduct("B");
console.log(productB.operation());
```
## Benefits of Factory Pattern
1. **Encapsulation**: Hides the creation logic from client code
2. **Single Responsibility**: Separates object creation from usage
3. **Open/Closed Principle**: New types can be added without modifying existing code
4. **Flexibility**: Makes code more maintainable and adaptable to change
Factory patterns are especially useful when object creation involves complex processes, when you want to centralize object creation logic, or when you need to create objects based on conditions.