1 / 17

CSE432S

CSE432S. Strategy and Template Method Patterns, Single User Protection. Strategy Pattern. “Define a Family of Algorithms, encapsulate each one, and make them interchangeable.”. Strategy – The General Idea.

Télécharger la présentation

CSE432S

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE432S Strategy and Template Method Patterns, Single User Protection CSE432S - Nick Beary

  2. Strategy Pattern “Define a Family of Algorithms, encapsulate each one, and make them interchangeable.” CSE432S - Nick Beary

  3. Strategy – The General Idea • 3 Participants: Strategy, Context, Client (book has Strategy, ConcreteStrategy, Context). • Client interacts with the same interface (Context), but has many options for algorithms (strategies). • Context is the intermediary, but Client must have knowledge of Strategy CSE432S - Nick Beary

  4. Strategy – Consequences • Families of related algorithms • An alternative to subclassing • Strategies eliminate conditional statements • A choice of implementations • Clients must be aware of different Strategies • Communication overhead between Strategy and Context • Increased number of objects. CSE432S - Nick Beary

  5. Strategy – Implementation Issues • Defining the Strategy and Context interfaces • Strategies as template parameters • Making Strategy objects optional CSE432S - Nick Beary

  6. Template Method Pattern “Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.” CSE432S - Nick Beary

  7. Template Method – The General Idea • 2 participants: AbstractClass and ConcreteClass • Abstract Class defines parts of an algorithm that are invariant, leaves as virtual functions pieces of the algorithm that may or must be defined by Concrete Classes. • The Hollywood Principle – Clever metaphor, or needless pun? CSE432S - Nick Beary

  8. Template Method – Operations • Concrete Operations • Client class, Concrete Class • Abstract Class • Primitive Operations (abstract operations) • Hook Operations • Key Point: Distinguish operations which may be defined from those that must be defined. CSE432S - Nick Beary

  9. Template Method – Implementation Issues • Using C++ access control. • Minimizing primitive operations • Naming conventions CSE432S - Nick Beary

  10. Single User Protection Time for some sweet, sweet Template Method Pattern Action! CSE432S - Nick Beary

  11. Problems • Write Protection • How do we stop ourselves from making rash decisions about deletion and editing? • Read Protection • How do we keep things from our spouses and/or children? (Vlissides example, not mine) • Note: File System Level CSE432S - Nick Beary

  12. Write Protection • Should prevent any changes to a node (relatively) • Shouldn’t be able to be explicitly deleted, but protection might change at run-time, so can’t use const (C++). • Solution: Protect the destructor! CSE432S - Nick Beary

  13. Destructor Protection – Who Deletes? • The Node class • A class outside the Node class hierarchy • Creates unnecessary friend status. • A global function • Really no benefit over a static member function. CSE432S - Nick Beary

  14. Static or no? • Syntax like node->destroy(); and delete this; unsettles some. • Static member doesn’t support modification in subclasses. • Non-virtual member function, employing everybody’s best friend… CSE432S - Nick Beary

  15. TEMPLATE METHOD!!! • Write a destroy method that takes care of invariant deletion functions, leave details to primitive operations/hooks. • Primitives in this case include protection checking, warning actions, so forth. CSE432S - Nick Beary

  16. Read Protection • Add Templating to reading operations (i.e. streamOut). CSE432S - Nick Beary

  17. Summary • Strategy Pattern • 1 Context, interchangeable interfaces • Template Method Pattern • Define Algorithm Invariant, leave primitives to subclasses • Single-User Protection • Use Template Method Pattern to apply security protocols, protect you from yourself and prying, ignorant others. CSE432S - Nick Beary

More Related