1 / 48

COMP 121

COMP 121. Week 7: Object-Oriented Design and Efficiency of Algorithms. Objectives. Learn about the software life cycle Learn how to discover new classes and methods Understand the use of CRC cards for class discovery

dory
Télécharger la présentation

COMP 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms

  2. Objectives • Learn about the software life cycle • Learn how to discover new classes and methods • Understand the use of CRC cards for class discovery • Be able to identify inheritance, aggregation, and dependency relationships between classes

  3. Objectives (cont’d) • Learn to use UML class diagrams to describe class relationships • Learn how to use object-oriented design to build complex programs • Learn how to analyze the efficiency of an algorithm

  4. Software Life Cycle • Encompasses all activities from initial analysis until obsolescence • Formal process for software development • Describes phases of the development process • Gives guidelines for how to carry out the phases

  5. Development Process • Analysis • Design • Implementation • Testing • Deployment

  6. Analysis Phase • Defines what the project is suppose to do • Not concerned with how the program will accomplish tasks • What is the output of the Analysis Phase?

  7. Design Phase • Plans system implementation • Identifies Classes, Methods Needed • What is the output of the Design phase?

  8. Implementation Phase • Write and compile the code • Implement classes and methods discovered in the design phase • Output?

  9. Testing Phase • Run tests to verify the program works correctly • Output: a report of the tests and their results, test coverage

  10. Deployment Phase • Software installed and used for its intended purpose

  11. The Waterfall Model

  12. The Waterfall Model • Sequential process of analysis, design, implementation, testing, and deployment • When rigidly applied, does not work well!

  13. The Spiral Model • Breaks development process down into multiple phases • Early phases focus on the construction of prototypes • Lessons learned from development of one prototype can be applied to the next iteration • Problem: can lead to many iterations, and process can take too long to complete

  14. The Spiral Model

  15. Extreme Programming • Strives for simplicity • Removes formal structure • Focuses on best practices • Realistic planning • Small releases • Metaphor • Simplicity • Testing • Re-factoring

  16. Extreme Programming (cont’d) • Focuses on best practices • Pair programming • Collective ownership • Continuous integration • 40-hour week • On-site customer • Coding standards

  17. Test Driven Development

  18. Test Driven Development • Typical Sequence of Steps: • Create a new test case • Write enough code to fail the test • Run the tests and watch the new test fail • Write the simplest code that will pass the new test • Run the tests and watch all the tests pass • Remove duplication • Rerun the tests

  19. Test Driven Development

  20. Discovering Classes • Class represents set of objects with the same behavior • Entities with multiple occurrences in problemdescription are good candidates for objects • Identify commonalities • Design classes to capture commonalities • Represent some entities as objects, others as primitive types • Should we make a class Address or use a String? • Not all classes can be discovered in analysis phase • Some classes may already exist?

  21. CRC Cards • Class, Responsibility, Collaboration • Used to start brainstorming about an object-oriented design • Responsibilities are operations performed by the class • Collaborators are other classes involved in performing these operations

  22. CRC Cards (cont’d)

  23. Relationships Between Classes • Inheritance • Aggregation • Dependency

  24. Inheritance • Is-a relationship • Relationship between a more general class (superclass) anda more specialized class (subclass) • Example: Every savings account is a bank account • Can be overused: Should a Tire be a subclass of Circle? • Design Principle: Favor composition over inheritance

  25. Aggregation • Has-a relationship • Objects of one class contain references to objects of another class • Use an instance variable • A tire has a circle as its boundary: class Tire{ . . . private String rating; private Circle boundary;}

  26. Aggregation (cont’d) • Every car has a tire (in fact, it has multiple tires) class Car extends Vehicle{ . . . private Tire[] tires;}

  27. UML Notation

  28. Dependency • Uses relationship • Example: Many applications depend on the Scanner class to read input • Aggregation is a stronger form of Dependency • BlueJ only shows Uses relationships

  29. UML Diagram Example

  30. Class Diagram - BlueJ

  31. Five-Part Development Process • Gather requirements • Use CRC cards to find classes, responsibilities, and collaborators • Use UML diagrams to record class relationships • Use javadoc to document method behavior • Implement your classes

  32. Discussion Questions? • How does this development process apply to your lab assignments? • Gather requirements • Use CRC cards to find classes, responsibilities, and collaborators • Use UML diagrams to record class relationships • Use javadoc to document method behavior • Implement your code

  33. Requirements • The requirements are defined in the lab write-up that is handed out • If they are unclear, you should get clarification from the customer (me!) • From the requirements, begin thinking about the classes, responsibilities, and relationships • It may be helpful to do this BEFORE you look at the code that was provided

  34. CRC Cards • Discover classes • Most, if not all, classes are already determined • Discover responsibilities • Relate the method names to the verbs in the requirements • Describe relationships • Consider whether there are other relationships between the classes besides those already established

  35. Javadoc • Examine the javadoc that is included with the lab • Consider how the methods relate to the responsibilities on the CRC cards • Consider whether there are other methods that should be included

  36. Implementation • Implement the code using test-driven development

  37. Summary • The life cycle of software encompasses all activities from initial analysis until obsolescence • The waterfall model describes a sequential process of analysis, design, implementation, testing, and deployment • The spiral method describes an iterative process in which design and implementation are repeated • Extreme Programming is a methodology that strives for simplification and focuses on best practices • In object-oriented design, you discover classes, determine the responsibilities of the classes, and describe the relationships between classes • A CRC card describes a class, its responsibilities, and its collaborating classes

  38. Summary (cont’d) • Inheritance (the is-a relationship) is sometimes inappropriately used when the has-a relationship would be more appropriate • Aggregation (the has-a relationship) denotes that objects of one class contain references to objects of another class • Dependency is another name for the uses relationship • UML uses different notations for inheritance, interface implementation, aggregation, and dependency • Use javadoc comments to document the behavior of classes

  39. Questions?

  40. Efficiency of Algorithms • Difficult to get a precise measure of the performance of an algorithm or program • Can characterize an algorithm by how the execution time increases as a function of the size of the input (asymptotic performance) • Big-O notation

  41. Common Growth Rates

  42. Growth Rate Functions

  43. Growth Rate Table

  44. What is the Big-O of this algorithm for (int i=0; i<n; i++) for (int j=0; j<n; j++) System.out.println(i + “ “ + j);

  45. What is the Big-O of this algorithm for (int i=0; i<n; i++) for (int j=0; j<2; j++) System.out.println(i + “ “ + j);

  46. What is the Big-O of this algorithm for (int i=0; i<n; i++) for (int j=n-1; j>=i; j--) System.out.println(i + “ “ + j);

  47. What is the Big-O of this algorithm for (int i=1; i<n; i++) for (int j=0; j<i; j++) if (j%i == 0) System.out.println(i + “ “ + j);

  48. Questions?

More Related