CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Advertisements

James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 325: Software Engineering February 12, 2015 Applying Responsibility-Assignment Patterns Design Patterns Situation-Specific Patterns Responsibility-Assignment.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CS 46B: Introduction to Data Structures June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160: Software Engineering December 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Advanced Object-Oriented Design Patterns and Architectures Part One COEN396A John Xiao
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
CS 160 and CMPE/SE 131 Software Engineering March 24 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Introduction To Design Patterns
Unit II-Chapter No. : 5- design Patterns
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
CS 153: Concepts of Compiler Design August 29 Class Meeting
Interfaces.
Design Patterns Introduction
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
CMPE/SE 131 Software Engineering March 23 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Software Design Lecture : 27.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 2 Project Team Design Reviews  Monday, October 27 Section 3  Code Monsters  160 Zaibatsu  Tin Bullet Section 4  Quiet Coders  Surprise Error  Dream Team  Wednesday, October 29 Section 3  List Ninja  Merge Monkeys  Noisy Coders Section 4  Activate  League of Gentlemen  Not Applicable

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 3 Project Team Design Reviews, cont’d  Present and defend key aspects of your design. 20 minutes per team.  Use PowerPoint slides Turn in your slides after your presentation.  Briefly discuss your overall design.  Show and explain the design of your most important or core classes. UML class diagrams and/or sequence diagrams _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 4 Project Team Design Reviews, cont’d  Briefly discuss your implementation plan.  Questions and answers at the end.  Audience: Very technical Other development engineers. Technical managers, stakeholders, financial backers, etc. Expect “deep” questions from the audience! _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak A Useful Combination of Techniques  Interfaces  Coding to the interface  Polymorphism  Design patterns strategy design pattern factory method design pattern  Dynamic class loading 5

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Interfaces and Classes  A Java class defines: an interface  a set of operations (methods) an implementation  statements that specify how to carry out the operations  statements that specify how to represent object state  It can be useful to separate an interface from its implementation. 6

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Java Interface  A Java interface type Defines a set of operations (methods). Does not define their implementations.  A Java class can implement an interface. The class must implement (supply the statements for) each of the interface’s methods.  A Java class can implement multiple interfaces. The class must implement all the methods of the interfaces that it implements. 7

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak The Interface as a Contract  A Java interface can be implemented by multiple classes.  Each class can implement an interface method in a different but related way.  An interface is a contract. Any class that implements the interface is guaranteed to implement each and every one of the interface methods. 8

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Interface Example  Interface Shape defines the draw() method.  Classes Rectangle, Triangle, and Circle each implements interface Shape. Each class must therefore implement the draw() method.  Each class implements draw() in a different but related way. Each class’s draw() method draws the shape. But each shape is drawn differently. 9

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 10 A Class Hierarchy Puzzle Animal LionDogPiranhaGoldfishParrotHummingbird MammalFishBird  We want to add the category HouseholdPet. Do we make it a superclass? Where does it belong in this class hierarchy?  How do we also add the category Biter ?

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 11 A Class Hierarchy Puzzle  Make HouseholdPet and Biter Java interfaces. A Java class can implement multiple interfaces. Animal MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 12 Java Subclass  If a class C is a subclass of superclass S, then C “is a” S: Animal MammalFishBird LionDogPiranhaGoldfishParrotHummingbird Dog is an Animal. Dog is a Mammal.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 13 Java Interface  If a class C implements interface N, then C “is a” N: Animal MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter Dog is a HouseholdPet. Dog is a Biter.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 14 Java instanceof Operator  If the value of variable x is of type Dog, then the following conditionals are all true: x instanceof Dog x instanceof Mammal x instanceof Animal x instanceof HouseholdPet x instanceof Biter _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Polymorphism  A variable can have an interface type. Example:  At run time, variable sh can have any object as a value that was instantiated from a class that implements the Shape interface. A call to sh.draw() will call the rectangle, triangle, or circle draw method, depending on the value of sh.  Polymorphism: The ability to determine automatically at run time which method to call. 15 Shape sh;

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Polymorphism, cont’d  A variable can have an interface type.  A value cannot have an interface type.  A value can be an object instantiated from a class that implements an interface. Or a value can be a scalar. 16

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Coding to the Interface  Compare: Are there advantages of one vs. the other?  The first declaration allows variable sh to be assigned only a Rectangle object.  The second declaration allows sh to be assigned a Rectangle, Triangle, or Circle object. Or any object instantiated from a class that implements interface Shape. This more flexible style is coding to the interface. 17 Shape sh; Rectangle sh;

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 18 Design Patterns  A design pattern is  A description of a problem. A solution that you can apply to many programming situations.  Design patterns show you how to build good software with good object-oriented design qualities.  Design patterns are proven object-oriented experience.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 19 Design Patterns, cont’d  Design patterns are not code, but are general solutions to design problems. You apply them to your specific application.  Design patterns are not invented – they’re discovered.  Design patterns address how to manage change. _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 20 Design Patterns, cont’d  Design patterns give programmers a very high-level, short-cut vocabulary to discuss design issues. Independent of specific implementations or programming languages. “We should use the factory method design pattern here.” “The decorator pattern will simplify this code.”

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 21 Design Patterns, cont’d  Each design pattern has A short name A brief description of the context A description of the problem that it solves A prescription for a solution _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 22 Design Patterns, cont’d  Building architect Christopher Alexander discovered over 250 patterns for architectural design. Co-authored A Pattern Language: Towns, Buildings, Construction, published in  In 1995, four authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (AKA “The Gang of Four”) published the classic software book Design Patterns. Original 23 design patterns

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 23 The Strategy Design Pattern  Context There are different algorithms (“strategies”) to solve a particular problem.  Description The algorithms all have similar public interfaces, but each solves the problem in a different way.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 24 The Strategy Design Pattern, cont’d  Solution Create a strategy interface that is an abstraction of the algorithm. Declare the interface shared by the algorithms. Code each strategy in a class that implements the strategy interface. At run time, select one of the strategies and call its interface methods. _

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak The Strategy Design Pattern, cont’d 25 > Shape draw() : void Triangle draw() : void Rectangle draw() : void Circle draw() : void Strategy interface Strategy

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 26 The Factory Method Design Pattern  Context An application can instantiate any one of several classes that implement an interface.  Description You know that your application needs to instantiate one of the classes.  But you won’t know which class until run time. You need to provide a means to instantiate the class as determined by the application at run time.  Therefore, your code must have the flexibility to instantiate and work with any of the classes.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 27 The Factory Method Design Pattern  Solution Design a factory method that will, based on its parameters, create and return an object that is instantiated from the correct class.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 28 Factory Method Example public class GreeterFactory1 { public static Greeter make(String language) throws Exception { if (language.equals("English")) { return new English(); } else if (language.equals("French")) { return new French(); } else { throw new Exception(); } > Greeter EnglishFrench public interface Greeter { String greet(); } public class English implements Greeter { public String greet() { return "Hello!"; } Demo

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 29 Factory Method Example, cont’d public class GreeterFactory1 { public static Greeter make(String language) throws Exception { if (language.equals("English")) { return new English(); } else if (language.equals("French")) { return new French(); } else { throw new Exception(); }  How can we make this factory method even more flexible? Hardcoded to work only with English and French.

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Dynamic Class Loading  Suppose that when we write the factory method, we don’t know which languages are available.  At run time, the factory method can dynamically load a language class if it is given the name of the language: where language is a string containing the name of a language. 30 Class.forName(language)

Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Dynamic Class Loading, cont’d 31 public class GreeterFactory2 { public static Greeter make(String language) throws ClassNotFoundException, InstantiationException, IllegalAccessException { return (Greeter) Class.forName(language).newInstance(); } Dynamically load a language class, and instantiate and return a language object. Demo