调用多SOAP服务时替代if-else的设计模式选型咨询
Great question! When you're dealing with conditional logic that maps different input values to distinct actions (like your indicator-based SOAP service calls), the Strategy Pattern paired with a Factory Pattern is the perfect combo to eliminate messy if-else chains. Let me break this down for you:
Step 1: Define a Strategy Interface
First, create an interface that encapsulates the common action of calling a subsequent SOAP service using the response from SOAP Service 1. This ensures all your service call logic follows a consistent contract.
// Strategy interface for subsequent SOAP service calls public interface SoapServiceStrategy { void execute(SoapService1Response soapService1Response); }
Step 2: Implement Strategy Classes for Each SOAP Service
Create a concrete implementation of the strategy interface for every downstream SOAP service (Service 2, 3, etc.). Each class handles the request mapping and service call specific to its target SOAP service, keeping logic isolated and focused.
// Strategy for calling SOAP Service 2 public class SoapService2Strategy implements SoapServiceStrategy { // Inject your SOAP client for Service 2 (via constructor/DI in real apps) private final SoapService2Client soapService2Client; public SoapService2Strategy(SoapService2Client soapService2Client) { this.soapService2Client = soapService2Client; } @Override public void execute(SoapService1Response response) { // Map SOAP Service 1's response to Service 2's request format SoapService2Request request = mapToService2Request(response); // Call SOAP Service 2 SoapService2Response service2Response = soapService2Client.invoke(request); // Add post-call processing logic here if needed } private SoapService2Request mapToService2Request(SoapService1Response response) { // Implement your custom mapping between responses and requests return new SoapService2Request(response.getUserId(), response.getTransactionDetails()); } } // Strategy for calling SOAP Service 3 public class SoapService3Strategy implements SoapServiceStrategy { private final SoapService3Client soapService3Client; public SoapService3Strategy(SoapService3Client soapService3Client) { this.soapService3Client = soapService3Client; } @Override public void execute(SoapService1Response response) { SoapService3Request request = mapToService3Request(response); SoapService3Response service3Response = soapService3Client.invoke(request); // Post-processing logic here } private SoapService3Request mapToService3Request(SoapService1Response response) { return new SoapService3Request(response.getOrderId(), response.getCustomerInfo()); } }
Step 3: Create a Factory to Retrieve Strategies
Use a factory class to manage the mapping between indicator values and their corresponding strategy implementations. This replaces your if-else chain with a clean key-value lookup.
public class SoapServiceStrategyFactory { private static final Map<Integer, SoapServiceStrategy> STRATEGY_MAP = new HashMap<>(); // Initialize the strategy map (use dependency injection if using Spring/Boot) static { STRATEGY_MAP.put(1, new SoapService2Strategy(new SoapService2Client())); STRATEGY_MAP.put(2, new SoapService3Strategy(new SoapService3Client())); STRATEGY_MAP.put(3, new SoapService4Strategy(new SoapService4Client())); // Add more as needed } public static SoapServiceStrategy getStrategy(int indicator) { SoapServiceStrategy strategy = STRATEGY_MAP.get(indicator); if (strategy == null) { // Handle unknown indicators (throw exception, return default strategy, etc.) throw new IllegalArgumentException("No strategy found for indicator: " + indicator); } return strategy; } }
Step 4: Use the Factory in Your Microservice
Your microservice code becomes clean and concise—no more tangled if-else blocks!
public class YourMicroservice { private final SoapService1Client soapService1Client; public YourMicroservice(SoapService1Client soapService1Client) { this.soapService1Client = soapService1Client; } public void processRequest() { // 1. Call SOAP Service 1 first SoapService1Response service1Response = soapService1Client.invoke(new SoapService1Request()); // 2. Get the appropriate strategy based on the indicator int indicator = service1Response.getIndicator(); SoapServiceStrategy strategy = SoapServiceStrategyFactory.getStrategy(indicator); // 3. Execute the strategy (calls the correct downstream SOAP service) strategy.execute(service1Response); } }
Why This Works
- Eliminates if-else bloat: Adding a new SOAP service only requires a new strategy class and a map entry in the factory—no need to modify existing conditional logic.
- Follows SOLID principles: Each strategy class has a single responsibility, and the design is open to extension but closed to modification.
- Improves testability: You can test each strategy independently without mocking the entire microservice flow.
内容的提问来源于stack exchange,提问作者VIPUL

