1 / 10

Design Pattern Aside

Design Pattern Aside. Why you should read extensively and often What are the downsides? What are the upsides? TANSFAAFL How to understand what you're reading? Practice what you're reading about Read more … http://www.cs.duke.edu/news/threads/2008/fall.pdf.

jreader
Télécharger la présentation

Design Pattern Aside

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. Design Pattern Aside • Why you should read extensively and often • What are the downsides? • What are the upsides? • TANSFAAFL • How to understand what you're reading? • Practice what you're reading about • Read more • … http://www.cs.duke.edu/news/threads/2008/fall.pdf

  2. Strategy Design Pattern (aka Policy) • Algorithm varies independently of client that uses algorithm • Need context to use algorithm, these may be coupled • Quiz taking "algorithms" we've discussed • Should there be sub-classes of quiz for … • Buzzfeed quizzes or Date/Matching "quiz" • Multiple correct vs. Single correct https://en.wikipedia.org/wiki/Strategy_pattern

  3. Context and Strategy (from GOF) • Context has-a strategy • Clients interact with the Context, not Strategy • Context uses strategy • Quiz compared to QuizTaker • What's the difference? What about whether questions should be shuffled? Adaptable question generation?

  4. What about sorting in java.util publicclass Sorter { publicvoid sort(ArrayList<String> list, Comparator<String> comp) { Collections.sort(list); Collections.sort(list,Comparator.reverseOrder()); Collections.sort(list,comp); } }

  5. Strategy Benefits • Open/Closed Principle • Program is open to extension without being modified • How to create/use .zip, or .tgz, or .rar • What are the differences here? What about adding new compression methods? • How to use encryption/cipher algorithms? • https://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/CryptoSpec.html#Cipher

  6. Prerequisites for Compsci 101

  7. Decorator and Strategy in Java • First let's look at some examples • http://www.programcreek.com/java-api-examples/index.php?api=javax.crypto.CipherInputStream • Let's look at what's common across several examples • How to create CipherInputStream? • How to use the InputStream created?

  8. Decorator Pattern • Add functionality to objects • Configure input stream at runtime • Rectangle, BorderedRect, ShadowedRect, … • The Decorator typically is-a and has-a • Is an input stream, has an input stream • Forwards requests to the has-a object • Behaves like because also is-a https://en.wikipedia.org/wiki/Decorator_pattern

  9. Decorator Pattern • Typically is a light-weight class, not too many "guts" • In latter case, prefer Strategy pattern! • If you need to add many "adornments", e.g., borders, shading, rounded, etc. • Perhaps the borders/adornments are a Strategy for drawing, not Bordered(Rounded(...) but ... • TANSTAAFL • How do you make choices when designing?

  10. From GOF book on Decorator • Functionality composed of simple pieces, don't need to anticipate everything • Think buffered readers, cipher input streams • Lots of little objects that all look alike • Easy to use if you understand how to create and compose, but hard to learn and debug There are always trade-offs. Understanding options is harder than knowing only one way

More Related