99. Refactor Driver–Bus Relationship

You are given a program that models drivers communicating over a hardware bus.

The current design is incorrect because:

  • Each driver creates its own bus
  • In real embedded systems, a single hardware bus is shared
  • Drivers incorrectly behave like buses

Your task is to fix the design without changing the behavior or output.

What the Program Must Achieve (End Goal):

  • There must be exactly one Bus
  • Two drivers must share the same Bus
  • Drivers must use the bus but must not be buses
  • The bus must start once and stop once

What Is Given to You:

  • A working but incorrectly designed program
  • Correct output format
  • An incorrect class relationship

What You Must Change (Very Explicit):

  • Driver must NOT inherit from Bus
  • Driver must contain or reference a Bus
  • ✅ Both drivers must use the same Bus object
  • ❌ Do NOT change input/output format
  • ❌ Do NOT add new features

In short:
Replace inheritance with composition so the bus can be shared.

Program Flow:

  1. Read four integers
    • Driver-1 address and value
    • Driver-2 address and value
  2. Create one Bus
  3. Create two Driver objects using the same Bus
  4. Each driver performs one write
  5. Program ends
  6. Bus shuts down once

Input:
Four integers

addr1 val1
addr2 val2

Output (Exact Order):

Bus ready
Driver started
Driver started
Bus write: <addr1> <val1>
Bus write: <addr2> <val2>
Driver stopped
Driver stopped
Bus stopped

Constraints:

  • Only one Bus object may exist
  • Bus must NOT be created inside Driver
  • Driver must NOT expose write() directly
  • No dynamic allocation
  • No smart pointers
  • No inheritance
  • Output must match exactly

 

 

Loading...

Input

10 20 30 40

Expected Output

Bus ready Driver started Driver started Bus write: 10 20 Bus write: 30 40 Driver stopped Driver stopped Bus stopped