Your SlideShare is downloading. ×
0
Introduction toObject Oriented Programming            Mumbai
What is an object?       Dog      Snowy
Dog is a generalization of Snowy                               Dog            Animal                               SnowySu...
Dog                Animal                         BirdPolymorphism?
object                Real world abstractions                      Encapsulate state                    represent informat...
namestate          behavior
inheritance       encapsulation       Building        blocks               polymorphism
Inheritance lets you build classes based on other classes, thus avoiding duplicating and repeating                       c...
When a class inherits from another,Polymorphism allows a subclass to standin for a                 superclass       duck  ...
Encapsulation is to hide the internalrepresentation of the object from view outside               object definition       ...
camry                 car                                accord                                          Vehicle          ...
Object OrientedSolutionsfor
What the stakeholders                         want   1                              Add flexibility,                      ...
We have a product which collects checks from      various banks and processes them.The process includes sending out email,...
Pay attention to the nouns (person, place or thing)            they are object candidates    The verbs would be the possib...
We have a product which collects checks from      various banks and processes them.The process includes sending out email,...
We have a product which collects checks from      various banks and processes them.The process includes sending out email,...
FastProcessor process(check:Check)                       BanksendEmail(check:Check) sendFax(check:Check)   scan(check:Chec...
object interactions
case class Bank(id:Int, name:String) case class Check(number:Int, bank:Bank) class FastProcessor { def process(checks:List...
We need to support BoA as well and that sends                   Faxes
We dont touch the design
case class Bank(id:Int, name:String)  case class Check(number:Int, bank:Bank)  class FastProcessor {  def process(checks:L...
We need to support HDFC and ICICI as well now!
good design == flexible designwhenever there is a change encapsulate it    5 mins
What the stakeholders                         want   1                              Add flexibility,                      ...
trait Bank {    def process(check: Check)  } object CitiBank extends Bank {   val name = "CitiBank"   def process(check: C...
class FastProcessor {    def process(checks: List[Check]) = checks foreach (check =>check.bank.process(check)) }  val citi...
bank                                 FastProcessorHDFC   BoA    Citibank                         Check
bank                                 FastProcessorHDFC   BoA    Citibank                         Check
Code to interfaces – makes software easy to                    extendEncapsulate what varies – protect classes from       ...
What the stakeholders                         want   1                                Add flexibility,                    ...
OO Principlesresult in maintenable, flexible and extensible                  software
Open Closed PrincipleClasses should be open for extension and closed                for modification
bankHDFC    BoA   Citibank                         Any number of banks?
DRY           Dont repeat yourselfAll duplicate code should be encapsulated /                 abstracted
bank                                         FastProcessorHDFC        BoA       Citibank                                 C...
What the stakeholders                         want   1                                Add flexibility,                    ...
Single Responsibility PrincipleEach object should have only one reason to                  change
What methods should really belong toAutomobile?
Liskov Substitution PrincipleSubtypes MUST be substitutable for their base                  types      Ensures well design...
Is this valid?
class Rectangle {   var height: Int = 0   var width: Int = 0   def setHeight(h: Int) = { height = h }   def setWidth(w: In...
There are multiple options other than             inheritance
Delegation When once class hands off the task of doing           something to anotherUseful when you want to use the funct...
bankHDFC   BoA      Citibank       We delegated processing to individual banks
Composition and AggregationTo assemble behaviors from other classes
HDFC        BoA       Citibank       CommunicationUtils
30 min Exercise     Design an OO parking lot. What classes and     functions will it have. It should say, full, empty     ...
OOPs Development with Scala
OOPs Development with Scala
OOPs Development with Scala
Upcoming SlideShare
Loading in...5
×

OOPs Development with Scala

1,271

Published on

Getting started with OOPs and Scala in an Agile way

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,271
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "OOPs Development with Scala"

  1. 1. Introduction toObject Oriented Programming Mumbai
  2. 2. What is an object? Dog Snowy
  3. 3. Dog is a generalization of Snowy Dog Animal SnowySubclass?
  4. 4. Dog Animal BirdPolymorphism?
  5. 5. object Real world abstractions Encapsulate state represent information state Communicate by Message passingbehavior May execute in sequence Or in parallel
  6. 6. namestate behavior
  7. 7. inheritance encapsulation Building blocks polymorphism
  8. 8. Inheritance lets you build classes based on other classes, thus avoiding duplicating and repeating code
  9. 9. When a class inherits from another,Polymorphism allows a subclass to standin for a superclass duck cuckoo Bird.flapWings() ostrich
  10. 10. Encapsulation is to hide the internalrepresentation of the object from view outside object definition Car.drive() Car drive()
  11. 11. camry car accord Vehicle toyota motorcycle honda Harley-davidsoncivic corolla 5 mins
  12. 12. Object OrientedSolutionsfor
  13. 13. What the stakeholders want 1 Add flexibility, Remove duplication Encapsulation, 2 Inheritance, Polymorphism 3Apply patternsLoose couplingDelegation
  14. 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. 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. 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. 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.
  18. 18. FastProcessor process(check:Check) BanksendEmail(check:Check) sendFax(check:Check) scan(check:Check) * Check ---------------- bank:Bank
  19. 19. object interactions
  20. 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. 21. We need to support BoA as well and that sends Faxes
  22. 22. We dont touch the design
  23. 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), newCheck(2,bankOfAmerica)) (new FastProcessor).process(citibankCheckList ::: bankOfAmericaCheckList) //> Email sent //| Email sent //| Fax sent //| Fax sent
  24. 24. We need to support HDFC and ICICI as well now!
  25. 25. good design == flexible designwhenever there is a change encapsulate it 5 mins
  26. 26. What the stakeholders want 1 Add flexibility, Remove duplication Encapsulation, 2 Inheritance, Polymorphism 3Apply patternsLoose couplingDelegation
  27. 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. 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), newCheck(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
  29. 29. bank FastProcessorHDFC BoA Citibank Check
  30. 30. bank FastProcessorHDFC BoA Citibank Check
  31. 31. Code to interfaces – makes software easy to extendEncapsulate what varies – protect classes from changes Each class should have only one reason to change
  32. 32. What the stakeholders want 1 Add flexibility, Remove duplication Encapsulation, Inheritance, 2 Polymorphism 3Apply patternsLoose couplingDelegation
  33. 33. OO Principlesresult in maintenable, flexible and extensible software
  34. 34. Open Closed PrincipleClasses should be open for extension and closed for modification
  35. 35. bankHDFC BoA Citibank Any number of banks?
  36. 36. DRY Dont repeat yourselfAll duplicate code should be encapsulated / abstracted
  37. 37. bank FastProcessorHDFC BoA Citibank Check CommunicationUtils
  38. 38. What the stakeholders want 1 Add flexibility, Remove duplication Encapsulation, Inheritance, 2 Polymorphism 3Apply patternsLoose couplingDelegation
  39. 39. Single Responsibility PrincipleEach object should have only one reason to change
  40. 40. What methods should really belong toAutomobile?
  41. 41. Liskov Substitution PrincipleSubtypes MUST be substitutable for their base types Ensures well designed inheritance
  42. 42. Is this valid?
  43. 43. 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
  44. 44. There are multiple options other than inheritance
  45. 45. Delegation When once class hands off the task of doing something to anotherUseful when you want to use the functionality of another class without changing its behavior
  46. 46. bankHDFC BoA Citibank We delegated processing to individual banks
  47. 47. Composition and AggregationTo assemble behaviors from other classes
  48. 48. HDFC BoA Citibank CommunicationUtils
  49. 49. 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.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×