Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Chapter 12 Object-Oriented Design.

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
Chapter 10 Classes Continued
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
Inheritance using Java
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
CHAPTER 13 (ONLINE): OBJECT-ORIENTED DATA MODELING © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 13 (Online): Object-Oriented Data Modeling Modern Database Management 10 th Edition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
PROG Object Oriented Programming II With Java PROG Object Oriented Programming II With Java Class Relationships.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Coming up: Inheritance
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
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.
ITEC0724 Modern Related Technology on Mobile Devices Lecture Notes #2 1.
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCE 240 – Intro to Software Engineering Lecture 3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
OOP: Encapsulation &Abstraction
Chapter 10 Thinking in Objects
Reference: COS240 Syllabus
Chapter 10 Thinking in Objects
Chapter 11 Object-Oriented Design
Chapter 10 Object-Oriented Modeling
Reference: COS240 Syllabus
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Thinking in Objects
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 9 Thinking in Objects
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 10 Thinking in Objects
Presentation transcript:

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design and Patterns 1

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Objectives To become familiar with the process of program development (§12.2). To learn the relationship types: association, aggregation, composition, dependency, strong inheritance, and weak inheritance (§12.3). To discover classes and determine responsibilities of each classes (§12.3). To declare classes to represent the relationships among them (§12.4). To design systems by identifying the classes and discovering the relationships among these classes (§12.5). To implement the Rational class and process rational numbers using this class (§12.6). To design classes that follow the class-design guidelines (§12.7). To know the concept of framework-based programming using the Java API (§12.8). To introduce design patterns for developing sound software systems (§12.9). 2

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Discovering Classes One popular way for facilitating the discovery process is by creating CRC cards. CRC stands for classes, responsibilities, and collaborators. Use an index card for each class, as shown in Figure

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Discovering Class Relationships Association Aggregation and Composition Dependency Inheritance 4

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Association Association represents a general binary relationship that describes an activity between two classes. 5 An association is usually represented as a data field in the class.

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Translation is not Unique NOTE: If you don’t need to know the courses a student takes or a faculty teaches, the data field coureList in Student or Faculty can be omitted. 6

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Association Between Same Class Association may exist between objects of the same class. For example, a person may have a supervisor. 7

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Aggregation and Composition  Aggregation: special form of association  Represents ownership relationship  Aggregation models the has-a relationship.  Composition: special form of aggregation  object exclusively owned by aggregated object 8

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Dependency Dependency: relationship between two classes where one (called client) uses the other (called supplier). In UML, draw a dashed line with an arrow from the client class to the supplier class. 9

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Dependency vs. Association Association is stronger than dependency. In association, the state of the object changes when its associated object changes. In dependency, the client object and the supplier object are loosely coupled. Association is implemented using data fields and methods. There is a strong connection between two classes. Dependency is implemented using methods. 10

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Coupling Dependency, association, aggregation, and composition all describe coupling relationships between two classes. 11

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Representing Aggregation in Classes An aggregation relationship is usually represented as a data field in the aggregated class. 12

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Inner Classes Translation If Name or Address is used in the Person class only, they can be declared as an inner class in Person. For example, 13 public class Person { private Name name; private Address address;... class Name {... } class Address {... }

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Inheritance Inheritance models the is-an-extension-of relationship between two classes. 14

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Weak Inheritance Relationship A weak is-an-extension-of relationship can be represented using interfaces. For example, the weak is-an-extension-of relationship “students are comparable based on their grades” can be represented by implementing the Comparable interface, as follows: 15

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Class Design 1. Identify classes for the system. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 4. Create classes. 16

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Borrowing Loans 17 Name BorrowerPersonLoanAddress

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Borrowing Loans, cont. The following is a test program that uses the classes Name, Person, Address, Borrower, and Loan. 18 BorrowLoan Run

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Class Design Guidelines Designing a Single Class. Using Modifiers public, protected, private and static Using Inheritance or Aggregation Using Interfaces or Abstract Classes 19

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Designing a Class A class should describe a single entity or a set of similar operations. A single entity with too many responsibilities can be broken into several classes to separate responsibilities. The String class, StringBuffer class, and StringTokenizer class all deal with strings, for example, but have different responsibilities. 20

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Designing a Class, cont. Classes are usually designed for use by many different customers. Cstomization through properties and methods. 21

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Designing a Class, cont. Classes are designed for reuse. Design a class that imposes no restrictions on what or when the user can do with it. Design the properties to ensure that the user can set properties in any order, with any combination of values. Design methods to function independently of their order of occurrence. 22

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Designing a Class, cont. Provide a public no-arg constructor Override ▫equals method ▫toString method defined in the Object class whenever possible. 23

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Designing a Class, cont. Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. Always provide a constructor and initialize variables to avoid programming errors. 24

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Visibility Modifiers Each class can present two contracts – one for the users of the class and one for the extenders of the class. Make the fields private and accessor methods public if they are intended for the users of the class. Make the fields or methods protected if they are intended for extenders of the class. The extended class may increase the visibility of an instance method from protected to public, or change its implementation. Do not change the implementation in a way that violates that contract. 25

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Visibility Modifiers, cont. A class should use the private modifier to hide its data from direct access by clients. get methods and set methods provide access to private data that should be visible or modifiable. Hide methods not intended for client use. (i.e., gcd method in the Rational class) 26

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using the static Modifier A property that is shared by all the instances of the class should be declared as a static property. 27

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation Difference: is-an-extension-of relationship vs. has-a relationship Examples: an apple is fruit (inheritance) a person has a name (aggregation) 28

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation, cont. Sometimes, the choice between inheritance and aggregation is not obvious. Example: inheritance to model the relationship between the classes Circle and Cylinder. (One could argue that a cylinder consists of circles and use aggregation.) 29

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Inheritance or Composition, cont. public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ } 30

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation, cont. Which design is preferred? For polymorphism - use inheritance. Otherwise, aggregation gives more flexibility. Classes are less dependent using aggregation than using inheritance. (Looser coupling) 31

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Interfaces or Abstract Classes Interfaces and abstract classes generalize common features. How to choose? A strong is-an-extension-of relationship  clearly describes a parent-child relationship  should be modeled using classes. A weak is-an-extension-of relationship  an is-kind-of relationship  indicates possession of a property  can be modeled using interfaces. 32

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Using Interfaces or Abstract Classes, cont. Interfaces are more flexible than abstract classes - a subclass can extend only one superclass, but implement any number of interfaces. Interfaces cannot contain concrete methods. Combine the virtues: create an interface with a companion abstract class implementation. 33

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved The Java API The Java API (Application Program Interface, Application Programming Interface, or Application Programmer interface) consists of numerous classes and interfaces grouped into more than a dozen of packages. 34

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Framework-Based Programming  To create comprehensive projects, use more classes and interfaces in the Java API.  The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java.  For example, the classes and interfaces in the Java GUI API establish a framework for developing GUI programs.  Use these classes and interfaces and follow their conventions and rules to create applications. This is referred to as framework-based programming. 35

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Framework-Based Programming, cont. Once you understand the concept of Java and object-orient programming, the most important lesson is learning how to use the API to develop useful programs. Most effective way to achieve it: imitate good examples. 36

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Framework-Based Programming, cont. 37

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Design Patterns One important benefit of object-oriented programming is to reuse code. Design patterns are proven sound software strategies for designing classes. Applying design patterns is like reusing experience. You can apply successful patterns to develop new software without reinventing new solution strategies. 38