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)
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
OO programming introduction
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. OO Programming- Introduction
Refreshing OO programming concepts
OO Programming - Introduction is licensed under a Creative Commons Attribution- 2
NonCommercial-ShareAlike 3.0 Unported License.
3. OO Programming Concepts
Why do I need objects ?
OO Programming - Introduction is licensed under a Creative Commons Attribution- 3
NonCommercial-ShareAlike 3.0 Unported License.
4. OO Programming Concepts
• I can easily model real world concepts.
Car
OO Programming - Introduction is licensed under a Creative Commons Attribution- 4
NonCommercial-ShareAlike 3.0 Unported License.
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
behavior
OO Programming - Introduction is licensed under a Creative Commons Attribution- 5
NonCommercial-ShareAlike 3.0 Unported License.
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 behavior
OO Programming - Introduction is licensed under a Creative Commons Attribution- 6
NonCommercial-ShareAlike 3.0 Unported License.
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 Program
OO Programming - Introduction is licensed under a Creative Commons Attribution- 7
NonCommercial-ShareAlike 3.0 Unported License.
8. OO Programming Concepts
• I can reuse objects to build different
systems
Wheel
Wheel Wheel
Seat
Seat
Objects Bike OO Program
OO Programming - Introduction is licensed under a Creative Commons Attribution- 8
NonCommercial-ShareAlike 3.0 Unported License.
9. OO Programming Concepts
More?
OO Programming - Introduction is licensed under a Creative Commons Attribution- 9
NonCommercial-ShareAlike 3.0 Unported License.
10. Programming OO
How I design and code?
OO Programming - Introduction is licensed under a Creative Commons Attribution- 10
NonCommercial-ShareAlike 3.0 Unported License.
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- 11
NonCommercial-ShareAlike 3.0 Unported License.
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- 12
NonCommercial-ShareAlike 3.0 Unported License.
13. Programming OO
• Know the differences between the interface
and the implementation
• Think abstractly
• Give users of my objects the minimal interface
possible
OO Programming - Introduction is licensed under a Creative Commons Attribution- 13
NonCommercial-ShareAlike 3.0 Unported License.
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- 14
NonCommercial-ShareAlike 3.0 Unported License.
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- 15
NonCommercial-ShareAlike 3.0 Unported License.
16. Programming OO
Interface and Implementation
• Object’s clients can be other objects
OO Programming - Introduction is licensed under a Creative Commons Attribution- 16
NonCommercial-ShareAlike 3.0 Unported License.
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- 17
NonCommercial-ShareAlike 3.0 Unported License.
18. Programming OO
• Know the differences between the interface
and the implementation
• Think abstractly
• Give users of my objects the minimal interface
possible
OO Programming - Introduction is licensed under a Creative Commons Attribution- 18
NonCommercial-ShareAlike 3.0 Unported License.
19. Programming OO
Think Abstractly
• Design interfaces as abstract as
possible
• Abstraction allows reuse
• Foundation of many design
guidelines
OO Programming - Introduction is licensed under a Creative Commons Attribution- 19
NonCommercial-ShareAlike 3.0 Unported License.
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- 20
NonCommercial-ShareAlike 3.0 Unported License.
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- 21
NonCommercial-ShareAlike 3.0 Unported License.
22. Programming OO
• Know the differences between the interface
and the implementation
• Think abstractly
• Give users of my objects the minimal
interface possible
OO Programming - Introduction is licensed under a Creative Commons Attribution- 22
NonCommercial-ShareAlike 3.0 Unported License.
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 it
OO Programming - Introduction is licensed under a Creative Commons Attribution- 23
NonCommercial-ShareAlike 3.0 Unported License.
24. Programming OO
Minimal interface
I give this interface to the client of my object. (In reality, I’m giving no
1 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 computer
3 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- 24
NonCommercial-ShareAlike 3.0 Unported License.
25. OO Concepts
• Encapsulation and Data Hiding
• Inheritance
• Polymorphism
• Composition
OO Programming - Introduction is licensed under a Creative Commons Attribution- 25
NonCommercial-ShareAlike 3.0 Unported License.
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- 26
NonCommercial-ShareAlike 3.0 Unported License.
27. OO Concepts
Inheritance
• Allows me to reuse code by abstracting
common behavior and state of a set of classes
OO Programming - Introduction is licensed under a Creative Commons Attribution- 27
NonCommercial-ShareAlike 3.0 Unported License.
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 Phone
OO Programming - Introduction is licensed under a Creative Commons Attribution- 28
NonCommercial-ShareAlike 3.0 Unported License.
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- 29
NonCommercial-ShareAlike 3.0 Unported License.
30. João Pereira
March 2012
OO Design Principles - Introduction by João Miguel Pereira is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported License.