Enhance Your Dart Skills: Understanding the Factory Design Pattern

Shivam Srivastava
2 min readMar 18, 2024

--

Are you a Flutter developer looking to level up your coding skills? If so, you’ve likely heard of Design Patterns, powerful tools that help streamline code and improve maintainability. One such pattern, the Factory Pattern, deserves a closer look for its ability to simplify object creation in Dart applications.

The Factory Pattern is a creational design pattern used to create objects. Instead of directly calling a constructor, the Factory Pattern provides a centralized method for creating instances of classes.

Why Use the Factory Pattern?

  1. Centralized object creation: By providing a single location for object creation, the Factory Pattern makes it easier to manage and modify the creation process. This improves code organization and reduces redundancy.
  2. Decoupling: Clients requesting objects are decoupled from the specific classes those objects belong to. This separation enhances adaptability, as clients remain unaware of the intricacies of object creation.

When to Use the Factory Pattern

The Factory Pattern shines in scenarios where object creation involves complex logic or requires additional setup. This pattern simplifies the process by encapsulating the creation logic within factory methods.

Example

The Factory Pattern can be very effective in managing different payment methods in an e-commerce app. Let’s consider a scenario where your e-commerce platform supports multiple payment gateways like Credit card, UPI, and NetBanking. Using the Factory Pattern here can provide a clean and maintainable solution.

// Abstract class for Payment methods
abstract class PaymentMethod {
void doPayment(double amount);
}

// Concrete classes representing different payment methods
class CreditCardPayment implements PaymentMethod {
@override
void doPayment(double amount) {
print('Processing credit card payment of $amount');
}
}

class UpiPayment implements PaymentMethod {
@override
void doPayment(double amount) {
print('Processing Upi payment of $amount');
}
}

class NetBankingPayment implements PaymentMethod {
@override
void doPayment(double amount) {
print('Processing NetBanking payment of $amount');

}
}

// Factory class to create payment instances whichever user selects
class PaymentMethodFactory {
static PaymentMethod createPaymentMethod(String type) {
switch (type) {
case 'credit_card':
return CreditCardPayment();
case 'upi':
return UpiPayment();
case 'net_banking':
return NetBankingPayment();
default:
throw Argument Error('Invalid payment method');
}
}
}

void main() {
// Client code using the Factory Pattern, when user selects payment.
final paymentMethod1 = PaymentMethodFactory.createPaymentMethod('upi');

paymentMethod1.doPayment(2500.0);
// Output
// Processing Upi payment of 2500.0

}

Follow Shivam Srivastava for Flutter and Mobile Dev related posts.

Join Discord for Flutter community https://bit.ly/48QaLey

--

--

Shivam Srivastava

Managing Director@Navoki | Lead Flutter and Android Developer | Freelancer | Udacity Android Nanodegree Graduate | Android Trainer