Chapter 12
Short Description
Download Chapter 12...
Description
1
Chapter 12 Object-Oriented Design and Patterns
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Objectives
2
• 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).
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
3
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 12.2. Class name
Student Register a course
Course
Responsibilities
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Collaborators
4
Discovering Class Relationships • Association • Aggregation and Composition
• Dependency • Inheritance
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
5
Association Association represents a general binary relationship that describes an activity between two classes.
Student
5..60
public class Student { /** Data fields */ private Course[] courseList;
Take
* Course
0..3
public class Course { /** Data fields */ private Student[] classList; private Faculty faculty;
/** Constructors */ /** Methods */
/** Constructors */ /** Methods */
}
Teach
1 Teacher
Faculty
public class Faculty { /** Data fields */ private Course[] courseList; /** Constructors */ /** Methods */ }
}
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. 0136012671
Translation is not Unique
6
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Association Between Same Class Association may exist between objects of the same class. For example, a person may have a supervisor.
1
Person Supervisor 1
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
7
8
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
Composition
Name
Aggregation
Person
Address
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
9
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.
ArrayList
Object
Calendar
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Date
Dependency vs. Association
10
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
11
Coupling Dependency, association, aggregation, and composition all describe coupling relationships between two classes.
coupling increases dependency, association, aggregation, composition
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Representing Aggregation in Classes An aggregation relationship is usually represented as a data field in the aggregated class. public class Name { /** Data fields */ /** Constructors */ /** Methods */ }
public class Person { /** Data fields */ private Name name; private Address address;
public class Address { /** Data fields */ /** Constructors */ /** Methods */ }
/** Constructors */ /** Methods */ }
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
12
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, 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. 0136012671
13
14
Inheritance Inheritance models the is-an-extension-of relationship between two classes. public class Faculty extends Person { Person
/** Data fields */ /** Constructors */ /** Methods */
Faculty } (A)
(B)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
15
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: public class Student extends Person implements Comparable { Person
/** Data fields, Constructors, and */ /** Methods */
Student Comparable
/** Implement the compareTo method */ public int compareTo(Object object) { // ... } } (A)
(B)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
16
Class Design 1. Identify classes for the system.
2. Describe attributes and methods in
each class. 3. Establish relationships among classes.
4. Create classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
17
Borrowing Loans Name
Person
-firstName: String -mi: char -lastName: String
-name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +seName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String
+Name() +Name(firstName: String, mi: char, lastName: String) +getFirstName(): String +getMi(): char +getLastName(): String +setFirstName(firstName: String): void +setMi(mi: char): void +setLastName(lastName: String): void +getFullName(): String
Borrower -loan: Loan
Loan Defined in Example 6.7
Name
Address -street: String -city: String -state: String -zip: String +Address() +Address(street: String, city: String, state: String, zip: String) +getStreet(): String +getCity(): String +getState(): String +getZip(): String +setStreet(street: String): void +setCity(city: String): void +setState(state: String): void +setZip(zip: String): void +getFullAddress(): String
+Borrower() +Borrower(name: Name, address: Address) +getLoan(): Loan +setLoan(loan: Loan): void +toString(): String
Loan
Person
Borrower Address
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
18
Borrowing Loans, cont. The following is a test program that uses the classes Name, Person, Address, Borrower, and Loan.
BorrowLoan
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Run
19
Class Design Guidelines • Designing a Single Class. • Using Modifiers public, protected, private and static • Using Inheritance or Aggregation • Using Interfaces or Abstract Classes
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
20
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
21
Designing a Class, cont. • Classes are usually designed for use by many different customers. • Cstomization through properties and methods.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
22
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
23
Designing a Class, cont. • Provide a public no-arg constructor • Override ▫ equals method
▫ toString method defined in the Object class whenever possible.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
24
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
25
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. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
26
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)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
27
Using the static Modifier • A property that is shared by all the instances of the class should be declared as a static property.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
28
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) Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
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.) Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
29
Using Inheritance or Composition, cont. public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ }
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
30
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) Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
31
32
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. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Using Interfaces or Abstract Classes, cont.
33
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. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
34
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. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
35
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
36
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
37
Framework-Based Programming, cont. Package
Description
java.lang
contains core Java classes and interfaces (e.g., System, Math, Object, String, StringBuffer, Number, Character, Boolean, Byte, Short, Integer, Long, Float, Double, Comparable, and Cloneable). This package is implicitly imported to every Java program.
javax.swing
contains the lightweight graphical user interface components (e.g., JOptionPane, JFrame, JButton) for developing Swing GUI programs.
java.util
contains utility classes and interfaces, such as Scanner, Arrays, ArrayList, Date, Calendar, and GregorianCalendar.
java.io
contains classes and interfaces for IO, such as File and PrintWriter.
java.io
contains classes and interfaces for IO, such as File and PrintWriter.
java.math
contains classes BigInteger for performing arbitraryprecision integer arithmetic and BigDecimal for performing arbitrary-precision decimal arithmetic.
java.net
contains classes for networking programming.
java.applet
contains classes for supporting applets.
java.awt
contains classes for supporting GUI programming.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
38
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. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
View more...
Comments