Delegation Pattern Flashcards

1
Q

The delegation pattern is a variation of another pattern known as:

A

Decorator

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

Why do we specify the delegate’s type as a protocol?

A

To restrict only the objects that conform to the protocol from being assigned to the delegate property

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

TF: A design pattern is a general, reusable solution to a commonly occurring problem within a given context, regardless of the particular domain.

A

True

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

Generally, why should the delegate property be marked as weak?

A

To prevent reference cycles when the delegate and owner reference each other

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

TF: A design pattern is a code snippet that you can add to your project.

A

False

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

What is the base class for all objective c classes?

A

NSObject

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

TF: You can assign a delegate both in code and in Interface Builder

A

True

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

Why can we not implement optional protocol requirements?

A

Optional protocol requirements are an objective c construct and are currently unavailable in Swift

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

What problems does delegation solve?

A
Avoiding tight coupling of objects 
Modifying behavior and appearance without the need to subclass objects
Allowing tasks to be handed off to an arbitrary object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What exactly is a design pattern?

A

A design pattern is a general, reusable solution to a commonly occurring problem within a given context, regardless of the particular domain.

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

What the purpose of using a delegate? Example

A

Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type.

protocol HorseRaceDelegate: class {
    func race(_ race: Race, didStartAt time: Date)
    func addLapLeader(_ horse: Horse, forLap lap: Int, atTime time: Date)
    func race(_ race: Race, didEndAt time: Date, withWinner winner: Horse)
}
class Race {
    weak var delegate: HorseRaceDelegate?
func updateLeader() {
	delegate?.addLapLeader(horse, forLap: horse.currentLap, atTime: Date())

}
}

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