Programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How would you explain the template method pattern?

A

Defining a basic structure in the superclass and sending messages to acquire subclass-specific contributions.

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

What maintainability rule should you employ when using the template method pattern?

A

Any class that uses the pattern must supply an implementation for every message it sends, even if it’s something like

def default_size
  raise NotImplementedError
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When refactoring using inheritance, what process should you use?

A
  1. Push concrete code down to the subclass
  2. Promote abstract code to the superclass
    (This makes failures more explicit and less hidden.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do I know when inheritance may be a fit?

A

Does your code contain an if statement that checks an attribute that holds the category of self to determine what message to send to self?

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

What does a hook method look like?

A

In subclasses, implement methods in the subclass so it doesn’t have to call super.

def post_initialize(args)
  @tape_color = args[:tape_color]
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why use hook methods?

A

If we have to call “super” in the subclass, the subclass has some knowledge of the algorithm. It’s cleaner to send hook messages that exist solely to provide subclasses a place to contribute information by implementing matching methods.

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

Is a Ruby class an object?

A

Yes.

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

What happens when methods are added to the Singleton class?

A

Any object can also have ad hoc methods added directly to its own personal “Singleton class”. These ad hoc methods are unique to this specific object.

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

What happens when Ruby doesn’t find a message the first time in the lookup chain?

A

Ruby gives the original receiver a second change by sending it a new message, method_missing, and passing :whatever as an argument. Attempts to resolve this new message restart the search along the same path, except now the search is for method_missing rather than spares.

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

What is an antipattern suggesting code could benefit from inheritance?

A

An object uses a variable with a name like “type” or category” to determine what message to send to self contains two highly related but slightly different types.

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

How can I know when I’ve overlooked a duck type?

A

When a sending object checks the class of a receiving object to determine what message to send.

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

Should I implement code in an abstract superclass that does not apply to every class that inherits it?

A

No. This creates quirks.

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

Must subclasses respond to every message in their superclasses’ interface?

A

Yes

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

What is an example of the template method pattern?

A

TODO: Furnish one.

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

Should a subtype be substitutable for its supertype?

A

Yes.

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