11-47's picture choucsan's picture
Duplicate from choucsan/mimo-claude-code-traces-1k
aeb931a
|
Raw History Blame Contribute Delete
7.19 kB
Claude Code trace
User 2026-06-21T12:14:25.994Z

Restructure this code to follow composition over inheritance:

python
def calculate(data):
    total = 0
    for item in data:
        if item['type'] == 'A':
            total += item['value'] * 1.1
        elif item['type'] == 'B':
            total += item['value'] * 0.9
        elif item['type'] == 'C':
            total += item['value'] * 1.05
    return total
Assistant mimo-v2.5-pro 2026-06-21T12:14:26.994Z 4,278↓ 517↑ (44,928 cached)
Thinking The user wants to restructure code to follow composition over inheritance. This is a straightforward refactoring task - no need for planning mode. I'll use a strategy pattern approach where different item types are handl...

The user wants to restructure code to follow composition over inheritance. This is a straightforward refactoring task - no need for planning mode. I'll use a strategy pattern approach where different item types are handled by composable strategy objects.

Assistant mimo-v2.5-pro 2026-06-21T12:14:28.994Z 4,278↓ 517↑ (44,928 cached)
Thinking Let me review what I wrote to make sure it's clean and correct. The key changes: - Extracted a `PricingStrategy` class that encapsulates the multiplier logic - Created a `STRATEGIES` dict mapping type codes to strategy i...

Let me review what I wrote to make sure it's clean and correct.

The key changes:

  • Extracted a PricingStrategy class that encapsulates the multiplier logic
  • Created a STRATEGIES dict mapping type codes to strategy instances
  • calculate now accepts an optional strategies parameter for composability
  • New types can be added by registering a new PricingStrategy in the dict — no modification to calculate needed

This is clean. Let me present the summary.

The refactoring extracts each type's pricing logic into a composable PricingStrategy object, then looks it up at runtime:

  • PricingStrategy — a simple callable that holds a multiplier and applies it to a value.
  • STRATEGIES dict — maps type codes ('A', 'B', 'C') to strategy instances. Adding a new type means adding one dict entry; calculate never changes.
  • calculate accepts an optional strategies argument, so callers can inject custom strategies at test time or for different business contexts.

New types are added without touching calculate:

python
STRATEGIES['D'] = PricingStrategy(2.0)
mimo-v2.5-pro Input 8.56k↓ Input Cached 89.9k↓ (91.3%) Output 1.03k↑