5. object
Real world abstractions
Encapsulate state
represent information
state
Communicate by
Message passing
behavior
May execute in sequence
Or in parallel
13. What the stakeholders
want
1
Add flexibility,
Remove duplication
Encapsulation,
2 Inheritance,
Polymorphism
3
Apply patterns
Loose coupling
Delegation
14. We have a product which collects checks from
various banks and processes them.
The process includes sending out email, a fax or
storing a scan for the check.
15. Pay attention to the nouns (person, place or thing)
they are object candidates
The verbs would be the possible methods
This is called textual analysis
16. We have a product which collects checks from
various banks and processes them.
The process includes sending out email, a fax or
storing a scan for the check.
5 mins
17. We have a product which collects checks from
various banks and processes them.
The process includes sending out email, a fax or
storing a scan for the check.
20. case class Bank(id:Int, name:String)
case class Check(number:Int, bank:Bank)
class FastProcessor {
def process(checks:List[Check]) = checks foreach (check => sendEmail)
def sendEmail = println("Email sent")
}
val citibank = new Bank(1, "Citibank") //> citibank :
com.baml.ooad.Bank = Bank(1,Citibank)
(new FastProcessor).process(List(new Check(1,citibank), new Check(2,citibank)))
//> Email sent
//| Email sent
21. We need to support BoA as well and that sends
Faxes
23. case class Bank(id:Int, name:String)
case class Check(number:Int, bank:Bank)
class FastProcessor {
def process(checks:List[Check]) = checks foreach (check => if
(check.bank.name=="Citibank") sendEmail else sendFax)
def sendEmail = println("Email sent")
def sendFax = println("Fax sent")
}
val citibank = new Bank(1, "Citibank") //
val bankOfAmerica = new Bank(2, "BoA")
//
val citibankCheckList = List(new Check(1,citibank), new Check(2,citibank))
val bankOfAmericaCheckList = List(new Check(1,bankOfAmerica), new
Check(2,bankOfAmerica))
(new FastProcessor).process(citibankCheckList ::: bankOfAmericaCheckList)
//> Email sent
//| Email sent
//| Fax sent
//| Fax sent
24. We need to support HDFC and ICICI as well now!
25. good design == flexible design
whenever there is a change encapsulate it
5 mins
26. What the stakeholders
want
1
Add flexibility,
Remove duplication
Encapsulation,
2 Inheritance,
Polymorphism
3
Apply patterns
Loose coupling
Delegation
27. trait Bank {
def process(check: Check)
}
object CitiBank extends Bank {
val name = "CitiBank"
def process(check: Check) = sendEmail
def sendEmail = println("Email sent")
}
object BankOfAmerica extends Bank {
val name = "BoA"
def process(check: Check) = sendFax
def sendFax = println("Fax sent")
}
object HDFC extends Bank {
val name = "HDFC"
def process(check: Check) = {sendFax; sendEmail}
def sendEmail = println("Email sent")
def sendFax = println("Fax sent")
}
case class Check(number: Int, bank: Bank)
28. class FastProcessor {
def process(checks: List[Check]) = checks foreach (check =>
check.bank.process(check))
}
val citibankCheckList = List(new Check(1, CitiBank), new Check(2,
CitiBank))
val bankOfAmericaCheckList = List(new Check(1, BankOfAmerica), new
Check(2, BankOfAmerica))
val hdfcCheckList = List(new Check(1, HDFC))
(new FastProcessor).process(citibankCheckList :::
bankOfAmericaCheckList ::: hdfcCheckList)
//> Email sent
//| Email sent
//| Fax sent
//| Fax sent
//| Fax sent
//| Email sent
31. Code to interfaces – makes software easy to
extend
Encapsulate what varies – protect classes from
changes
Each class should have only one reason to
change
32. What the stakeholders
want
1
Add flexibility,
Remove duplication
Encapsulation,
Inheritance,
2 Polymorphism
3
Apply patterns
Loose coupling
Delegation
47. Delegation
When once class hands off the task of doing
something to another
Useful when you want to use the functionality of
another class without changing its behavior
48. bank
HDFC BoA Citibank
We delegated processing to individual banks
51. 30 min Exercise
Design an OO parking lot. What classes and
functions will it have. It should say, full, empty
and also be able to find spot for Valet parking.
The lot has 3 different types of parking: regular,
handicapped and compact.