CS 151: Object-Oriented Design October 8 Class Meeting

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Advertisements

Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Creational Patterns (1) CS350, SE310, Fall, 2010.
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
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
CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
CS 160: Software Engineering October 15 Class Meeting
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
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design August 27 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Design Patterns Introduction
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 235: User Interface Design March 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Sections Inheritance and Abstract Classes
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
Chapter 11: Inheritance and Polymorphism
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
Section 11.1 Class Variables and Methods
UML Class Diagram: class Rectangle
Interfaces and Inheritance
CMPE 152: Compiler Design August 23 Class Meeting
CS 350 – Software Design Principles and Strategies – Chapter 14
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Inheritance and Polymorphism
CMPE 135: Object-Oriented Analysis and Design March 19 Class Meeting
CS 144 Advanced C++ Programming January 31 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
CS 144 Advanced C++ Programming March 28 Class Meeting
Presentation transcript:

CS 151: Object-Oriented Design October 8 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

The Java Interface as a Contract public interface HouseholdPet { void feed(Food f); } Any class that implements an interface is guaranteed to implement each and every one of the interface methods. public interface ActionListener { void actionPerformed(ActionEvent event); } public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); } SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

Review: 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. 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. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

Review: The Factory Method Design Pattern Context An application can instantiate any one of several subclasses of a superclass (or any one of several implementers of an interface). Description You know that your application needs to instantiate one of the subclasses. But you won’t know which subclass until run time. You need to provide a means to instantiate the subclass as determined by the application at run time. Your code must have the flexibility to instantiate and work with any of the subclasses. Solution Design a factory method that will, based on its parameters, create and return an object of the correct subclass type. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

Review: Factory Method Example ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow ThrowCalculator calc = ThrowCalculator.makeCalculator(type, ...); public abstract class ThrowCalculator { public static ThrowCalculator makeCalculator(int type, ...) switch (type) { case RANDOM: return new RandomThrow(...); break; case SMART: return new SmartThrow(...); break; case GENIUS: return new GeniusThrow(...); break; } public abstract Throw calculateThrow(...); // abstract method SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

Review: Coding to the Interface ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow ThrowCalculator calc = ThrowCalculator.makeCalculator(type, ...); Throw t = calc.calculateThrow(...); Variable calc has the interface type ThrowCalculator. It refers to an object that can be a RandomThrow, a SmartThrow, or a GeniusThrow. An object of one of those types is returned by the factory. Variable t = calc.calculateThrow(...) doesn’t care which type of object calc refers to. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

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. Solution Create a strategy interface that is an abstraction of the algorithm. Declare the public 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 public interface methods. _ SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

The Strategy Design Pattern We’ve already seen examples of this pattern: Calendar Gregorian Lunar ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow Recall the Comparator interface used by the Collections class to sort an array list of objects. How can you apply the strategy design pattern? You can have several classes that implement the Comparator interface all with different compare() methods. Compare by weight, height, etc. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

A Powerful Technique for Flexibility To achieve maximum code flexibility, combine The strategy design pattern The factory method design pattern with dynamic class loading Coding to the interface The strategy design pattern tells us how to handle multiple algorithms to solve a problem. The factory method design pattern tells us how to instantiate an algorithm at run time. Coding to the interface ensures we don’t hard-code to any one algorithm. Demo SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak

NetBeans Swing Tutorials Introduction to GUI Building http://netbeans.org/kb/docs/java/gui-functionality.html Designing a Swing GUI in NetBeans IDE http://netbeans.org/kb/docs/java/quickstart-gui.html NetBeans Swing design demo SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak