OO programming introduction

1,390 views

Published on

An introduction to OO Programming.
Visit http://jpereira.eu/2012/04/11/oo-design-principles-or-guidelines-part-1/ for more information. If you want to download the presentation, contact me (find my contacts on my page)

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,390
On SlideShare
0
From Embeds
0
Number of Embeds
575
Actions
Shares
0
Downloads
1
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

OO programming introduction

  1. 1. OO Programming - Introduction João Pereira October 2012 OO Programming - Introduction by João Miguel Pereira is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. OO Programming- Introduction Refreshing OO programming conceptsOO Programming - Introduction is licensed under a Creative Commons Attribution- 2NonCommercial-ShareAlike 3.0 Unported License.
  3. 3. OO Programming Concepts Why do I need objects ?OO Programming - Introduction is licensed under a Creative Commons Attribution- 3NonCommercial-ShareAlike 3.0 Unported License.
  4. 4. OO Programming Concepts • I can easily model real world concepts. CarOO Programming - Introduction is licensed under a Creative Commons Attribution- 4NonCommercial-ShareAlike 3.0 Unported License.
  5. 5. OO Programming Concepts • I can focus on the data and behavior of the atoms of my system Focus on grouping the Wheel state for a Diameter wheel Weight Model supporting Load Move() autoCalibrate() checkPressure() Focus on currentLoad() grouping the behaviorOO Programming - Introduction is licensed under a Creative Commons Attribution- 5NonCommercial-ShareAlike 3.0 Unported License.
  6. 6. OO Programming Concepts • I can share objects and still have everything I need to use them in other systems Machine A Machine B Object Object Object’s Object’s state Network state Object’s Object’s behavior behaviorOO Programming - Introduction is licensed under a Creative Commons Attribution- 6NonCommercial-ShareAlike 3.0 Unported License.
  7. 7. OO Programming Concepts • I can build small, manageable, self- contained objects and use them as building blocks for bigger things Wheel Wheel Wheel Seat Belt Wheel Wheel Tire Objects Car OO ProgramOO Programming - Introduction is licensed under a Creative Commons Attribution- 7NonCommercial-ShareAlike 3.0 Unported License.
  8. 8. OO Programming Concepts • I can reuse objects to build different systems Wheel Wheel Wheel Seat Seat Objects Bike OO ProgramOO Programming - Introduction is licensed under a Creative Commons Attribution- 8NonCommercial-ShareAlike 3.0 Unported License.
  9. 9. OO Programming Concepts More?OO Programming - Introduction is licensed under a Creative Commons Attribution- 9NonCommercial-ShareAlike 3.0 Unported License.
  10. 10. Programming OO How I design and code?OO Programming - Introduction is licensed under a Creative Commons Attribution- 10NonCommercial-ShareAlike 3.0 Unported License.
  11. 11. Programming OO • I don’t try to build the perfect solution • Tend to be creative • Brainstorm, sometimes with myself • Implement alternatives • Refactor my code • Get a bird-eye view on the whole system, frequently • Focus my effort on small parts of the system (a Class at a time)OO Programming - Introduction is licensed under a Creative Commons Attribution- 11NonCommercial-ShareAlike 3.0 Unported License.
  12. 12. Programming OO • Know the differences between the interface and the implementation • Think abstractly • Give users of my objects the minimal interface possible • And…. – Know the design principles – Reuse knowledge (Patterns)OO Programming - Introduction is licensed under a Creative Commons Attribution- 12NonCommercial-ShareAlike 3.0 Unported License.
  13. 13. Programming OO • Know the differences between the interface and the implementation • Think abstractly • Give users of my objects the minimal interface possibleOO Programming - Introduction is licensed under a Creative Commons Attribution- 13NonCommercial-ShareAlike 3.0 Unported License.
  14. 14. Programming OO Interface and Implementation interface Services that my objects expose to my object’s clients public class Computer { private PowerSupply powerSupply; public void turnOn() { turnOnPowerSupply(); } public void turnOff() { turnOffPowerSupply(); } private void turnOffPowerSupply() { powerSupply.turnOff(); } private void turnOnPowerSupply() { powerSupply.turnOn(); } }OO Programming - Introduction is licensed under a Creative Commons Attribution- 14NonCommercial-ShareAlike 3.0 Unported License.
  15. 15. Programming OO Interface and Implementation implementation Implementation is how the object realize the services expected by the clients public class Computer { private PowerSupply powerSupply; public void turnOn() { turnOnPowerSupply(); } public void turnOff() { turnOffPowerSupply(); } private void turnOffPowerSupply() { powerSupply.turnOff(); } private void turnOnPowerSupply() { powerSupply.turnOn(); } }OO Programming - Introduction is licensed under a Creative Commons Attribution- 15NonCommercial-ShareAlike 3.0 Unported License.
  16. 16. Programming OO Interface and Implementation • Object’s clients can be other objectsOO Programming - Introduction is licensed under a Creative Commons Attribution- 16NonCommercial-ShareAlike 3.0 Unported License.
  17. 17. Programming OO Interface and Implementation • If an implementation change, clients don’t care, as long as the interface provides them the service they expect public class Computer { public void turnOn() { PowerSupply.on(); } public void turnOff() { PowerSupply.off(); }}OO Programming - Introduction is licensed under a Creative Commons Attribution- 17NonCommercial-ShareAlike 3.0 Unported License.
  18. 18. Programming OO • Know the differences between the interface and the implementation • Think abstractly • Give users of my objects the minimal interface possibleOO Programming - Introduction is licensed under a Creative Commons Attribution- 18NonCommercial-ShareAlike 3.0 Unported License.
  19. 19. Programming OO Think Abstractly • Design interfaces as abstract as possible • Abstraction allows reuse • Foundation of many design guidelinesOO Programming - Introduction is licensed under a Creative Commons Attribution- 19NonCommercial-ShareAlike 3.0 Unported License.
  20. 20. Programming OO Think Abstractly Which interface is more abstract? 1 public class Computer { public void positionDiskHead(int sector) { } public void startDiskRotation() { } public Byte[] readBytes(int endSector) { } public void endDiskRotation() { } } 2 public class Computer { public Byte[] readFromMedium(MediumIdentifier id, ReadInstructions instructions) { return null; } }OO Programming - Introduction is licensed under a Creative Commons Attribution- 20NonCommercial-ShareAlike 3.0 Unported License.
  21. 21. Programming OO Think Abstractly Which interface is more reusable? 1 public class Computer { public void positionDiskHead(int sector) { } public void startDiskRotation() { } public Byte[] readBytes(int endSector) { } public void endDiskRotation() { } } 2 public class Computer { public Byte[] readFromMedium(MediumIdentifier id, ReadInstructions instructions) { return null; } }OO Programming - Introduction is licensed under a Creative Commons Attribution- 21NonCommercial-ShareAlike 3.0 Unported License.
  22. 22. Programming OO • Know the differences between the interface and the implementation • Think abstractly • Give users of my objects the minimal interface possibleOO Programming - Introduction is licensed under a Creative Commons Attribution- 22NonCommercial-ShareAlike 3.0 Unported License.
  23. 23. You Ain’t Programming OO Gonna Need It Minimal interface • Tend to follow the YAGNI principle • Give clients only the strictly needed interfaces • Only add new interfaces if object’s clients really require itOO Programming - Introduction is licensed under a Creative Commons Attribution- 23NonCommercial-ShareAlike 3.0 Unported License.
  24. 24. Programming OO Minimal interface I give this interface to the client of my object. (In reality, I’m giving no1 interface at all) public class Computer { }2 The client asks, “Hey, now what? How do I turn it on?” I give an interface to allow the client to turn on the computer3 public class Computer { public void turnOn() {} }4 The client then asks, “Thanks, but… How do I turn it off?”5 Story goes on…OO Programming - Introduction is licensed under a Creative Commons Attribution- 24NonCommercial-ShareAlike 3.0 Unported License.
  25. 25. OO Concepts • Encapsulation and Data Hiding • Inheritance • Polymorphism • CompositionOO Programming - Introduction is licensed under a Creative Commons Attribution- 25NonCommercial-ShareAlike 3.0 Unported License.
  26. 26. OO Concepts Encapsulation and Data Hiding • Hide implementation details, such as state and behavior pertaining only the implementation of interface. • Give users only access to interfaces public class Computer { private PowerSupply powerSupply; public void turnOn() { What a user see turnOnPowerSupply(); } public void turnOff() { turnOffPowerSupply(); } public class Computer { public void turnOn() { private void turnOffPowerSupply() { turnOnPowerSupply(); powerSupply.turnOff(); } } public void turnOff() { private void turnOnPowerSupply() { turnOffPowerSupply(); powerSupply.turnOn(); } } } }OO Programming - Introduction is licensed under a Creative Commons Attribution- 26NonCommercial-ShareAlike 3.0 Unported License.
  27. 27. OO Concepts Inheritance • Allows me to reuse code by abstracting common behavior and state of a set of classesOO Programming - Introduction is licensed under a Creative Commons Attribution- 27NonCommercial-ShareAlike 3.0 Unported License.
  28. 28. OO Concepts Polymorphism • I can have a have a type that assumes multiple forms ElectronicEquipment computer = new Computer(); ElectronicEquipment phone = new Phone(); computer.turnOn(); phone.turnOn(); • An ElectronicEquipment can assume the form of a Computer or a Phone • A user with a reference to an ElectronicEquipment don’t care if it’s a Computer or a PhoneOO Programming - Introduction is licensed under a Creative Commons Attribution- 28NonCommercial-ShareAlike 3.0 Unported License.
  29. 29. OO Concepts Composition • Can reuse code by factor in common behavior and state to a separate class and use that class as a building block of other classes. – Both Printer and Computer have a PowerSupply and a motherborad.OO Programming - Introduction is licensed under a Creative Commons Attribution- 29NonCommercial-ShareAlike 3.0 Unported License.
  30. 30. João Pereira March 2012OO Design Principles - Introduction by João Miguel Pereira is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.

×