OO Flashcards

1
Q

What’s the difference between a parameter and an argument?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain why you should “encapsulate what varies”

A

Separating what changes frequently from what stays the same frequently prevents having to rewrite logic every time a new type of change is introduced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Program to a ____ not a ____

A

interface | implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is it preferred to program to an interface and not an implementation?

A

Allows implementation to change/improve, without affecting clients.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Complete the design principle: Encapsulate what ___

A

varies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Complete the design principle: Favor ____ over ____

A

composition | inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is composition usually preferred over inheritance.

A

Allows for loose coupling. Can get many of same benefits of inheritance (reuse attributes, methods) but with more flexibility to behave differently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

“Has a” defines what kind of relationship?

A

Composition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

“Is a” implies what kind of relationship?

A

Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define Aggregation

A

Comprised of one or more of something, in a “has a” relationship. Still is a thing if it loses all those somethings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is composition?

A

When an object is comprised of one more more of something else in a “has a” relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the single responsibility principle.

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the idea behind loose coupling?

A

Composition is loose coupling, inheritance is strong coupling. Loose coupling is often more flexible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the open closed principle?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the strategy pattern.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the adapter pattern.

A

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.

17
Q

Explain the observer pattern.

A

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.

18
Q

Explain the iterator pattern

A

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.

19
Q

Explain the decorator pattern.

A

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.

20
Q

What does it mean that a function has side effects?

A

It doesn’t return anything. Maybe it alters an array or writes to a file, or prints to screen, etc

21
Q

What is a pure function?

A

Always will return the same output, given the same input, every time.

22
Q

What’s the difference between a method and a function (at least in Python)?

A

Method is a function that belongs to an object. Function is it’s own independent object.

23
Q

True or false, you should define all instance attributes of an object in its constructor?

A

True. Otherwise someone may try to access an attribute that has not yet been set by a method. Can initialize to null or None.