lecture 29 : template method pattern

29
LECTURE 29: TEMPLATE METHOD PATTERN Computer Science 313 – Advanced Programming Topics

Upload: sasson

Post on 15-Feb-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Computer Science 313 – Advanced Programming Topics. Lecture 29 : Template method Pattern. Professor. Professor Has A Problem. I need a cool assignment ,. Professor Has A Problem. I need a cool assignment , but my students only chug beer . API Coder Struggles With Design. How can I - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture  29 : Template method Pattern

LECTURE 29:TEMPLATE METHOD PATTERN

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture  29 : Template method Pattern

Professor

Page 3: Lecture  29 : Template method Pattern

Professor Has A Problem

I need a cool assignment,

Page 4: Lecture  29 : Template method Pattern

Professor Has A Problem

I need a cool assignment,

butmy students

only chug beer

Page 5: Lecture  29 : Template method Pattern

API Coder Struggles With DesignHow can I

write a component which coders can make

look like their favorite Muppet?

Page 6: Lecture  29 : Template method Pattern

Owner Wants to Increase SalesI want to

start selling hot drinks, but my

McWorkers are idiots who cannot keep 2 ideas at a

time.

Page 7: Lecture  29 : Template method Pattern

That Days Headline…

Page 8: Lecture  29 : Template method Pattern

What Do They Have in Common? Each group member has single

algorithm Boil, Brew, Pour, & Serve for the Clown Nerd must write Component & have Muppet

drawn One-handed, but cool, assignment for

Professor But these algorithms will be specialized

Completion of assignment left to the drunkards

Programmers will add Muppet drawings to API

Make coffee or tea based on order at McDs

Page 9: Lecture  29 : Template method Pattern

Therapist Provides a Suggestion This is

complex. Perhaps a

design pattern can

help?

Page 10: Lecture  29 : Template method Pattern

To the Design Pattern Bin

Design Patterns

Slightly Used

Page 11: Lecture  29 : Template method Pattern

Rejecting the Design Patterns

Strategy pattern

sounds good, but

I already have an algorithm.

Page 12: Lecture  29 : Template method Pattern

Rejecting the Design Patterns

Observer pattern?Dude, the

Component can only look like one Muppet at a time.

Page 13: Lecture  29 : Template method Pattern

Rejecting the Design PatternsDecorator pattern?

You need to add new

functionality!

Page 14: Lecture  29 : Template method Pattern

Rejecting the Design Patterns

But how does that make you feel about your

mother?

Page 15: Lecture  29 : Template method Pattern

Rejecting the Design Patterns

If only Gilligan were here…

Page 16: Lecture  29 : Template method Pattern

Rejecting the Design Patterns

…but this Abstract Factory gives me an idea.

Page 17: Lecture  29 : Template method Pattern

All About the Hooks

The Professor began to think Never let a Professor think, it ends in

bad places

Page 18: Lecture  29 : Template method Pattern

All About the Hooks

The Professor began to think Never let a Professor think, it ends in

bad places Like this lecture…

Page 19: Lecture  29 : Template method Pattern

All About the Hooks

The Professor began to think Never let a Professor think, it ends in

bad places He ends up wondering how velcro

works

Page 20: Lecture  29 : Template method Pattern

All About the Hooks

The Professor began to think Never let a Professor think, it ends in

bad places He ends up wondering how velcro

works

Page 21: Lecture  29 : Template method Pattern

Template Method Pattern Key Yes! We only

need to provide

some hooks.

Page 22: Lecture  29 : Template method Pattern

Source of the Brain Storm

Velcro works by creating lots of little hooks Loops on the connector then holds

things up Abstract factory method also creates

hooks Interface specifies methods for concrete

factories Clients create instances using these

hooks Subclasses create proper type for each

method

How could they use this idea?

Page 23: Lecture  29 : Template method Pattern

Template Method Pattern

Superclass defines the steps of the algorithm Specifies the single algorithm to

implement Creates abstract methods for missing

details Classes extend and complete

superclass Provide own copies of the abstract

methods Method defining algorithm inherited

automatically Client’s assume superclass is

complete Instances define details of the

implementation

Page 24: Lecture  29 : Template method Pattern

Store Owner Increases SalesI define the process in an

abstract superclass and

rely on separate coffee & tea

classes to define brewing step.

Page 25: Lecture  29 : Template method Pattern

API Writer Completes His DesignI wrote my Component but

left repaintMuppet

abstract for users to define.

Page 26: Lecture  29 : Template method Pattern

API Writer Completes His Design

Now I have to choose:

Miss Piggy or Fozzy?

Page 27: Lecture  29 : Template method Pattern

Professor Has Cool Assignment

Page 28: Lecture  29 : Template method Pattern

Professor Has Cool Assignment

But my students still just want to chug beer

Page 29: Lecture  29 : Template method Pattern

For Next Class

Current lab due on Wednesday Next cool lab assignment already on web Means no lab over Spring Break

Next set of reports due Mon., April 12th Read pages 287 – 298 for Wednesday

How do we implement the Template Method pattern?

Where have we used this before?