> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/system-design/design-principles-and-patterns/design-pattern/examples/transaction-dispute.md).

# Transaction Dispute

## Context

In many enterprise applications, such as payment platforms or banking systems, a **transaction dispute** can occur due to different reasons:

* Unauthorized access
* Duplicate transaction
* Product not received
* Service-related issues

Each type of dispute requires **distinct validation and handling logic**.

## Problem Statement

We receive a **dispute type** as an `enum` from a request parameter in a REST controller. We want to:

* Route this enum to the correct validation strategy (bean)
* Call its `validateDispute()` method
* Avoid large if-else or switch-case blocks

## Design Solution

This fits the **Strategy Pattern**, where:

* The `enum` acts as a **strategy key**
* Each `DisputeHandler` is a **strategy implementation**
* Spring injects the correct bean based on the enum at runtime

## Structure

#### A. Dispute Type Enum

```java
public enum DisputeType {
    UNAUTHORIZED,
    DUPLICATE,
    PRODUCT_NOT_RECEIVED,
    SERVICE_ISSUE
}
```

#### B. Strategy Interface

```java
public interface DisputeHandler {
    void validateDispute(Transaction transaction);
    DisputeType getSupportedType();
}
```

#### C. Implementations

```java
@Component
public class UnauthorizedDisputeHandler implements DisputeHandler {

    @Override
    public void validateDispute(Transaction transaction) {
        // Logic for unauthorized dispute
    }

    @Override
    public DisputeType getSupportedType() {
        return DisputeType.UNAUTHORIZED;
    }
}
```

Repeat similar beans for other dispute types.

#### D. Strategy Resolver Using Enum Map

```java
@Service
public class DisputeValidationService {

    private final Map<DisputeType, DisputeHandler> handlerMap;

    @Autowired
    public DisputeValidationService(List<DisputeHandler> handlers) {
        this.handlerMap = handlers.stream()
            .collect(Collectors.toMap(DisputeHandler::getSupportedType, Function.identity()));
    }

    public void validateDispute(DisputeType type, Transaction txn) {
        DisputeHandler handler = handlerMap.get(type);
        if (handler == null) {
            throw new IllegalArgumentException("Unsupported dispute type: " + type);
        }
        handler.validateDispute(txn);
    }
}
```

#### E. Controller Layer

```java
@RestController
@RequestMapping("/api/disputes")
public class DisputeController {

    private final DisputeValidationService disputeService;

    public DisputeController(DisputeValidationService disputeService) {
        this.disputeService = disputeService;
    }

    @PostMapping("/validate")
    public ResponseEntity<Void> validate(
        @RequestParam DisputeType type,
        @RequestBody Transaction transaction
    ) {
        disputeService.validateDispute(type, transaction);
        return ResponseEntity.ok().build();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/system-design/design-principles-and-patterns/design-pattern/examples/transaction-dispute.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
