Factory Method Design Pattern in .NET — Real-Time Finance Example
The Factory Method design pattern is a classic solution to a recurring problem in software design — object creation without exposing the instantiation logic. Instead of using new directly in client code, the Factory Method delegates the creation responsibility to a method that returns an object implementing a common interface or abstract class.
What is the
Factory Method Pattern?
The Factory Method pattern defines an
interface or abstract method for creating an object but allows subclasses or
implementers to decide which class to instantiate. It hides object creation
logic from the client and promotes loose coupling.
Purpose
To encapsulate object creation, so the
code using the object does not depend on specific implementations. This
promotes better maintainability and extensibility, especially when new types
need to be introduced.
Key
Characteristics
- Defines
a common interface or abstract class for object creation
- Lets
subclasses or factory methods decide which class to instantiate
- Promotes
polymorphism and loose coupling
Pros
- Decouples
client code from concrete object types
- Supports
the Open/Closed Principle
- Makes
the codebase more flexible and maintainable
Cons
- Adds
complexity with additional classes and interfaces
- Overkill
for simple object creation
Use Cases
- Creating
different types of transactions (NEFT, IMPS, RTGS) in a banking system
- Generating
reports in different formats (PDF, Excel, CSV)
- Creating
notification services (Email, SMS, Push)
Real-Time Finance
Example: Bank Transaction Processing
In a banking system, you may need to process
various types of transactions. Instead of hardcoding each type, the Factory
Method can be used to create the transaction dynamically based on input.
1. Create a
Common Interface
public interface
IBankTransaction
{
void Process(decimal amount);
}
2. Implement
Concrete Classes
public class
NEFTTransaction : IBankTransaction
{
public void Process(decimal amount)
{
Console.WriteLine($"Processing
NEFT transaction of ₹{amount}");
}
}
public class IMPOSTransaction
: IBankTransaction
{
public void Process(decimal amount)
{
Console.WriteLine($"Processing
IMPS transaction of ₹{amount}");
}
}
public class
RTGSTransaction : IBankTransaction
{
public void Process(decimal amount)
{
Console.WriteLine($"Processing
RTGS transaction of ₹{amount}");
}
}
3. Create a
Factory
public static
class TransactionFactory
{
public static IBankTransaction
CreateTransaction(string type)
{
return type switch
{
"NEFT" => new
NEFTTransaction(),
"IMPS" => new
IMPOSTransaction(),
"RTGS" => new
RTGSTransaction(),
_ => throw new
ArgumentException("Invalid transaction type"),
};
}
}
4. Client Code
Usage
class Program
{
static void Main()
{
string transactionType =
"IMPS";
decimal amount = 5000;
IBankTransaction transaction =
TransactionFactory.CreateTransaction(transactionType);
transaction.Process(amount);
}
}
Benefits in This
Scenario
- Client
code does not need to know the concrete class name
- Adding
a new transaction type like UPI requires only a new class and a factory
entry
- Promotes
extensibility, loose coupling, and single responsibility
Summary
- Factory
Method is ideal when object creation should depend on configuration,
input, or conditions
- In
finance applications, this pattern handles the dynamic creation of
components like transactions, reports, or notifications
- It improves scalability and maintainability, especially in large systems with changing requirements
Comments
Post a Comment