What is the design pattern for invoking different implementations according to different parameter types?

for example:

  1. A system connects three distribution platforms, namely platform A, platform B and platform C
  2. for upper-level business, you only need to take your own order (this part of the attribute is fixed) object as a parameter, as well as the platform to be delivered, such as platform A, to call the API
  3. .
  4. platform D may be added in the future. Which design pattern can be flexibly extended without modifying the interface?
  5. what you can think of now is that, according to this type, different platforms are called, and a lot of
    if (type==A_CODE) {
    / / call platform A"s ordering interface}
    else if (type==B_CODE) {
    / / call platform B"s ordering interface
    } else if (type==C_CODE) {
    / / call platform C"s ordering interface
    }

in this way, if you want to refactoring, is there a suitable design pattern?

Jan.08,2022

responsibility chain. For the new platform, you only need to add an implementation. Cooperate with spi Maze


like eliminating if..else.. Just use a Map < Type,Impl > to map different implementation classes, and add a key by extending one type. Is it a strategic model?


Policy mode is the most appropriate.

Policy pattern definition
Policy pattern (Strategy Pattern), encapsulates various algorithms into concrete classes as subclasses of an abstract policy class, making them interchangeable. The client can decide which algorithm to use.

To put it simply, the

policy pattern is to choose different implementations of the same interface according to different parameters.

you can refer to this article Java Design pattern (XII) Strategy pattern

Menu