I read the Wikipedia articles on FactoryMethod and AbstractFactory but the following code doesn't seem to fit anywhere. Can someone explain to me what the following pattern is or if it is an anti-pattern?
interace PaymentGateway{
void makePayment();
}
class PaypalPaymentGateway implements PaymentGateway
{
public void makePayment()
{
//some implementation
}
}
class AuthorizeNetPaymentGateway implements PaymentGateway
{
public void makePayment()
{
//some implementation
}
}
class PaymentGatewayFactory{
PaymentGateway createPaymentGateway(int gatewayId)
{
if(gatewayId == 1)
return PaypalPaymentGateway();
else if(gatewayId == 2)
return AuthorizeNetPaymentGateway();
}
}
Let's say the user selects the payment method using a radio button on an html page and the gatewayId is derived from the radio button value.
I have seen code like this and thought it was the AbstractFactory pattern but after reading the Wikipedia article, I'm having doubts.
the gateway class implements a strategy pattern with selection delegated to what I'd call a parameterized factory.
If you want to reduce it to one of the GOF patterns I'd say it is more like a builder pattern, condensed into a shorthand call instead of setting the strategy and calling build() afterward.
If you want to extend to Fowler patterns, you can compare it to a Registry
In Craig Larman's book "Applying UML and Patterns" (Section 26.4) he refers to this as the "Simple Factory" or "Concrete Factory" and says it's not a GoF pattern, but is extremely widespread.
"Head First Design Patterns" also devotes space to this pattern:
[Edit] I like this blog entry explaining the difference between Simple Factory, Factory Method and Abstract Factory.
This is not the factory pattern, abstract factory pattern or the builder pattern.There are specific intents of them and non of them matches with the code.
But as some have suggested, interface PaymentGateway and concrete classes implement the strategy pattern. The intent of the strategy pattern matches perfectly: "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."
Strategy pattern need not necessarily possess on-the-fly behavior change. It depends on the client code (the code that uses the strategy pattern abstraction).
PaymentGatewayFacotry doesn't match with any GoF pattern. It doesn't match with the intent of the factory pattern, abstract factory pattern or the builder pattern. As Lorenzo has suggested, may be you can call it a parameterized factory or a parameterized selector. It is a badly coded class though. It violates the open-close principle.