Algorithms Georgi Yosifov ... -...

15
Encapsulating Algorithms ELSYS 2014/2015 Vasil Kostov Georgi Yosifov

Transcript of Algorithms Georgi Yosifov ... -...

Page 1: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

EncapsulatingAlgorithms

ELSYS 2014/2015Vasil Kostov

Georgi Yosifov

Page 2: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 3: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 4: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Let’s play “coding barista” and write some code for creating coffee and tea.

Page 5: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

and now the Tea...

Page 6: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 7: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Sir, may I abstract your Coffee, Tea?

Page 8: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Taking the design further...

● So what else do Coffee and Tea have in common? Let’s start with the recipes.

Page 9: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Abstracting prepareRecipe()

● The first problem we have is that Coffee uses brewCoffeeGrinds() and addSugarAndMilk() methods while Tea uses steepTeaBag() and addLemon() methods.

Page 10: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 11: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 12: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Meet the Template MethodThe Template Method defines the steps of an algorithm and allowssubclasses to provide the implementation for one or more steps.

Page 13: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

Template Method Pattern defined

Page 14: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do
Page 15: Algorithms Georgi Yosifov ... - lubo.elsys-bg.orglubo.elsys-bg.org/wp-content/uploads/2015/01/11-Algorithms.pdf · ELSYS 2014/2015 Vasil Kostov Georgi Yosifov. ... So what else do

The Hollywood Principle and Template Method

The connection between the Hollywood Principle and the Template Method Pattern is probably somewhat apparent: when we design with the Template Method Pattern, we’re telling subclasses, “don’t call us, we’ll call you.”