What is this Design Pattern?

2019-07-01 19:32发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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:

The Simple Factory isn’t actually a Design Pattern; it’s more of a programming idiom. But it is commonly used, so we’ll give it a Head First Pattern Honorable Mention. Some developers do mistake this idiom for the “Factory Pattern,” so the next time there is an awkward silence between you and another developer, you’ve got a nice topic to break the ice.

Freeman, Eric; Robson, Elisabeth; Bates, Bert; Sierra, Kathy (2004-10-25). Head First Design Patterns (Kindle Locations 1920-1923). O'Reilly Media. Kindle Edition.

[Edit] I like this blog entry explaining the difference between Simple Factory, Factory Method and Abstract Factory.