Download - OOPs Development with Scala
![Page 1: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/1.jpg)
Introduction to
Object Oriented Programming
Mumbai
![Page 2: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/2.jpg)
What is an object?
Dog
Snowy
![Page 3: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/3.jpg)
Dog is a generalization of Snowy
Animal
Snowy
Dog
Subclass?
![Page 4: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/4.jpg)
Animal
Bird
Dog
Polymorphism?
![Page 5: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/5.jpg)
object
Real world abstractions
Encapsulate staterepresent information
Communicate by Message passing
May execute in sequenceOr in parallel
behavior
state
![Page 6: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/6.jpg)
state
name
behavior
![Page 7: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/7.jpg)
Buildingblocks
inheritance encapsulation
polymorphism
![Page 8: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/8.jpg)
Inheritance lets you build classes based on other classes, thus avoiding duplicating and repeating
code
![Page 9: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/9.jpg)
When a class inherits from another, Polymorphism allows a subclass to standin for a
superclass
Bird.flapWings()
duck
cuckoo
ostrich
![Page 10: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/10.jpg)
Encapsulation is to hide the internal representation of the object from view outside
object definition
Car
drive()
Car.drive()
![Page 11: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/11.jpg)
Vehicle
car
civiccorolla
camry
Harley-davidsonhonda
accord
motorcycle
toyota
5 mins
![Page 12: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/12.jpg)
Object Oriented Solutions
for
![Page 13: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/13.jpg)
What the stakeholders want
Add flexibility, Remove duplicationEncapsulation, Inheritance, Polymorphism
Apply patternsLoose couplingDelegation
1
2
3
![Page 14: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/14.jpg)
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.
![Page 15: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/15.jpg)
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
![Page 16: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/16.jpg)
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
![Page 17: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/17.jpg)
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.
![Page 18: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/18.jpg)
FastProcessor
process(check:Check)sendEmail(check:Check)sendFax(check:Check)
scan(check:Check)
Bank
Check----------------bank:Bank
*
![Page 19: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/19.jpg)
object interactions
![Page 20: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/20.jpg)
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
![Page 21: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/21.jpg)
We need to support BoA as well and that sends Faxes
![Page 22: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/22.jpg)
We dont touch the design
![Page 23: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/23.jpg)
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
![Page 24: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/24.jpg)
We need to support HDFC and ICICI as well now!
![Page 25: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/25.jpg)
good design == flexible design
whenever there is a change encapsulate it
5 mins
![Page 26: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/26.jpg)
What the stakeholders want
Add flexibility, Remove duplicationEncapsulation, Inheritance, Polymorphism
Apply patternsLoose couplingDelegation
1
2
3
![Page 27: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/27.jpg)
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)
![Page 28: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/28.jpg)
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
![Page 29: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/29.jpg)
bank
BoA CitibankHDFC
Check
FastProcessor
![Page 30: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/30.jpg)
bank
BoA CitibankHDFC
Check
FastProcessor
![Page 31: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/31.jpg)
Code to interfaces – makes software easy to extend
Encapsulate what varies – protect classes from changes
Each class should have only one reason to change
![Page 32: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/32.jpg)
What the stakeholders want
Apply patternsLoose couplingDelegation
1
2
3
Add flexibility, Remove duplicationEncapsulation, Inheritance, Polymorphism
![Page 33: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/33.jpg)
OO Principles
result in maintenable, flexible and extensible software
![Page 34: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/34.jpg)
Open Closed Principle
Classes should be open for extension and closed for modification
![Page 35: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/35.jpg)
bank
BoA CitibankHDFC
Any number of banks?
![Page 36: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/36.jpg)
DRY
Don't repeat yourself
All duplicate code should be encapsulated / abstracted
![Page 37: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/37.jpg)
bank
BoA Citibank
Check
FastProcessor
CommunicationUtils
HDFC
![Page 38: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/38.jpg)
What the stakeholders want
Apply patternsLoose couplingDelegation
1
2
3
Add flexibility, Remove duplicationEncapsulation, Inheritance, Polymorphism
![Page 39: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/39.jpg)
Single Responsibility Principle
Each object should have only one reason to change
![Page 40: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/40.jpg)
![Page 41: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/41.jpg)
What methods should really belong to Automobile?
![Page 42: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/42.jpg)
![Page 43: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/43.jpg)
Liskov Substitution Principle
Subtypes MUST be substitutable for their base types
Ensures well designed inheritance
![Page 44: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/44.jpg)
Is this valid?
![Page 45: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/45.jpg)
class Rectangle { var height: Int = 0 var width: Int = 0 def setHeight(h: Int) = { height = h } def setWidth(w: Int) = { width = w } }
class Square extends Rectangle { override def setHeight(h: Int) = { height = h; width = h } override def setWidth(w: Int) = { width = w; height = w } }
val rectangle = new Square rectangle.setHeight(10) rectangle.setWidth(5)
assert(10 == rectangle.height) //> java.lang.AssertionError: assertion failed
![Page 46: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/46.jpg)
There are multiple options other than
inheritance
![Page 47: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/47.jpg)
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
![Page 48: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/48.jpg)
bank
BoA CitibankHDFC
We delegated processing to individual banks
![Page 49: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/49.jpg)
Composition and Aggregation
To assemble behaviors from other classes
![Page 50: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/50.jpg)
BoA Citibank
CommunicationUtils
HDFC
![Page 51: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/51.jpg)
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.
![Page 52: OOPs Development with Scala](https://reader033.vdocument.in/reader033/viewer/2022052621/55838e54d8b42a8e0c8b505e/html5/thumbnails/52.jpg)