31
COMP 121 COMP 121 Week 02 Week 02

COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Embed Size (px)

Citation preview

Page 1: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

COMP 121COMP 121

Week 02Week 02

Page 2: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

AgendaAgenda

• Review this week’s expected Review this week’s expected outcomesoutcomes

• Review Guided Learning Activity Review Guided Learning Activity solutionssolutions

• Introduce homework problemsIntroduce homework problems• Question and answer sessionQuestion and answer session

Page 3: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

OutcomesOutcomes

• Explain how interfaces reduce coupling Explain how interfaces reduce coupling while increasing code reuse.while increasing code reuse.

• Define polymorphism and late binding.Define polymorphism and late binding.• Write generic algorithms that use Write generic algorithms that use

polymorphism to act on covariant data polymorphism to act on covariant data types.types.

• Explain what design patterns are and Explain what design patterns are and how they are used.how they are used.

• Recognize and apply the Strategy design Recognize and apply the Strategy design pattern to solve a given problem.pattern to solve a given problem.

Page 4: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InterfacesInterfaces

• The set of ways to interact with The set of ways to interact with somethingsomething

• A contract between two entitiesA contract between two entities• Hide unnecessary detailsHide unnecessary details

Page 5: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InterfacesInterfaces• Contain only method signatures and Contain only method signatures and

constantsconstants• Cannot be instantiatedCannot be instantiated• Cannot contain instance variablesCannot contain instance variables• Specify what methods must be supported Specify what methods must be supported

by the classes that by the classes that implementimplement the the interfaceinterface

• Implicitly define their methods as public Implicitly define their methods as public and abstractand abstract• The body of the method is not filled inThe body of the method is not filled in• The body or implementation of the method is The body or implementation of the method is

done in the classes that implement the done in the classes that implement the interfaceinterface

Page 6: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InterfacesInterfaces

• Advantages:Advantages:• Reduce couplingReduce coupling• Allow contracts to be enforced by Allow contracts to be enforced by

the compilerthe compiler• Make code more general and Make code more general and

reusablereusable

Page 7: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InterfacesInterfacespublic interface Predator {public interface Predator {

boolean pursuePrey(Prey p);boolean pursuePrey(Prey p);

void devourPrey(Prey p);void devourPrey(Prey p);

}}

public class Lion implements Predator {public class Lion implements Predator {

boolean pursuePrey(Prey p) { … }boolean pursuePrey(Prey p) { … }

void devourPrey(Prey p) { … }void devourPrey(Prey p) { … }

void roar() { … }void roar() { … }

}}

Page 8: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

PolymorphismPolymorphismLion lion = new Lion();Lion lion = new Lion();

lion.pursuePrey(prey);lion.pursuePrey(prey);

Predator p = lion; // Okay – all lions are predatorsPredator p = lion; // Okay – all lions are predators

p.pursuePrey(prey);p.pursuePrey(prey);

p.roar(); // Illegalp.roar(); // Illegal

Lion lion2 = p; // Illegal – not all predators are lionsLion lion2 = p; // Illegal – not all predators are lions

Lion lion2 = (Lion)p;Lion lion2 = (Lion)p;

Page 9: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

PolymorphismPolymorphismPredator [] predators = new Predator[10];Predator [] predators = new Predator[10];

predators[0] = new Lion();predators[0] = new Lion();

predators[1] = new Tiger();predators[1] = new Tiger();

for(Predator p : predators) {for(Predator p : predators) {

p.devourPrey(prey);p.devourPrey(prey);

}}

Page 10: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Guided LearningGuided Learning

Activity Solutions for Week 02Activity Solutions for Week 02

Page 11: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 2-1Activity 2-1

Outcome: Explain how Outcome: Explain how interfaces reduce coupling interfaces reduce coupling while increasing code reuse.while increasing code reuse.

Page 12: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 2-2Activity 2-2

Outcome: Define polymorphism Outcome: Define polymorphism and late binding.and late binding.

Q: Is polymorphism like Q: Is polymorphism like overloadingoverloading? In what way? In ? In what way? In what way is this principle different what way is this principle different from overloading?from overloading?

Page 13: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

A: Both overloading and A: Both overloading and polymorphism involve two methods polymorphism involve two methods with the same name. Overloading with the same name. Overloading is handled at compile time (early is handled at compile time (early binding), and polymorphism is binding), and polymorphism is handled at runtime (late binding).handled at runtime (late binding).

Q: What is early binding? What is late Q: What is early binding? What is late binding?binding?

Page 14: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

A: Early binding is when a call to A: Early binding is when a call to a method can be resolved at a method can be resolved at compile time. Late binding is compile time. Late binding is when a call can’t be resolved when a call can’t be resolved until the call is actually being until the call is actually being made at runtime.made at runtime.

Page 15: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Motivation behind Design Motivation behind Design PatternsPatterns

• Designing object-oriented software is Designing object-oriented software is hardhard

• Designing reusable object-oriented Designing reusable object-oriented software is even hardersoftware is even harder

