object- oriented design principles
DESCRIPTION
Object- oriented Design Principles. The pyramid of OO. Abstract/ Philosophical. Practical. Object-orientation is good!. Object-orientation is good?. Object-orientation is good (?). Why are we learning about object-orientation in the first place? - PowerPoint PPT PresentationTRANSCRIPT
Object- oriented Design Principles
PBA WEB – BEWP 2
The pyramid of OO
OO Paradigm
OO Concepts
OO Principles
Design Patterns
Practical
Abstract/Philosophical
PBA WEB – BEWP 3
Object-orientation is good!
Object-orientation is good?
PBA WEB – BEWP 4
PBA WEB – BEWP 5
Object-orientation is good (?)
• Why are we learning about object-orientation in the first place?
• Other ”paradigms” for programming exist, for instance functional programming and logic programming
• However, OO is currently the dominating paradigm for programming – more or less an industry standard
PBA WEB – BEWP 6
Object-orientation is good!
• Object-orientation has proven to be a friutful way of connecting real life and software development
• However, there are certain ”rules” to obey when playing the OO-game…
• At the top, we rely on a few Object-Oriented concepts
PBA WEB – BEWP 7
Object-oriented concepts
• Abstraction• Encapsulation• Polymorphism• Inheritance• (Composition)
PBA WEB – BEWP 8
Abstraction
• The idea that we focus only on the essence of a concept
• Inessential details are ”abstracted away”• Abstraction works at multiple levels• Can be a challenge to find the appropriate
level of abstraction• Abstractions are manifested through
interfaces, which define behaviors
PBA WEB – BEWP 9
Encapsulation
• The idea not to expose details about how behaviors are achieved
• Concepts are considered black boxes• This enables us to change how behaviors
are achieved, without affecting the user of a particular concept
PBA WEB – BEWP 10
Polymorphism
• The idea that concrete instances of a behavior can take many forms
• All animals can eat – but do so in very different ways…
• Eating is polymorphic behavior – it can take many forms, but has the same essence
• Allows us to focus on the essence of the behavoir, neglecting actual behaviors
PBA WEB – BEWP 11
Inheritance
• The idea that concepts can inherit behaviors from other, more general concepts
• Allows us to reuse behaviors that have previously been defined
• We can easily define hierarchies of related concepts, only needing to add genuinely new behaviors
PBA WEB – BEWP 12
Composition
• The idea that new concepts can be com-posed by combining existing concepts
• A supplement/alternative to inheritance• A different approach to reuse• Composition helps us realise has-a
relations between concepts, inheritance is used for is-a relations
PBA WEB – BEWP 13
Fine, but…
• The OO-concepts are nice, but not particularly operational
• We need something more concrete
• The OO-principles provide more specific guidelines
PBA WEB – BEWP 14
The Object-oriented principles1. Encapsulate the aspects of your application that vary
2. Favor composition over inheritance
3. Program to an interface, not an implementation
4. Strive for loose couplings between objects that interact
5. Make classes open for extension, closed for modification
6. Depend on abstractions, not concrete classes
7. Only talk to your closest friends
8. Don’t call us, we’ll call you
9. A class should only have one reason to change
OO principles
• Where do these principles come from…?• Not from ”proofs” or theory…• Condensed best practices from real life• Principles are to some extent overlapping
PBA WEB – BEWP 15
PBA WEB – BEWP 16
OO principles
• ”Encapsulate the aspects of your applica-tion that vary”– Promotes reusability– When designing classes for a system of
concepts, put the varying elements into separate classes
– Can be realised both through inheritance and composition, often using interfaces
PBA WEB – BEWP 17
OO principles
• ”Program to an interface, not an imple-mentation”– An essential idea; if we know the interface, we
do not need to know any details about a specific implementation (weak coupling)
– Allows us the change the implementation without affecting the user
– This is more or less encapsulation
PBA WEB – BEWP 18
OO principles
• ”Make classes open for extension, closed for modification”– Very important principle!– Once we have a well-documented, well-tested
class, we should not change it!– If we need more functionality, achieve it
through extension, using inheritance and/or composition
– Keep classes as ”pure” as possible
PBA WEB – BEWP 19
OO principles
• ”Only talk to your closest friends”– Hmm, what does that mean…?– A typical application architecture divides
classes into a number of layers– Classes in one layer should only know
classes in the next layer (loose couplings)– Avoid reliance on the specific composition of
a class
PBA WEB – BEWP 20
OO principles
• What is best, and why…?
double temp = station.GetThermometer().GetTemperature();
double temp = station.GetStationTemperature();
• We should not rely on specifics about how a Station is implemented
• Only one ”.” in a line of code…
PBA WEB – BEWP 21
OO principles
• ”Don’t call us, we’ll call you”– The Hollywood Agent principle…– A more advanced principle; instead of imple-
menting an algorithm using a lot of calls to generic classes, implement a generic algo-rithm with ”plugins” for specialised behavior
– Also known as Inversion of Control– ”Encapsulate what varies…”
PBA WEB – BEWP 22
Beyond principles
• We can use these OO principles directly when developing software
• Next step is Design Patterns, which describe designs for solving common problems, relying on the OO principles