110 likes | 145 Views
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.
E N D
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
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
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?
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); } }
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
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?
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
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?
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