• Experienced object-oriented designers Experienced object-oriented designers develop good designsdevelop good designs• Don’t solve every problem from first Don’t solve every problem from first

principlesprinciples• Reuse solutions that have worked in the pastReuse solutions that have worked in the past

Page 16: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Design PatternDesign Pattern• Simple yet elegant solution to a specific Simple yet elegant solution to a specific

problem in object-oriented designproblem in object-oriented design• Based on proven object-oriented Based on proven object-oriented

experienceexperience• Distills design experienceDistills design experience• Abstracts a recurring design structureAbstracts a recurring design structure• Comprises class and/or object dependencies, Comprises class and/or object dependencies,

structures, interactions, and conventionsstructures, interactions, and conventions• Aren’t Aren’t inventedinvented, but , but discovereddiscovered• Names and specifies the design structure Names and specifies the design structure

explicitlyexplicitly

Page 17: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Strategy PatternStrategy Pattern

• Defines a family of algorithms, Defines a family of algorithms, encapsulate each one, and encapsulate each one, and make them interchangeablemake them interchangeable

• Strategy lets the algorithm vary Strategy lets the algorithm vary independently from the code independently from the code that uses itthat uses it

Page 18: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Strategy PatternStrategy Pattern

• Duck BehaviorDuck Behavior• Classes can be supplied for each Classes can be supplied for each

FlyBehavior and QuackBehaviorFlyBehavior and QuackBehavior• Avoids the explosion of classes you Avoids the explosion of classes you

would get by using inheritancewould get by using inheritance• The behaviors can even be changed at The behaviors can even be changed at

runtimeruntime

Page 19: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Template Method PatternTemplate Method Pattern

• Defines a skeleton of an Defines a skeleton of an algorithm, deferring some of the algorithm, deferring some of the steps to interchangeable steps to interchangeable subclassessubclasses

• Lets subclasses redefine certain Lets subclasses redefine certain steps of an algorithm without steps of an algorithm without changing the algorithm’s basic changing the algorithm’s basic structurestructure

Page 20: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Template Method PatternTemplate Method Pattern

Use when:Use when:• Parts of an algorithm do not vary Parts of an algorithm do not vary

while other parts dowhile other parts do• There is common behavior in There is common behavior in

subclasses that can be factoredsubclasses that can be factored• There is a need to limit what can be There is a need to limit what can be

done in subclasses, allowing done in subclasses, allowing extensions only at certain points extensions only at certain points (hooks)(hooks)

Page 21: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Template Method PatternTemplate Method Pattern

• All card games have basically the same All card games have basically the same set of stepsset of steps• Shuffle the cardsShuffle the cards• Deal the cardsDeal the cards• Play the handPlay the hand• Count up the scoreCount up the score

• Some of the steps may be the same for Some of the steps may be the same for all card games (shuffle the cards)all card games (shuffle the cards)

• Others may be different (deal the cards, Others may be different (deal the cards, play the hand, count up the score)play the hand, count up the score)

Page 22: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 2-3Activity 2-3

Outcome: Write generic Outcome: Write generic algorithms that use algorithms that use polymorphism to act on polymorphism to act on covariant data types.covariant data types.

Page 23: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Insertion Sort

13 4 9 6 5

4 13 9 6 5

4 9 13 6 5

4 6 9 13 5

Pass 1:

Pass 2:

Pass 3:

Page 24: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

• iterations

• At -th iteration, comparisons

Insertion Sort Analysis

1

2

11...21

nnn

2n

1n

k 1k

Page 25: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 2-4Activity 2-4

Outcome: Explain what design Outcome: Explain what design patterns are and how they are patterns are and how they are used.used.

Q: How would using design patterns Q: How would using design patterns impact the complexity of your impact the complexity of your solution?solution?

Page 26: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

A: Design patterns should decrease A: Design patterns should decrease coupling and increase cohesion, coupling and increase cohesion, so the complexity of the solution so the complexity of the solution should be reduced.should be reduced.

Q: How would using design patterns Q: How would using design patterns affect the amount of time it takes affect the amount of time it takes to design your solution?to design your solution?

Page 27: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

A: Using design patterns may not have A: Using design patterns may not have a large impact on the time it takes to a large impact on the time it takes to design your solution, but they will design your solution, but they will greatly reduce the time it takes to greatly reduce the time it takes to make changes in the future.make changes in the future.

Q: How does using design patterns Q: How does using design patterns affect the ability to reuse and/or affect the ability to reuse and/or modify a solution?modify a solution?

Page 28: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

A: Using design patterns make a A: Using design patterns make a solution more flexible and solution more flexible and easier to change. Because of easier to change. Because of this, pieces of the solution will this, pieces of the solution will be more reusable.be more reusable.

Page 29: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 2-5Activity 2-5

Outcome: Recognize and apply Outcome: Recognize and apply the Strategy design pattern to the Strategy design pattern to solve a given problem.solve a given problem.

Page 30: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Homework AssignmentsHomework Assignments

• Due next weekDue next week• Homework 1Homework 1

Page 31: COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Question and Answer SessionQuestion and Answer Session