lecture 29 : template method pattern
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 PresentationTRANSCRIPT
LECTURE 29:TEMPLATE METHOD PATTERN
Computer Science 313 – Advanced Programming Topics
Professor
Professor Has A Problem
I need a cool assignment,
Professor Has A Problem
I need a cool assignment,
butmy students
only chug beer
API Coder Struggles With DesignHow can I
write a component which coders can make
look like their favorite Muppet?
Owner Wants to Increase SalesI want to
start selling hot drinks, but my
McWorkers are idiots who cannot keep 2 ideas at a
time.
That Days Headline…
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
Therapist Provides a Suggestion This is
complex. Perhaps a
design pattern can
help?
To the Design Pattern Bin
Design Patterns
Slightly Used
Rejecting the Design Patterns
Strategy pattern
sounds good, but
I already have an algorithm.
Rejecting the Design Patterns
Observer pattern?Dude, the
Component can only look like one Muppet at a time.
Rejecting the Design PatternsDecorator pattern?
You need to add new
functionality!
Rejecting the Design Patterns
But how does that make you feel about your
mother?
Rejecting the Design Patterns
If only Gilligan were here…
Rejecting the Design Patterns
…but this Abstract Factory gives me an idea.
All About the Hooks
The Professor began to think Never let a Professor think, it ends in
bad places
All About the Hooks
The Professor began to think Never let a Professor think, it ends in
bad places Like this lecture…
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
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
Template Method Pattern Key Yes! We only
need to provide
some hooks.
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?
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
Store Owner Increases SalesI define the process in an
abstract superclass and
rely on separate coffee & tea
classes to define brewing step.
API Writer Completes His DesignI wrote my Component but
left repaintMuppet
abstract for users to define.
API Writer Completes His Design
Now I have to choose:
Miss Piggy or Fozzy?
Professor Has Cool Assignment
Professor Has Cool Assignment
But my students still just want to chug beer
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?