Putting Objects Together
Putting Objects Together: Inheritance and Composition: Is-a and Has-a, Delegation: Handing the Job On, Functions as Values.
Updated
- Inheritance and Composition: Is-a and Has-a (Apprentice): Two ways to build a class from another: inheritance, where an e-bike is a bike and overrides its price, and composition, where a bike has a lock it keeps as a field; saying the relationship aloud, is-a or has-a, tells you which one fits.
- Delegation: Handing the Job On (Apprentice): When every screen reaches through a rental to price its bike, a new rule such as a $2 helmet has to be added everywhere, and one place is missed; delegation gives the rental its own price() that hands the bike's part on to the bike, the move half the design patterns are built from.
- Functions as Values (Apprentice): A weekend discount as a true/false flag means the total's code changes with every new deal; functions as values let the caller hand in the rule instead: a function stored in a variable, passed to another function, or made and returned by one, in C#, Java, Python, JavaScript and TypeScript.