design pattern aside strategy design pattern (aka...

3
Com psci 290.3/Mobile, Spr ing 2017 14-f inal-design-patter ns-2017.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.e du/ ne ws/threa ds/2008/fall.pdf Com psci 290.3/Mobile, Spr ing 2017 14-f inal-design-patter ns-2017.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/ wi ki/Strategy_pattern Com psci 290.3/Mobile, Spr ing 2017 14-f inal-design-patter ns-2017.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? Com psci 290.3/Mobile, Spr ing 2017 14-f inal-design-patter ns-2017.4 What about sorting in java.util public class Sorter { public void sort(ArrayList<String> list, Comparator<String> comp) { Collections.sort( list); Collections.sort( list,Comparator.reverseOrder()); Collections.sort( list,comp ); } }

Upload: others

Post on 30-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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.e du/ ne ws/threa ds/2008/fall.pdf

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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?

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.4

What about sorting in java.util

public class Sorter {

public void sort(ArrayList<String> list, Comparator<String> comp) {

Collections.sort(list);Collections.sort(list,Comparator.reverseOrder());Collections.sort(list,comp);

}}

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.6

Prerequisites for Compsci 101

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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?

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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(Rounde d( ...) but ...

● TANSTAAFLØ How do you make choices when designing?

Compsci 290.3/Mobile, Spring 201714-f inal-design-patterns-2017.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