1 / 32

Object-Oriented Design Part 1

Learn how to make good object-oriented design decisions, assign responsibilities to objects, and design collaborations using patterns. Explore the Creator, Information Expert, and Low Coupling patterns.

orlandof
Download Presentation

Object-Oriented Design Part 1

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. Object-Oriented Design Part 1 http://flic.kr/p/btp5ZK

  2. What are you goingto learn about today? • How to make good object-oriented design decisions http://flic.kr/p/8JpkTg

  3. Iterative Development Process We are here Analysis Requirements Design InitialPlanning Implementation Planning Testing Evaluation Deployment

  4. Responsibility-Driven Design Frames object design as deciding • How to assign responsibilities to objects • How objects should collaborate • What role each object should play in a collaboration http://flic.kr/p/btp5ZK

  5. What is meant by responsibilities An object’s obligations (and thus behavior) Two types: • Doing responsibilities, e.g.: • Creating an object • Calculating something • Initiating action on other objects • Coordinating activities among other objects • Knowing responsibilities, e.g.: • Knowing about data • Knowing about related objects • Knowing about things it can derive or calculate

  6. Domain Model attributes inspireknowing responsibilities Analysis Design Register knows its ID Sale knows its time

  7. What is meant by collaboration? Objects interact (via messages) to fulfill responsibilities • Example: Register collaborateswith Sale and Paymentto process a payment(its responsibility)

  8. Responsibilities vary in granularity • “Big” responsibilities may require collaborations of many objects • “Small” responsibilities may be fulfilled by a single object

  9. How to assign responsibilities anddesign collaborations? • No mechanical method • Requires expert human judgment! • But there’s hope: patterns! http://flic.kr/p/4tTsQe

  10. Patterns • Repeatable solution for a commonly occurring software problem • Codify existing tried-and-true knowledge • Provide vocabulary Let’s take tour 3 of Larman’s GRASP patterns

  11. ? ? Design Question #1 What object should be responsible forcreating a SalesLineItem?

  12. Creator Pattern Assign class B responsibility of creating instances of class A if 1+ of the following: • B“contains”A • B records A • B closely uses A • B has initializing data for A • B is an expert with respect to creating A

  13. ? ? Design Question #1 What object should be responsible for creating a SalesLineItem? The Creator Pattern says that Sale should create SalesLineItem

  14. How a Sale object might createa SalesLineItem object : Register : Sale

  15. ? ? Design Question #2 What object should be responsible forknowing the grand total of a sale?

  16. Information Expert Pattern Assign a knowing responsibility to the class that has the information necessary to fulfill the responsibility

  17. ? ? Design Question #2 What object should be responsible forknowing the grand total of a sale? The Information Expert Pattern says that Sale should know the grand total

  18. How Sale might calculate the grand total Sale will need a method for computing the total

  19. How Sale might calculate the grand total Sales will get the subtotal from each SalesLineItem

  20. How Sale might calculate the grand total SalesLineItem will need to get the price from ProductDescription

  21. How Sale might calculate the grand total These three objects collaborate to compute the grand totalEach one must fulfill its own set of responsibilities

  22. ? ? Design Question #3 What class should be responsible for this? Assume we need to create a Payment instance and associate it with a Sale instance

  23. Assume that Register is responsible for handling all user operations

  24. One possible design: Register creates Payment Sale depends on Payment

  25. Another possible design: Sale creates Payment

  26. Which design is better?

  27. Low Coupling Pattern Assign responsibilities so that coupling stays low Coupling: measure of how strongly one element • is connected to others • has knowledge of others • relies on others

  28. Common types of coupling in Java Class C is coupled to class D if • C has instance variable that refers to D • C invokes methods of D • C method parameter or local variable references D • C is direct or indirect subclass of D • C implements interface D

  29. Problems with high coupling Given class C highly coupled to class D: • Changes in D may force changes in C • Harder to understand C in isolation • Harder to reuse C because of dependencies on D

  30. Which design has lower coupling? First design adds extra coupling between Register and Sale so second design has lower coupling

  31. Critique: Other considerations may override preference toward low coupling • Strength of coupling should be balanced against other design considerations Critique: High coupling to stable/pervasive elements is seldom a problem • Consider coupling to Java standard libraries

  32. Summary • Responsibility-Driven Design • Patterns • Creator • Information Expert • Low Coupling http://flic.kr/p/YSY3X

More Related