Group Group Group Group Group Group Group Group Group

Why use convenience, self.init and not super.init()

In the memento pattern chapter, you add a BaseStrategy and make it a base class for Random and Sequential which I understand. Why though do you make the init a convenience?

Couldn’t you just call super.init() and remove the convenience?

Hi @mcneils, The initialization of each strategy will vary depending on the approach to implement the desired behavior

In this case convenience init just simplifies the number of arguments to take while creating a new strategy and allows you to manipulate arguments before initializing BaseQuestionStrategy

// BaseQuestionStrategy
public init(questionGroupCaretaker: QuestionGroupCaretaker, questions: [Question]) {
  ...
}

// RandomQuestionStrategy
public convenience init(questionGroupCaretaker: QuestionGroupCaretaker) {
  let questionGroup = questionGroupCaretaker.selectedQuestionGroup!
  let questions = questionGroup.questions
  self.init(questionGroupCaretaker: questionGroupCaretaker, questions: questions)
}

// SequentialQuestionStrategy
public convenience init(questionGroupCaretaker: QuestionGroupCaretaker) {
  let questionGroup = questionGroupCaretaker.selectedQuestionGroup!
  let questions = questionGroup.questions
  self.init(questionGroupCaretaker: questionGroupCaretaker, questions: questions)
}

Hope it helps!!
Good luck :]

1 Like