Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided."— Presentation transcript:

1 COMP 121 Week 02

2 Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided Learning Activity solutions Introduce homework problemsIntroduce homework problems Question and answer sessionQuestion and answer session

3 Outcomes Explain how interfaces reduce coupling while increasing code reuse.Explain how interfaces reduce coupling while increasing code reuse. Define polymorphism and late binding.Define polymorphism and late binding. Write generic algorithms that use polymorphism to act on covariant data types.Write generic algorithms that use polymorphism to act on covariant data types. Explain what design patterns are and how they are used.Explain what design patterns are and how they are used. Recognize and apply the Strategy design pattern to solve a given problem.Recognize and apply the Strategy design pattern to solve a given problem.

4 Interfaces The set of ways to interact with somethingThe set of ways to interact with something A contract between two entitiesA contract between two entities Hide unnecessary detailsHide unnecessary details

5 Interfaces Contain only method signatures and constantsContain only method signatures and constants Cannot be instantiatedCannot be instantiated Cannot contain instance variablesCannot contain instance variables Specify what methods must be supported by the classes that implement the interfaceSpecify what methods must be supported by the classes that implement the interface Implicitly define their methods as public and abstractImplicitly define their methods as public and 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 done in the classes that implement the interfaceThe body or implementation of the method is done in the classes that implement the interface

6 Interfaces Advantages:Advantages: Reduce couplingReduce coupling Allow contracts to be enforced by the compilerAllow contracts to be enforced by the compiler Make code more general and reusableMake code more general and reusable

7 Interfaces public interface Predator { boolean pursuePrey(Prey p); boolean pursuePrey(Prey p); void devourPrey(Prey p); void devourPrey(Prey p);} 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() { … }}

8 Polymorphism Lion lion = new Lion(); lion.pursuePrey(prey); Predator p = lion; // Okay – all lions are predators p.pursuePrey(prey); p.roar(); // Illegal Lion lion2 = p; // Illegal – not all predators are lions Lion lion2 = (Lion)p;

9 Polymorphism Predator [] predators = new Predator[10]; predators[0] = new Lion(); predators[1] = new Tiger(); for(Predator p : predators) { p.devourPrey(prey); p.devourPrey(prey);}

10 Guided Learning Activity Solutions for Week 02

11 Learning Activities Activity 2-1 Outcome: Explain how interfaces reduce coupling while increasing code reuse.

12 Learning Activities Activity 2-2 Outcome: Define polymorphism and late binding. Q: Is polymorphism like overloading? In what way? In what way is this principle different from overloading?

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

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

15 Motivation behind Design Patterns Designing object-oriented software is hardDesigning object-oriented software is hard Designing reusable object-oriented software is even harderDesigning reusable object-oriented software is even harder Experienced object-oriented designers develop good designsExperienced object-oriented designers develop good designs Don’t solve every problem from first principlesDon’t solve every problem from first principles Reuse solutions that have worked in the pastReuse solutions that have worked in the past

16 Design Pattern Simple yet elegant solution to a specific problem in object-oriented designSimple yet elegant solution to a specific problem in object-oriented design Based on proven object-oriented experienceBased on proven object-oriented experience Distills design experienceDistills design experience Abstracts a recurring design structureAbstracts a recurring design structure Comprises class and/or object dependencies, structures, interactions, and conventionsComprises class and/or object dependencies, structures, interactions, and conventions Aren’t invented, but discoveredAren’t invented, but discovered Names and specifies the design structure explicitlyNames and specifies the design structure explicitly

17 Strategy Pattern Defines a family of algorithms, encapsulate each one, and make them interchangeableDefines a family of algorithms, encapsulate each one, and make them interchangeable Strategy lets the algorithm vary independently from the code that uses itStrategy lets the algorithm vary independently from the code that uses it

18 Strategy Pattern Duck BehaviorDuck Behavior Classes can be supplied for each FlyBehavior and QuackBehaviorClasses can be supplied for each FlyBehavior and QuackBehavior Avoids the explosion of classes you would get by using inheritanceAvoids the explosion of classes you would get by using inheritance The behaviors can even be changed at runtimeThe behaviors can even be changed at runtime

19 Template Method Pattern Defines a skeleton of an algorithm, deferring some of the steps to interchangeable subclassesDefines a skeleton of an algorithm, deferring some of the steps to interchangeable subclasses Lets subclasses redefine certain steps of an algorithm without changing the algorithm’s basic structureLets subclasses redefine certain steps of an algorithm without changing the algorithm’s basic structure

20 Template Method Pattern Use when: Parts of an algorithm do not vary while other parts doParts of an algorithm do not vary while other parts do There is common behavior in subclasses that can be factoredThere is common behavior in subclasses that can be factored There is a need to limit what can be done in subclasses, allowing extensions only at certain points (hooks)There is a need to limit what can be done in subclasses, allowing extensions only at certain points (hooks)

21 Template Method Pattern All card games have basically the same set of stepsAll card games have basically the same set 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 all card games (shuffle the cards)Some of the steps may be the same for all card games (shuffle the cards) Others may be different (deal the cards, play the hand, count up the score)Others may be different (deal the cards, play the hand, count up the score)

22 Learning Activities Activity 2-3 Outcome: Write generic algorithms that use polymorphism to act on covariant data types.

23 Insertion Sort 1349654 96549 65469 5 Pass 1: Pass 2: Pass 3:

24 iterations At -th iteration, comparisons Insertion Sort Analysis

25 Learning Activities Activity 2-4 Outcome: Explain what design patterns are and how they are used. Q: How would using design patterns impact the complexity of your solution?

26 Learning Activities A: Design patterns should decrease coupling and increase cohesion, so the complexity of the solution should be reduced. Q: How would using design patterns affect the amount of time it takes to design your solution?

27 Learning Activities A: Using design patterns may not have a large impact on the time it takes to design your solution, but they will greatly reduce the time it takes to make changes in the future. Q: How does using design patterns affect the ability to reuse and/or modify a solution?

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

29 Learning Activities Activity 2-5 Outcome: Recognize and apply the Strategy design pattern to solve a given problem.

30 Homework Assignments Due next weekDue next week Homework 1Homework 1

31 Question and Answer Session


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

Similar presentations


Ads by Google