OOP Review and Design Principles
OOP Review and Design Principles
Design Principles
CSE327: Software Engineering
Object-Oriented Programming
Revisited
Key OOP Concepts
Object, Class
Instantiation, Constructors
Encapsulation
Inheritance and Subclasses
Abstraction
Reuse
Polymorphism, Dynamic Binding
Object-Oriented Design and Modeling
2
Object-oriented Programming
Object-orientedprogramming is a paradigm
based on the concept of wrapping pieces of
data, and behavior related to that data, into
special bundles called objects, which are
constructed from a set of “blueprints”, defined
by a programmer, called classes.
3
Object
Definition: a thing that has identity, state, and
behavior
4
Class
Definition: a collection of data (fields/
variables) and methods that operate on that
data
5
Example: Class in UML
6
Instantiation
Object creation
Memory is allocated for the object’s fields as
constructor
a special method invoked when objects are
created
7
Objects: Instance of a Class
8
Encapsulation
A key OO concept: “Information Hiding”
Encapsulation is the ability of an object to
hide parts of its state and behaviors from
other objects, exposing only a limit- ed
interface to the rest of the program.
Key points
The user of an object should have access only to
those methods (or data) that are essential
Unnecessary implementation details should be hidden
from the user
In Java/C++, use classes and access modifiers
(public, private, protected) 9
Inheritance
Inheritance:
programming language feature that allows for the
implicit definition of variables/methods for a class
through an existing class
Subclass relationship
B is a subclass of A
B inherits all definitions (variables/methods) in A
10
Example: Inheritance
11
Abstraction
OOP is about abstraction
Abstraction is a model of a real-world object
12
Example: Abstraction
13
Reuse
Inheritance encourages software reuse
Existing code need not be rewritten
Successful reuse occurs only through careful
14
Polymorphism
Polymorphism is the ability of a program to detect the
real class of an object and call its implementation
even when its real type is unknown in the current
context.
“Many forms”
allow several definitions under a single method name
Example:
“move” means something for a person object but means
something else for a car object
Dynamic binding:
capability of an implementation to distinguish between the
different forms during run-time 15
Example: Polymorphism
16
Class Relationships
17
Interface
Interfaceis a class-like construct contains
only constants and abstract methods.
A class can implement multiple interfaces.
18
Example: Interface
19
Building Complex Systems
From Software Engineering:
complex systems are difficult to manage
Proper use of OOP aids in managing this
complexity
The analysis and design of OO systems
20
Object-Oriented Modeling
UML: Unified Modeling Language
OO Modeling Standard
Booch, Jacobson, Rumbaugh
What is depicted?
Class details and static relationships
System functionality
Object interaction
State transition within an object
21
Some UML Modeling
Techniques
Class Diagrams
Use Cases/Use Case Diagrams
Interaction Diagrams
State Diagrams
22
Example:
Class Diagram
0..1 0..3
Borrower Book
currBorr bk[]
23
Example:
Use Case Diagram
LIBRARY SYSTEM
Facilitate Checkout
Librarian Borrower
Search for Book
Facilitate Return
24
Example:
Interaction Diagram
2: isAvailable()
Checkout
:Book
Screen
1: borrowAllowed()
3: borrowBook()
4: setBorrower()
:Borrower
25
Example:
State Diagram (Book)
start
Reserved
New Borrowed
26
Building an
OO Dynamic Model
Identify use cases
Describe each use case through an
interaction diagram
Derive implied methods (and attributes)
27
Software Design
Principles
28
Features of Good Design
Code Reuse
Don’t invent the wheel again!
Extensibility
Changes are inevitable!
Embrace them not denying them!
29
Design Principles
Encapsulate what varies
On Class level
On Method level
Programto an Interface, not an
Implementation
Depend on abstraction, not on concrete classes
Favor Composition over Inheritance
30
Example: Encapsulation on
Class Level (1)
31
Example: Encapsulation on
Class Level (2)
32
Example: Encapsulation on
Method Level (1)
33
Example: Encapsulation on
Method Level (2)
34
Example: Program to an
Interface
35
Example: Composition over
Inheritance (1)
36
Example: Composition over
Inheritance (2)
37
Design Patterns
38
Design Patterns
Design patterns are typical solutions to
commonly occurring problems in software
design.
They are like pre-made blue- prints that you
39
Components of a Design Pattern
Intent of the pattern briefly describes both
the problem and the solution.
Motivation further explains the problem and