0% found this document useful (0 votes)
6 views41 pages

OOP Review and Design Principles

The document provides an overview of Object-Oriented Programming (OOP) principles, including key concepts such as classes, objects, encapsulation, inheritance, abstraction, reuse, and polymorphism. It emphasizes the importance of proper design principles and modeling techniques, particularly using UML, to manage complexity in software engineering. Additionally, it discusses design patterns as solutions to common software design problems, classified into creational, structural, and behavioral patterns.

Uploaded by

fazle.tihum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views41 pages

OOP Review and Design Principles

The document provides an overview of Object-Oriented Programming (OOP) principles, including key concepts such as classes, objects, encapsulation, inheritance, abstraction, reuse, and polymorphism. It emphasizes the importance of proper design principles and modeling techniques, particularly using UML, to manage complexity in software engineering. Additionally, it discusses design patterns as solutions to common software design problems, classified into creational, structural, and behavioral patterns.

Uploaded by

fazle.tihum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

OOP Review and

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

 identity: a distinguished instance of a class


 state: collection of values for its variables
 behavior: capability to execute methods

* variables and methods are defined in a class

4
Class
Definition: a collection of data (fields/
variables) and methods that operate on that
data

 define the contents/capabilities of the instances


(objects) of the class
 a class can be viewed as a factory for objects
 a class defines a recipe for its objects

5
Example: Class in UML

6
Instantiation
 Object creation
 Memory is allocated for the object’s fields as

defined in the class


 Initialization is specified through a

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

or phenomenon, limited to a specific context,


which represents all details relevant to this
context with high accuracy and omits all the
rest.

12
Example: Abstraction

13
Reuse
 Inheritance encourages software reuse
 Existing code need not be rewritten
 Successful reuse occurs only through careful

planning and design


 when defining classes, anticipate future
modifications and extensions

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

require corresponding modeling techniques

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[]

public class Borrower { public class Book {


Book bk[]; Borrower currBorr;
… …
public Borrower() { }
bk = new Book[3];
}
}

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

Librarian activates Borrower returns book


book as available Available

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

can customize to solve a recurring design


problem in your code.

39
Components of a Design Pattern
 Intent of the pattern briefly describes both
the problem and the solution.
 Motivation further explains the problem and

the solution the pattern makes possible.


 Structure of classes shows each part of the

pattern and how they are related.


 Code example in one of the popular

programming languages makes it easier to


grasp the idea behind the pattern.
40
Classification of Design
Patterns
 Creational patterns provide object creation
mechanisms that increase flexibility and
reuse of existing code.
 Structural patterns explain how to assemble

objects and classes into larger structures,


while keeping the structures flexible and
efficient.
 Behavioral patterns take care of effective

communication and the assignment of


responsibilities between objects. 41

You might also like