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

Slides:



Advertisements
Similar presentations
COMP 121 Week 2: Interfaces and Polymorphism. Objectives To learn about interfaces To be able to convert between class and interface references To understand.
Advertisements

Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Software Engineering and Design Principles Chapter 1.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented Programming Concepts
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
DAAD project “Joint Course on OOP using Java” Design Patterns in the course ‘OOP in Java’ - first experiences Ana Madevska Bogdanova Institute of informatics.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Design Patterns OOD. Course topics Design Principles UML –Class Diagrams –Sequence Diagrams Design Patterns C#,.NET (all the course examples) Design Principles.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Case Studies on Design Patterns Design Refinements Examples.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Review of Parnas’ Criteria for Decomposing Systems into Modules Zheng Wang, Yuan Zhang Michigan State University 04/19/2002.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns: MORE Examples
Object-Oriented Design
Chapter 10 Design Patterns.
Introduction to Design Patterns
Behavioral Design Patterns
Software Design and Architecture
OOP What is problem? Solution? OOP
object oriented Principles of software design
Strategy Design Pattern
Object Oriented Analysis and Design
Presentation transcript:

COMP 121 Week 02

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

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.

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

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

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

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() { … }}

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;

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

Guided Learning Activity Solutions for Week 02

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

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?

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?

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.

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

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

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

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

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

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)

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)

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

Insertion Sort Pass 1: Pass 2: Pass 3:

iterations At -th iteration, comparisons Insertion Sort Analysis

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?

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?

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?

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.

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

Homework Assignments Due next weekDue next week Homework 1Homework 1

Question and Answer Session