click below
click below
Normal Size Small Size show me how
OOP - 5.2
Contract Programming
| Question | Answer |
|---|---|
| A way of writing software where each class and method declares a contract | Contract Programming |
| what it expects | preconditions |
| what it guarantees | postconditions |
| must always hold true about its state | invariants |
| 3 Core Concepts: | preconditions postconditions invariants |
| - What must be true before a method runs. - Responsibility of the caller - If violated, method should reject execution. - Ex. The number must not be negative | Preconditions |
| What must be true after the method finishes. - Responsibility of the method itself - Ensures correct behavior and output. - Ex. The result is completely computed | Postconditions |
| - What must always be true for an object - Maintained throughout the object’s lifetime - Ex. A bank account balance is never negative | Invariants |
| Roles in Contract Programming | Caller or Client Implementer or Supplier Method or Service Provider |
| The caller is the part of the program that invokes a method or requests a services | Caller or Client |
| The implementer is the developer or class responsible for writing the method and enforcing the contract | Implementer or Supplier |
| The method is the actual implementation where the contract is executed. | Method or Service Provider |