OO Flashcards
What’s the difference between a parameter and an argument?
Parameters don’t have value, they are just the names of the inputs in function/method definition while arguments are the values passed in for the parameters when function/method is called
Explain why you should “encapsulate what varies”
Separating what changes frequently from what stays the same frequently prevents having to rewrite logic every time a new type of change is introduced.
Program to a ____ not a ____
interface | implementation
Why is it preferred to program to an interface and not an implementation?
Allows implementation to change/improve, without affecting clients.
Complete the design principle: Encapsulate what ___
varies
Complete the design principle: Favor ____ over ____
composition | inheritance
Why is composition usually preferred over inheritance.
Allows for loose coupling. Can get many of same benefits of inheritance (reuse attributes, methods) but with more flexibility to behave differently.
“Has a” defines what kind of relationship?
Composition
“Is a” implies what kind of relationship?
Inheritance
Define Aggregation
Comprised of one or more of something, in a “has a” relationship. Still is a thing if it loses all those somethings.
What is composition?
When an object is comprised of one more more of something else in a “has a” relationship.
Explain the single responsibility principle.
Class should only have one reason to change because every additional responsibility increases the chance of change, and we want to minimize code changes (prefer code additions).
What is the idea behind loose coupling?
Composition is loose coupling, inheritance is strong coupling. Loose coupling is often more flexible.
What is the open closed principle?
Open for extension but close for modification. Should be able to augment an object without opening up its code, decorator a good example of this.
Explain the strategy pattern.
If a certain aspect of something can change a lot, or would change in the future, encapsulate what varies and place the business logic in something else, which can be a component of the original object.