290 likes | 398 Views
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
E N D
Computer Science 313 – Advanced Programming Topics Lecture 29:Template method Pattern
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 Design How can I write a component which coders can make look like their favorite Muppet?
Owner Wants to Increase Sales I want to start selling hot drinks, but my McWorkers are idiots who cannot keep 2 ideas at a time.
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 PatternsSlightly 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 Patterns Decorator 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 methodsfor 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 Sales I define the process in an abstract superclassand rely on separate coffee & tea classes to define brewing step.
API Writer Completes His Design I 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 But my studentsstill 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?