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.
Explain the adapter pattern.
Think of a electrical socket adapter. The device has no idea it’s using an adapter, the socket has no idea what’s plugged in is an adapter. You can make a work with b by making a c, rather than modifying a or b. Good for when libraries are changed.
Explain the observer pattern.
One to many dependency. One thing keeps track of state, other objects can register themselves with the observer. When state changes, observer notifies all registered objects.
Explain the iterator pattern
Iterator is essentially an agreed upon interface in a particular language, so any type that implements it can be iterated through in a consistent manner, regardless of what the underlying implementation looks like.
Explain the decorator pattern.
Wrap something up to provide additional logic or attributes. Can be done infinitely. Rather than have a million different attributes and stuff to denote coffee types, have coffee be wrapped up in with whip, with sugar. Each wrapper should behave like a regular coffee type, and contain what its wrapping up.
What does it mean that a function has side effects?
It doesn’t return anything. Maybe it alters an array or writes to a file, or prints to screen, etc
What is a pure function?
Always will return the same output, given the same input, every time.
What’s the difference between a method and a function (at least in Python)?
Method is a function that belongs to an object. Function is it’s own independent object.
True or false, you should define all instance attributes of an object in its constructor?
True. Otherwise someone may try to access an attribute that has not yet been set by a method. Can initialize to null or None.