310 likes | 328 Views
This article explores the concepts of responsibilities and collaborations in object-oriented design, as well as introduces the use of patterns as repeatable solutions. It discusses the GRASP patterns of Creator, Information Expert, and Low Coupling.
E N D
Object-Oriented Design Part 1 http://flic.kr/p/btp5ZK
What are you goingto learn about today? • How to make good object-oriented design decisions http://flic.kr/p/8JpkTg
Recall: Iterative development process We are here http://en.wikipedia.org/wiki/File:Iterative_development_model_V2.jpg
Responsibility-Driven Design Frames object design as deciding • How to assign responsibilities to objects • How objects should collaborate • What role each each object should play in a collaboration http://flic.kr/p/btp5ZK
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
Domain Model attributes inspireknowing responsibilities Analysis Design Register knows its ID Sale knows its time
What is meant by collaboration? Objects interact (via messages) to fulfill responsibilities • Example: Register collaborateswith Sale and Paymentto process a payment(its responsibility)
Responsibilities vary in granularity • “Big” responsibilities may require collaborations of many objects • “Small” responsibilities may be fulfilled by a single object
How to assign responsibilities anddesign collaborations? • No mechanical method • Requires expert human judgment! • But there’s hope: patterns! http://flic.kr/p/4tTsQe
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
POS Design Question What object should be responsible forcreating a SalesLineItem?
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
POS Design Question What object should be responsible for creating a SalesLineItem? The Creator Pattern says that Sale should create SalesLineItem
How a Sale object might createa SalesLineItem object : Register : Sale
POS Design Question What object should be responsible forknowing the grand total of a sale?
Information Expert Pattern Assign a knowing responsibility to the class that has the information necessary to fulfill the responsibility
POS Design Question What object should be responsible forknowing the grand total of a sale? The Information Expert Pattern says that Sale should know the grand total
How Sale might calculate the grand total Sale will need a method for computing the total
How Sale might calculate the grand total Sales will get the subtotal from each SalesLineItem
How Sale might calculate the grand total SalesLineItem will need to get the price from ProductDescription
How Sale might calculate the grand total These three objects collaborate to compute the grand totalEach one must fulfill its own set of responsibilities
POS Design Question What class should be responsible for this? Assume we need to create a Payment instance and associate it with a Sale instance
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
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
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
Which design has lower coupling?Assuming Sale will eventually need to be coupled with Payment First design adds extra coupling between Register and Sale so second design has lower coupling
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
Summary • Responsibility-Driven Design • Patterns • Creator • Information Expert • Low Coupling http://flic.kr/p/YSY3X