slide2.4_2009_r1 - Module 2.4 Object-Oriented Design...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 2.4 Object-Oriented Design EE3206/EE5805 Java Programming and Applications 1 Intended Learning Outcomes To become familiar with the process of program development. To To discover classes using CRC cards. To To understand the impacts of coupling to a system. To To learn the relationship types: association, aggregation, composition, dependency, realization and generalization. composition, To understand design principles and guidelines. To To design classes and establish relationships by applying the principles and guidelines. principles EE3206/EE5805 Java Programming and Applications 2 Introduction The preceding topics introduced objects, classes, class inheritance, and interfaces. You learned the concepts and tools for object-oriented programming. tools oriented This topic will give you more insight about the analysis and design of OO software systems. You will learn the guidelines for designing class and making them reusable. EE3206/EE5805 Java Programming and Applications 3 Software Development Process Requirement Specification System Analysis System Design Implementation Testing Deployment Maintenance EE3206/EE5805 Java Programming and Applications 4 Requirement Specification Requirement Specification System Analysis System Design A formal process that seeks to understand the problem and document in detail what the software system needs to do. This phase involves close interaction between users and designers. Implementation Testing Most of the examples in this book are simple, and their requirements are clearly stated. In the real world, however, problems are not well defined. You need to study a problem carefully to identify its requirements. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 5 System Analysis Requirement Specification System Analysis System Design Seeks to analyze the business process in terms of data flow, and to identify the system’s input and output. Implementation Testing Part of the analysis requires modeling the system’s behavior. The model is intended to capture the essential elements of the system and to define services to the system. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 6 System Design Requirement Specification System Analysis System Design The process of designing the system’s components. Implementation Testing 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. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 7 Implementation Requirement Specification System Analysis System Design The process of translating the system design into programs. Separate programs are written for each component and put to work together. Implementation Testing This phase requires the use of a programming language like Java. The implementation involves coding, testing, and debugging. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 8 Testing (System) Requirement Specification System Analysis System Design Ensures that the code meets the requirements specification and weeds out bugs. An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing. Implementation e.g. Acceptance testing Functional testing Black-box testing QA testing Testing Acceptance test generally involves running a suite of tests on the completed system. Each individual test, known as a case, exercises a particular operating condition of the user's environment or feature of the system, and will result in a pass or fail expected outcome. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 9 Deployment Requirement Specification System Analysis System Design Deployment makes the project available for use. Implementation Testing For a Java applet, this means installing it on a Web server; for a Java application, installing it on the client's computer. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 10 Maintenance Requirement Specification System Analysis System Design Maintenance is concerned with changing and improving the product. Implementation Testing A software product must continue to perform and improve in a changing environment. This requires periodic upgrades of the product to fix newly discovered bugs and incorporate changes. Deployment Maintenance EE3206/EE5805 Java Programming and Applications 11 Discovering Classes Using CRC Card Class Responsibility Collaborator (CRC) cards are a popular brainstorming tool brainstorming tool used in discovering classes. It uses an index card for each class as shown below. used A class represents a collection of similar objects. class A responsibility is something that a class knows or does. knows or does A collaborator is another class that a class interacts with to fulfill its responsibilities. collaborator Class name Student Register a course Responsibilities Course Collaborators EE3206/EE5805 Java Programming and Applications 12 Using CRC to Capture a Student For example, students have names, addresses, and phone numbers. These are the things a student knows. Students also enroll in seminars, drop seminars, the op and request transcripts. These are the things a student does. Sometimes a class has a responsibility to fulfill, but not have enough information to do it. For example, to accomplish enrollment of seminars, a information eminars, student needs to know if a seat is available in the seminar and, iif so, he then f student requests to be added to the seminar. Class name Other classes being associated with Data fields Methods EE3206/EE5805 Java Programming and Applications 13 Class Relationships Class Relationships Dependency Dependency Association Association Aggregation Aggregation Composition Composition Generalization Generalization Realization Realization EE3206/EE5805 Java Programming and Applications 14 Coupling Dependency, association, aggregation, composition, realization and generalization all describe coupling relationships between two and wo classes. The difference is the degree of coupling. degree In general, lower degree of coupling implies higher stability of In the system that a change in one module will not require a change change in the implementation of another module. Low coupling is often a desirable system property. Low coupling increases dependency, association, aggregation, composition, realization, generalization EE3206/EE5805 Java Programming and Applications 15 Dependency Dependency describes a using relationship between two classes Dependency describes using between where one (called client) uses the other (called supplier). The client object and the supplier object are loosely coupled. loosely The dependency relationship is usually implemented using methods. methods If A depends on B, then class A usually presents a method that take an If ake object of B as parameters. (i.e. A uses B in one of A’s method) object In UML, draw a dashed line with an arrow from the client class to In the supplier class. For example, the ArrayList class uses Object ArrayList class because you can add objects to an ArrayList. ArrayList ArrayList Object Calendar Date EE3206/EE5805 Java Programming and Applications 16 Association Association represents a structural relationship that specifies objects of one thing Association represents structural that are connected to objects of another and there is a channel between classes through which messages can be sent. . It is a binary peer-to-peer relationship. One can navigate another from both sides. It The state of the object changes when its associated object changes. The es. The association relationship is usually implemented using data fields. There is a strong data There connection between two classes. connection Student 5..60 Take * Course 0..3 Teach 1 Teacher Faculty public class Student { /** Data fields */ private Course courseList; /** Constructors */ /** Methods */ } public class Course { /** Data fields */ private Student classList; private Faculty faculty; /** Constructors */ /** Methods */ } public class Faculty { /** Data fields */ private Course courseList; /** Constructors */ /** Methods */ } 17 EE3206/EE5805 Java Programming and Applications Association Properties The properties are optional in UML diagram The Name Name Describe the nature of the relationship Describe May give a direction to the name May Digit * 0..1 1..* 3..5 0, 2..5, 9..* The exact number of elements Zero to many Zero or one One to many Three to five Zero, two to five, and nine to Zero, many many Role Role Specify the role it plays in the relationship Specify Multiplicity Multiplicity State how many instances may be connected across the relationship across EE3206/EE5805 Java Programming and Applications 18 Association Between Same Class Association may exist between objects of the same Association class. For example, a person may have a supervisor. supervisor 1 Person student 1 EE3206/EE5805 Java Programming and Applications 19 Aggregation and Composition Aggregation iis a special form of association, which represents a whole-part Aggregation s whole part relationship (a.k.a. has-a relationship) between two classes. relationship The part cannot exist without the whole. The Composition is a stronger form of aggregation. It adds lifetime responsibiliity ty Composition to aggregation that a whole is responsible for the lifetime of its part. to ts The instance of Name is only owned by the instance of Person at a time. The instance of Address can be owned by the instance of Person as well as instance of other classes simultaneously. For example, if you and your family members live in the same place, you all For ce, share the same address, but have you own unique name. Composition part Name whole Person EE3206/EE5805 Java Programming and Applications Aggregation part Address 20 Representing Aggregation and Representing Composition in Classes Composition An aggregation or composition relationship is usually represented as a data field in the aggregated class. data The implementations of aggregation and association do not have much difference. Their main difference is in the semantics but not implementation. public class Name { /** Data fields */ /** Constructors */ /** Methods */ } /** Constructors */ /** Methods */ } public class Person { /** Data fields */ private Name name; private Address address; public class Address { /** Data fields */ /** Constructors */ /** Methods */ } EE3206/EE5805 Java Programming and Applications 21 Inner Classes If Name or Address iis used in the Person Name or Address s Person class only, they can be declared as an inner class class in Person. Person There are three types of inner class: There member inner class declared within another class declared public class Person { public private Name name; private private Address address; ... private class Name { ... } private class Address { ... } } local inner class declared within the body of a method anonymous inner class. anonymous declared within the body of a method without naming it The following example uses member inner class. class. EE3206/EE5805 Java Programming and Applications 22 Generalization Generalization models the inheritance relationship (is-a relationship) inheritance relationship) between two classes. generalized class (superclass) generalized specialized class (subclass) specialized public class Faculty extends Person { Person Faculty /** Data fields */ /** Constructors */ /** Methods */ } (A) (B) EE3206/EE5805 Java Programming and Applications 23 Realization Realization represents a is-a-kind-of relationship, which describes a class provides is which an implementation of a contract specified by an interface class. an For example, the is-a-kind-of relationship “students are comparable based on their For of students grades” can be represented by implementing the Comparable interface. grades can Comparable In other words, Student realizes the contract, compareTo(), stated by Comparable. compareTo(), public class Student extends Person implements Comparable { Person Student <<interface>> Comparable /** Data fields, Constructors, and */ /** Methods */ /** Implement the compareTo method */ public int compareTo(Object object) { // ... } } (A) (B) EE3206/EE5805 Java Programming and Applications 24 Class Design in 4 Steps 1. Identify classes for the system. Ordinary classes, abstract classes, interfaces Ordinary 2. Describe attributes and methods in each class. Using Modifiers public, protected, private and static public protected private and static 3. Establish relationships among classes. Dependency, Association, generalization, realization, etc. Dependency, 4. Create classes. EE3206/EE5805 Java Programming and Applications 25 Class Design Principles Single Responsibility Principle (SRP) Single A class should describe a single entity or a set of similar operations. class ations. A single entity with too many responsibilities can be broken into several single several classes to separate responsibilities. Don’t Repeat Yourself (DRY) Don Classes should be designed for use and reuse in many different siituations. tuations. Classes One carefully and nicely designed piece of work could be useful in a wide range of applications and increases your productivity. You should design a class that imposes no or minimal restrictions on what or You on when the user can do with it. That means users can incorporate cllasses in asses when many different combinations, orders, and environments. The class should provide a variety of ways for customization through The ough properties and methods. This can also increase the chance of adoption of the properties ption class. class. EE3206/EE5805 Java Programming and Applications 26 Using Visibility Modifiers Each class can present two contracts – one for the users one of the class and one for the extenders of the class. Make the fields private and accessor/mutator methods accessor/mutator methods public if they are intended for the users of the class. A class should use the private modifier to hide its data from direct access by clients. You can use get methods and set 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. class Make the fields or method protected if they are intended for extenders of the class. EE3206/EE5805 Java Programming and Applications 27 Using the static Modifier A property that is shared by all the instances of the class should be declared as a static property. should Note that a static method (e.g. the main method) cannot reference to the non-static data fields like an instance reference static method does. This is because the non-static data fields are instance This static variables which are not exist in the static context when executing a static method. EE3206/EE5805 Java Programming and Applications 28 Using Inheritance or Aggregation Sometimes, the choice between inheritance and aggregation is not obvious. For example, the relationship between the classes Circle obvious. Circle and Cylinder can apparently be modeled with inheritance. But one and Cylinder can could argue that a cylinder consists of circles; thus, you might use could use aggregation to define the Cylinder class as follows: aggregation Aggregation public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ } } Inheritance public class Cylinder extends Circle { /** Constructors */ /** Methods */ EE3206/EE5805 Java Programming and Applications 29 Using Inheritance or Aggregation Both designs are fine, but which one is preferred? Both If polymorphism is desirable, you need to use the inheritance design. inheritance If you don’t care about polymorphism, the aggregation If care design gives more flexibility because the classes are less dependent on the other when using aggregation than using inheritance. using EE3206/EE5805 Java Programming and Applications 30 Class Design Example Borrowing Loans The following is a test program that uses the classes Name, Name Person, Address, Borrower, and Loan. Person Address Borrower and Loan Name -firstName: String -mi: char -lastName: 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 -name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +seName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String Person -street: String -city: String -state: String -zip: String Address Borrower -loan: Loan +Borrower() +Borrower(name: Name, address: Address) +getLoan(): Loan +setLoan(loan: Loan): void +toString(): 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 Loan EE3206/EE5805 Java Programming and Applications 31 ...
View Full Document

Ask a homework question - tutors are online