ObjectOriented Modeling 1 Topics n n n n
Object-Oriented Modeling 1
Topics n n n n System life cycle. Association, aggregation, composition, strong inheritance, and weak inheritance. Classes to represent the relationships among the classes. Design systems by identifying the classes and discovering the relationships among these classes. Rational class and process rational numbers using this class. Design classes that follow the class-design guidelines. Model dynamic behavior using sequence diagrams and statechart diagrams. Framework-based programming using Java API. 2
Software Development Process 3
Requirement Specification Understand the problem and document what the software system needs to do. Close interaction between users and designers. 4
System Analysis Analyze the business process in terms of data flow. Identify the system’s input and output. 5
System Designing the system’s components. This phase involves the use of many levels of abstraction to decompose the problem into manageable components, identify classes and interfaces, and establish relationships among the classes and interfaces. 6
Implementation Translating the system design into programs. Separate methods are written for each component and designed to work together. This phase requires the use of a programming language like Java. The implementation involves coding, testing, and debugging. 7
Testing Code meets the requirements specification and eliminates bugs. Software engineers not involved in the design and implementation of the project usually conduct such testing. 8
Deployment makes the project available for use. For a Java applet, this means installing it on a Web server; for a Java application, installing it on the client's computer. 9
Maintenance is concerned with changing and improving the product. A software product must continue to perform and improve in a changing environment. Periodic upgrades of the product to fix newly discovered bugs and incorporate changes. 10
Relationships among Classes n Association n Aggregation n Composition n Inheritance 11
Association: binary relationship that describes an activity between two classes. Association is usually represented as a data field in the class. 12
Translation is not Unique The data field course. List in Student or Faculty can be omitted if courses are not important. 13
Association Between Same Class Association may exist between objects of the same class. For example, a person may have a supervisor. 14
Aggregation and Composition n. Aggregation : a form of association, which represents an ownership relationship between two classes. n Aggregation models the has-a relationship. n. If an object is exclusively owned by an aggregated object, the relationship between the object and its aggregated object is referred to as composition. 15
Representing Aggregation in Classes An aggregation relationship is usually represented as a data field in the aggregated class. 16
Inner Classes If Name or Address is used in the Person class only, declare them as inner classes in Person. public class Person { private Name name; private Address address; . . . class Name {. . . } class Address {. . . } } 17
Inheritance models is-a relationship between two classes. 18
Weak Inheritance A weak is-a models interfaces. For example, “students are comparable based on their grades” can be represented by implementing the Comparable interface, as follows: 19
Class Design 1. Identify classes. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 20
Borrowing Loans Name Loan Person Borrower Address 21
Borrowing Loans Test program that uses the classes Name, Person, Address, Borrower, and Loan. Borrow. Loan Run 22
The Rational Class Rational Test. Rational. Class Run 23
Class Design Guidelines n Designing a Single Class. n Using Modifiers public, protected, private and static n Using Inheritance or Aggregation n Using Interfaces or Abstract Classes 24
Designing Classes n A class should describe a single entity or a set of similar operations. n A single class with too many responsibilities can be broken into several classes to separate responsibilities. n The String, String. Buffer, and String. Tokenizer classes all deal with strings but have different responsibilities. 25
Designing Classes n Design classes for use by many different customers. The class should provide a variety of ways for customization through properties and methods. 26
Designing Classes n Classes are designed for reuse. n Design classes that impose no restrictions on what or when the user can do with it. n Design the properties to ensure that the user can set properties in any order, with any combination of values. n Design methods to function independently of their order of call. 27
Designing Classes n Provide a public no-arg constructor. n Override the equals method from Object. n Override the to. String method from Object. 28
Designing Classes n Follow standard Java programming style and naming conventions. n Choose informative names for classes, data fields, and methods. n Place the data fields before the constructor. n Place constructors before other methods. n Provide a constructor and initialize variables to avoid programming errors. 29
Visibility Modifiers n n 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 contract for the extenders encompasses the contract for the users. The extended class may increase the visibility of an instance method from protected to public, or change its implementation. 30
Using Visibility Modifiers n n n Use the private modifier to hide its data from direct access by clients. Use getter and setter methods to provide users with access to the private data, but only to private data you want the user to see or to modify. A class should also hide methods not intended for client use. The gcd method in the Rational class is private because it is only for internal use within the class. 31
static modifier n A property that is shared by all the instances of the class should be declared as a static property. 32
Using Inheritance or Aggregation n. Inheritance: is-a nan apple is fruit. n Aggregation: has-a. n. A person has a name. 33
Using Inheritance or Aggregation, n. Sometimes, the choice between inheritance and aggregation is not obvious. n. Inheritance models the relationship between the classes Circle and Cylinder. n. A cylinder consists of circles; use aggregation to define the Cylinder class as follows: 34
Using Inheritance or Composition public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ } 35
Using Inheritance or Aggregation, n. If polymorphism is desirable, use the inheritance design. n. If you don’t care about polymorphism, the aggregation design gives more flexibility because the classes are less dependent using aggregation than using inheritance. 36
Using Interfaces or Abstract Classes n. Both interfaces and abstract classes can be used to generalize common features. n. A strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes. 37
Using Interfaces or Abstract Classes n. An Orange is a Fruit (Orange extends Fruit). n. An Orange is Round (Orange implements Round) n. Strings can be compared (String implements Comparable). n. A circle or a rectangle is a geometric object (Circle extends Geometric. Object and Rectangle extends Geometric. Object). n. Circles can be compared based on their radii (Circle implements Comparable) 38
Using Interfaces or Abstract Classes n. Interfaces are more flexible than abstract classes na subclass can extend only one superclass, n. A subclass can implement any number of interfaces. n. Interfaces cannot contain concrete methods. n Combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that “partially” implements the interface. 39
The Java API n. 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. 40
- Slides: 40