Download presentation
Presentation is loading. Please wait.
Published bySherman Blair Modified over 9 years ago
1
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Chapter 12 Object-Oriented Design and Patterns 1
2
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
3
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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. 3
4
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Discovering Class Relationships Association Aggregation and Composition Dependency Inheritance 4
5
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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.
6
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
7
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. 7
8
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
9
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
10
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
11
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Coupling Dependency, association, aggregation, and composition all describe coupling relationships between two classes. 11
12
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. 12
13
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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 {... }
14
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Inheritance Inheritance models the is-an-extension-of relationship between two classes. 14
15
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
16
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
17
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Borrowing Loans 17 Name BorrowerPersonLoanAddress
18
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Borrowing Loans, cont. The following is a test program that uses the classes Name, Person, Address, Borrower, and Loan. 18 BorrowLoan Run
19
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Class Design Guidelines Designing a Single Class. Using Modifiers public, protected, private and static Using Inheritance or Aggregation Using Interfaces or Abstract Classes 19
20
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
21
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Designing a Class, cont. Classes are usually designed for use by many different customers. Cstomization through properties and methods. 21
22
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
23
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Designing a Class, cont. Provide a public no-arg constructor Override ▫equals method ▫toString method defined in the Object class whenever possible. 23
24
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
25
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
26
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
27
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Using the static Modifier A property that is shared by all the instances of the class should be declared as a static property. 27
28
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
29
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.) 29
30
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Using Inheritance or Composition, cont. public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ } 30
31
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
32
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
33
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
34
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
35
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
36
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
37
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Framework-Based Programming, cont. 37
38
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.