8.5 Object-Oriented Design
Object-oriented design (OOD) is a method of software design that focuses on identifying and organizing the components of a system as "objects." It translates the analysis of the system into a detailed design using object-oriented concepts such as classes, inheritance, polymorphism, and encapsulation. The goal of OOD is to create a blueprint for implementing the system based on the models developed during the object-oriented analysis phase.
1. Analysis to Design
The transition from analysis to design is a critical phase in object-oriented software development. In the analysis phase, we focus on understanding the problem and defining the system requirements. In the design phase, we take those requirements and create a structured solution.
Key steps in this transition include:
Identifying Design Classes: After analyzing the requirements and creating use cases, we identify potential design classes that will realize the system’s functionality.
Refining System Architecture: The design phase involves defining the system's overall structure, including its modules, components, and relationships.
Detailed Design: This phase involves refining the design elements, including classes, interfaces, and data structures, as well as considering design patterns, performance, and scalability.
The design phase builds on the analysis and provides a clear structure for coding.
2. Describing and Elaborating Use Cases
Use cases describe how users interact with a system to accomplish a specific task. In object-oriented design, we take use cases from the analysis phase and elaborate on them to specify the design-level functionality.
Use Case Realization: In design, a use case is realized by determining the classes, objects, and methods that implement the behavior described in the use case. This is often done through sequence diagrams or collaboration diagrams, which show how objects interact over time.
Detailed Behavior: Use cases are expanded with details on how the system will respond to different inputs, and these behaviors are captured using design models like sequence diagrams and activity diagrams.
Elaborating on use cases ensures that the system's functionality is clearly defined before implementation begins.
3. Collaboration Diagram
Collaboration diagrams are used to illustrate how objects interact to accomplish a particular task. They focus on the relationships between objects and the messages exchanged between them to fulfill a use case or system requirement.
Objects: Represented as nodes in the diagram.
Messages: Arrows between objects show the communication between them.
Focus on Object Interaction: Collaboration diagrams help clarify the interaction between objects and emphasize how different components of the system work together to complete a function.
Diagram 1: Purchase Journey
In this example, a collaboration diagram of the purchase journey of a consumer on an e-commerce website is illustrated. It depicts the order of purchase from the ‘Item’ object to the message of ‘order details’ being displayed.
Diagram 2: Admin Panel
This example depicts a collaboration diagram of an admin panel. It shows the user signing in using their unique Id to access an interface of profiles, products, etc.
Collaboration diagrams are particularly useful for representing complex interactions and object relationships in a design.
4. Objects and Patterns
In object-oriented design, objects are instances of classes, and design patterns provide proven solutions to common design problems. Patterns help make the design flexible, reusable, and maintainable.
Objects:
The building blocks of the design. Each object has properties (attributes) and behaviors (methods).
Design Patterns:
In software development, a pattern (or design pattern) is a written document that describes a general solution to a design problem that recurs repeatedly in many projects.
A pattern is a named description of a problem & its solution that can be applied to new contexts.
Software designers adapt the pattern solution to their specific project.
A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a repeated problem.
Pattern elements:
There are four essential elements of the design patterns:
Name: A name that is a meaningful reference to the pattern.
Problem description: Description of the problem.
Solution description: Not a concrete design but a template for a design solution that can be implemented in different ways.
Consequences: The results and trade-offs of applying the pattern. It helps the designer to understand whether a pattern can be effectively applied in particular situation
Some commonly used patterns in object-oriented design include:
Singleton Pattern
Imagine: A printer spooler in an office. Problem: Multiple users cannot manage the printer queue independently. Solution: Use a Singleton pattern to ensure only one PrinterSpooler object exists, managing all print jobs.
Factory Pattern
Imagine: A car manufacturing company producing different car models. Problem: Each model requires different configurations, and manually creating each model is error-prone. Solution: Use a Factory pattern to centralize and standardize the creation process, allowing users to request specific car models (e.g., sedan, SUV) without knowing the internal details.
3. Observer Pattern
Imagine: A stock market tracking system. Problem: Investors want real-time updates on stock prices. Solution: Use an Observer pattern where the stock market acts as the subject, and investors are observers automatically notified of price changes.
4. Strategy Pattern
Imagine: A navigation app. Problem: Users want to choose between different routes (e.g., fastest, shortest, scenic). Solution: Use a Strategy pattern to encapsulate different algorithms for route calculation and allow users to select the desired one dynamically.
5. Decorator Pattern
Imagine: A coffee shop offering customizable drinks. Problem: Adding options like extra sugar, milk, or syrup increases complexity in creating different drink types. Solution: Use a Decorator pattern to dynamically add options to the base drink without modifying the core structure.
6. Adapter Pattern
Imagine: A laptop with a USB-C port, but the user has a traditional USB-A device. Problem: Incompatible interfaces between devices. Solution: Use an Adapter pattern to create a USB-C to USB-A converter, allowing compatibility.
7. Command Pattern
Imagine: A remote control for a smart home system. Problem: Users want to execute, undo, and queue various commands (e.g., turning on lights, adjusting temperature). Solution: Use a Command pattern to encapsulate each request as an object, making it easier to handle complex operations.
Using design patterns in object-oriented design helps create solutions that are easy to understand, modify, and maintain.
5. Determining Visibility
Visibility in object-oriented design refers to the accessibility of a class’s attributes and methods to other classes or objects. It helps determine how objects interact with each other and restricts access to the internal workings of an object to maintain encapsulation.
Access Modifiers: In most object-oriented languages, visibility is controlled using access modifiers such as:
Public: Attributes and methods are accessible from anywhere.
Private: Attributes and methods are only accessible within the class.
Protected: Attributes and methods are accessible within the class and its subclasses.
Encapsulation: Visibility rules are part of the principle of encapsulation, which hides the internal details of an object and only exposes necessary interfaces.
Determining the appropriate visibility for each class member ensures that objects interact correctly while protecting internal details.
6. Class Diagram
A class diagram is one of the most important diagrams in object-oriented design. It represents the structure of a system by showing the system's classes, their attributes, methods, and the relationships between them.
Classes: Represented as rectangles containing the class name, attributes, and methods.
Attributes: The properties of the class (e.g., a Person class might have attributes such as name, age, and address).
Methods: The behaviors or functions that the class can perform (e.g., a Person class might have a method like changeAddress()).
Relationships: The relationships between classes are shown using different types of associations:
Association: A basic relationship between two classes.
Inheritance: A subclass inherits attributes and methods from a superclass.
Aggregation: A whole-part relationship between classes.
Composition: A stronger version of aggregation where the child object cannot exist without the parent object.
Class diagrams provide a high-level view of the system’s static structure and form the basis for implementing the system. This was just the overview of the class diagram, we already studied about the class diagrams on the topic 8.4.
Conclusion
Object-oriented design transforms the analysis models into detailed designs that guide the development of the system. The key activities in object-oriented design include refining use cases, identifying and organizing objects, applying design patterns, and determining the appropriate visibility for each class member. Class diagrams are used to define the static structure of the system, while collaboration diagrams help visualize object interactions. By adhering to object-oriented principles and using design patterns, developers can create maintainable and flexible software systems.
Last updated