OO Design Principles & Metrics - Exercises
OO Design Principles & Metrics - Exercises
Exercises
Single Responsibility
ApplicationFor m
Video 0..1 1 title : Str ing year : int directorName : String directorDOB : String directorNationality : String findVideo(title : String) : Collection insertVideo() deleteVideo() updateVideo()
Interface Segregation
openAppForm() 0..1 ApplicationFor m Pseudo code: buildMenus() buildToolbars() findVideo(title : String) show() close() refreshVideoList() 0..1 void openAppForm() { applicationFor m = new ApplicationForm() applicationFor m.buildMenus() applicationFor m.buildToolbars() applicationFor m.show () }
SearchController 1 performSearch()
Dependency Inversion
performBBCSearch()
1 SearchController 1 1 AmazonSearch
performSearch() 1
performAmazonSearch()
HMVSearch
Pseudo code: void performSearch() { results = new ArrayList() results.add(bbcSearch.performBBCSearch()) results.add(amazonSearch.performAmazonSearch()) results.add(hmvSearch.performHMVSearch()) }
performHMVSearch()
Open-Closed
1. Calculate classes extended and not modified / classes extended and/or modified for this pair of models
ApplicationController ApplicationController
0..1 ApplicationFor m
openAppForm()
0..1 ApplicationFor m
openAppForm()
SearchController
performSearch()
2. How could this change have been done so that classes extended and not modified
Liskov Substitution
0..1 ApplicationFor m
openAppForm()
buildMenus() buildToolbars() findVideo(title : String) show() close() refreshVideoList(Video[ ]) 0..1 1 SearchController # title : Str ing # results : Collection performSearch()
Pseudo code: BBCSearchController void performSearch() { If(title != null) then results = webConnection.go(videotitle=+HttpContext.urlEncode(title)) }
performSearch()
2. Refactor the model so that the client wont break when BBCSearchController is substituted for SearchController
Law of Demeter
homeAddress 1
Address
1 * postcodes Postcode houseCode : String getPostalArea() : PostalArea getHouseCode() : String getPostCodeString() : String getAddresses() : List
Pseudo code: int getPopulationSize() { int population = 0 for each Postcode postcode in postcodes { for each Address address in postcode.getAddresses() { for each Contact contact in address.getContacts() { population++; } } } return population; }
Package Cohesion
PhoneNumber
PhoneList
ContactDataAccess
DbConnection
SqlConnection
Package Coupling
ui::contacts
model::contacts
PhoneNumber
IDataAccess
1 1 ContactDataAccess DbConnection
SqlConnection
3. Refactor the model to reduce the distance from the main sequence for every package where D > 0