Download presentation
Presentation is loading. Please wait.
Published byClifton Golden Modified over 8 years ago
1
Inheritance vs Composition Picking the right tool
2
Inheritance – Technical mechanism for code reuse – Specific guarantee : Is-A Relationship Apple is-a Fruit Composition – Technical mechanism for code reuse – Indicate Has-A Relationship Car has-a Engine
3
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
4
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
5
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
6
Is-A Gurantee DerivedCylinder can be treated like a Circle!!!
7
Composed Cylinder Have Circle, want to make Cylinder… – With Composition
8
Composed Cylinder Can't treat Cylinder as a Circle
9
Other Composition +'s Can have multiple members of same type – Triangle has three Points
10
Type Explosion Want to represent orders at a company – Email, Fax, Phone, InPerson orders – Credit, Check, Cash payment options
11
Type Explosion Option 1 : Inheritance Order class -Sub classes EmailOrder, FaxOrder, PhoneOrder… -EmailOrder subclasses: EmailCreditOrder, EmailCheckOrder, EmailCashOrder… -FaxOrderSubClasses: FaxCreditOrder, FaxCheckOrder, FaxCashOrder… …
12
Type Explosion Option 2 : Composition OrderType class -subtypes: Email, Phone, … PaymentType class -subtypes: Credit, Check, … Order class -Members : OrderType, PaymentType
13
Type Explosion Option 3 : Multiple Inheritance OrderType class -subtypes: Email, Phone, … PaymentType class -subtypes: Credit, Check, … EmailCreditOrder – multiple inheritance from Email, Credit EmailCheckOrder – multiple inheritance from Email, Check …
14
Google says Composition is often more appropriate than inheritance. When using inheritance, make it public… Try to restrict use of inheritance to the "is-a" case: Bar subclasses Foo if it can reasonably be said that Bar "is a kind of" Foo.
15
Designing with Inheritance 1.Identify existing Is-A relationships – Use Inheritance to eliminate duplication 2.Identify remaining duplicate information 3.Look to introduce new parent classes – Where appropriate
16
Inheritance Design Sample Want to represent following worker types: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need name, title, salary, amount of sales, and method to calculate the amount for paycheck
17
1) Existing Is-A relationship: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need name, title, salary, amount of sales, and method to calculate the amount for paycheck
18
1B) Inherit & Eliminate – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
19
2) Look for other duplication – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need amount of sales, and method to calculate the amount for paycheck
20
3) "Discover" common parent class – Employee Need name, title, method to calculate amount for paycheck – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
21
3B) Inherit and eliminate – Employee Need name, title, method to calculate amount for paycheck – Hourly Need hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
22
Quick Challenge Sketch a quick UML diagram for classes below – Don't worry about behaviors, just state – Add classes where appropriate DVD Title, Release year, Director, Price CD Title, Release year, Genre, Price Book Title, Isbn, Price EBook Title, Isbn, Format (PDF/Kindle/etc…), Price
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.