500 likes | 517 Views
http://flic.kr/p/btp5ZK. Object-Oriented Design. What are you going to learn about today?. How to make good object-oriented design decisions. http://flic.kr/p/8JpkTg. Iterative Development Process. We are here. Analysis. Requirements. Design. Initial Planning. Implementation. Planning.
E N D
http://flic.kr/p/btp5ZK Object-Oriented Design
What are you goingto learn about today? • How to make good object-oriented design decisions http://flic.kr/p/8JpkTg
Iterative Development Process We are here Analysis Requirements Design InitialPlanning Implementation Planning Testing Evaluation Deployment
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
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 some of Larman’s GRASP patterns
? ? Design Question #1 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
? ? Design Question #1 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
? ? Design Question #2 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
? ? 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
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
? ? 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
Assume that Register is responsible for handling all user operations
Which design is better? Registerresponsible Saleresponsible
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 is better? Registerresponsible Saleresponsible Additional coupling b/t Register and Sale
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 Coupling is one criteria for choosing, but there is another criteria…
High Cohesion Pattern Assign responsibilities so that cohesion remains high Cohesion: Measure of how strongly related and focused the responsibilities of a class/package/etc are
Critique of first design How does adding the responsibility of creating payments affect Register’s cohesiveness? It may not seem like a big deal in this small example, but what if Register is responsible for a large number of user operations?
Problems with low cohesion Elements with low cohesion are • Hard to understand • Hard to reuse • Hard to maintain • Brittle (affected by change)
By delegating creating Payment to Sale, this design promotes higher cohesion in Register
Coupling and Cohesion are interrelated • Coupling: how strongly one element depends on others • Lower coupling = better • Cohesion: how focused an element’s responsibilities are • Higher cohesion = better • The lower the coupling, the higher the cohesion tends to be and vice versa
? ? Design Question #4 Why is this design fragile (in the face of change)? public void fragileMethod() { AccountHolder holder = sale.getPayment().getAccount().getAccountHolder(); ... } • Depends too much on object structure—a common point of instability • Coupling: Couples “this” to many classes • Cohesion: “This” has added responsibility of knowing how to use those classes
Law of Demeter – “Don’t talk to strangers” • Within a method, only send messages to: • The “this” object • A parameter of the method • An attribute of “this” • An element of a collection which is an attribute of “this” • An object created within the method Stranger and stranger… public void fragileMethod() { AccountHolder holder = sale.getPayment().getAccount().getAccountHolder(); ... }
? ? Design Question #5 How to make different calculators pluggable?We’ll need to decouple tax calculator from Sale, but how? Assume: • System uses external third-party tax calculator • Many different kinds, such as: TaxMaster GoodAsGoldTaxPro
Indirection Pattern To decouple two classes, assign responsibility to an intermediate object to mediate between the two • Intermediary creates indirection
Example intermediary adapter for decoupling Sale and TaxMaster class Adds level of indirection : TaxMaster
? ? Design Question #6 How do we make tax calculators interchangeable? • Tax calculators have similar but varying interfaces • One supports raw TCP socket protocol • Another has SOAP interface TaxMaster GoodAsGoldTaxPro … TCP-related operations … … SOAP-related operations …
Protected Variations Pattern To reduce the potential drawbacks of design instability: • Identify points of variation/instability • Create stable interface (in the broad sense) around such points
Example use of Protected Variations Pattern to decouple Sales and tax calculators Wrap all the different calculator adapters with one interface
? ? Design Question #7 How to wrap tax calculators with interface? ??????????????????????? <???> TaxMaster GoodAsGoldTaxPro
Polymorphism Pattern When related behaviors vary by class, use polymorphic operations to handle the behaviors Polymorphic operations: Operations of different objects that have the same signature, making the objects/methods interchangeable
How to apply Polymorphism to tax calculators Adapters share same interface but have different implementations adaptee adaptee adaptee <???> TaxMaster GoodAsGoldTaxPro Thus, different tax calculators can be swapped in and out
How have you used polymorphismin your project? http://flic.kr/p/9ksxQa
For more OOD patterns, see this classic book by the “Gang of Four”
What’s next? • Exam 3 next class • Beta Presentations during the final exam slot • Tue 10 Dec 1:00-3:00 • Beta Milestone due before presentations • Don’t forget about Homework 6!!! http://flic.kr/p/YSY3X