Object-Oriented Design (OOD) is a programming approach based on objects and data rather than actions and logic. It emphasizes the use of reusable software components and aims to model real-world entities and their relationships. Here’s a detailed look at the core principles and components of OOD:
Core Principles of OOD
- Encapsulation
- Definition: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, known as a class.
- Benefits: Encapsulation helps in data hiding, which protects the internal state of the object from unintended or harmful modifications.
- Abstraction
- Definition: Abstraction involves simplifying complex reality by modeling classes appropriate to the problem, and working at the most relevant level of inheritance for a particular aspect of the problem.
- Benefits: It reduces complexity by allowing the designer to focus on the interactions at the higher level without needing to consider lower-level details.
- Inheritance
- Definition: Inheritance is a mechanism by which one class (child or derived class) inherits attributes and methods from another class (parent or base class).
- Benefits: Inheritance promotes code reuse and establishes a natural hierarchy between classes.
- Polymorphism
- Definition: Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name.
- Benefits: It provides flexibility to use objects of different classes interchangeably and to define methods that process objects differently based on their data type or class.
Components of OOD
- Classes
- Definition: A class is a blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class can use.
- Example: A
Car
class with attributes likecolor
,make
, andmodel
, and methods likedrive()
andbrake()
.
- Objects
- Definition: An object is an instance of a class. It represents a specific entity and has a state and behavior defined by the class.
- Example: A specific car, like a red Toyota Corolla, which is an object of the
Car
class.
- Methods
- Definition: Methods are functions defined within a class that describe the behaviors of the objects of the class.
- Example: The
drive()
method in theCar
class that defines how a car drives.
- Attributes
- Definition: Attributes are data stored inside an object or class that represent the object’s state.
- Example: The
color
attribute in theCar
class.
Design Patterns in OOD
Design patterns are typical solutions to common problems in software design. Here are some widely-used patterns:
- Creational Patterns
- Singleton: Ensures a class has only one instance and provides a global point of access to it.
- Factory Method: Defines an interface for creating an object but lets subclasses alter the type of objects that will be created.
- Structural Patterns
- Adapter: Allows objects with incompatible interfaces to work together.
- Decorator: Adds behavior to objects dynamically without affecting the behavior of other objects from the same class.
- Behavioral Patterns
- Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Example: Designing a Simple Library System
- Identify Classes
- Book, Member, Librarian, Loan
- Define Relationships
- A
Member
can borrowBooks
. - A
Librarian
manages theLoans
.
- A
- Create Class Diagrams
- Book
- Attributes: title, author, ISBN
- Methods: checkout(), return()
- Member
- Attributes: name, memberID
- Methods: borrowBook(), returnBook()
- Librarian
- Attributes: name, employeeID
- Methods: addBook(), removeBook()
- Loan
- Attributes: book, member, loanDate, returnDate
- Methods: calculateFine()
- Book
Example Code Snippet
class Book:
def __init__(self, title, author, ISBN):
self.title = title
self.author = author
self.ISBN = ISBN
self.is_checked_out = False
def checkout(self):
if not self.is_checked_out:
self.is_checked_out = True
print(f”{self.title} has been checked out.”)
else:
print(f”{self.title} is already checked out.”)
def return_book(self):
if self.is_checked_out:
self.is_checked_out = False
print(f”{self.title} has been returned.”)
else:
print(f”{self.title} was not checked out.”)
class Member:
def __init__(self, name, memberID):
self.name = name
self.memberID = memberID
self.borrowed_books = []
def borrow_book(self, book):
if not book.is_checked_out:
book.checkout()
self.borrowed_books.append(book)
else:
print(f”{book.title} is already checked out.”)
def return_book(self, book):
if book in self.borrowed_books:
book.return_book()
self.borrowed_books.remove(book)
else:
print(f”{self.name} did not borrow {book.title}.”)
# Example usage
book1 = Book(“1984”, “George Orwell”, “123456789”)
member1 = Member(“Alice”, “M001”)
member1.borrow_book(book1)
member1.return_book(book1)