- Real-time Examples of Factory Pattern
Introduction to Factory Pattern
- Definition of Factory pattern
- Purpose and benefits of using Factory pattern
- Types of Factory pattern (Simple Factory, Factory Method, Abstract Factory)
UML Diagram of Factory Pattern
- Visual representation of the Factory pattern UML diagram
- Explanation of components:
- Product interface or superclass
- Concrete products
- Factory interface or superclass
- Concrete factories
Example 1: Shape Factory
- Problem Statement: Creating different shapes (e.g., Circle, Rectangle, Square) based on user input.
- Solution: Shape Factory using Factory Method pattern:
- Shape interface:
public interface Shape { void draw(); }
- Concrete shapes (Circle, Rectangle, Square):
public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing Rectangle"); } } public class Square implements Shape { @Override public void draw() { System.out.println("Drawing Square"); } }
- ShapeFactory:public class ShapeFactory {
public Shape createShape(String shapeType) { if (shapeType.equalsIgnoreCase("circle")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("rectangle")) { return new Rectangle(); } else if (shapeType.equalsIgnoreCase("square")) { return new Square(); } return null; } }
- Shape interface:
Example 2: Connection Factory
- Problem Statement: Creating database connections dynamically based on configuration.
- Solution: Connection Factory using Abstract Factory pattern:
- Connection interface:public interface Connection {
void connect(); }
- Concrete connections (MySQLConnection, OracleConnection):public class MySQLConnection implements Connection {
@Override public void connect() { System.out.println("Connecting to MySQL database..."); } } public class OracleConnection implements Connection { @Override public void connect() { System.out.println("Connecting to Oracle database..."); } }
- ConnectionFactory interface and concrete factories:
public interface ConnectionFactory { Connection createConnection(); } public class MySQLConnectionFactory implements ConnectionFactory { @Override public Connection createConnection() { return new MySQLConnection(); } } public class OracleConnectionFactory implements ConnectionFactory { @Override public Connection createConnection() { return new OracleConnection(); } }
- Connection interface:
Example 3: Payment Gateway
- Problem Statement: Implementing different payment methods (e.g., Credit Card, PayPal) for an e-commerce platform.
- Solution: Payment Gateway using Factory Method pattern:
- Payment interface:public interface Payment {
void pay(); }
- Concrete payments (CreditCardPayment, PayPalPayment):public class CreditCardPayment implements Payment {
@Override public void pay() { System.out.println("Processing credit card payment..."); } } public class PayPalPayment implements Payment { @Override public void pay() { System.out.println("Processing PayPal payment..."); } }
- PaymentFactory:public class PaymentFactory {
public Payment createPayment(String paymentType) { if (paymentType.equalsIgnoreCase("credit card")) { return new CreditCardPayment(); } else if (paymentType.equalsIgnoreCase("paypal")) { return new PayPalPayment(); } return null; } }
- Payment interface:
Benefits of Factory Pattern
- Provides flexibility by centralizing object creation logic
- Encapsulates object creation details from client code
- Supports open/closed principle by allowing easy extension with new products or variants
Considerations and Best Practices
- Choose the appropriate type of Factory pattern based on requirements (Simple Factory, Factory Method, Abstract Factory)
- Ensure proper naming conventions for factories and products
- Document factory interfaces and product hierarchies clearly
Real-world Application: GUI Framework
- Example: Creating different UI components (e.g., Button, Textbox) based on platform (Windows, macOS) using Abstract Factory pattern.
Conclusion
- Recap of Factory pattern benefits and applications
- Importance of design patterns in promoting maintainable and scalable software architecture