Unit 1&2 OOSD
Unit 1&2 OOSD
Unit 1&2 OOSD
Object Orientation
Object Orientation is a programming paradigm that revolves around the concept of objects and
classes. It is a way of designing and organizing code that models real-world objects and systems.
The core idea is to create objects that have properties and behaviors, which can interact with each
other to achieve a specific goal.
Object Identity
Object Identity refers to the unique existence of an object in a system. Each object has its own set
of attributes (data) and methods (functions) that define its behavior. Object identity is essential in
object-oriented programming as it allows objects to be distinguished from one another.
Encapsulation
Encapsulation is the concept of bundling data and methods that operate on that data within a single
unit, called a class. This helps to hide the implementation details of an object from the outside
world, making it harder for other objects to access or modify its internal state directly.
Encapsulation promotes data hiding and abstraction.
Information Hiding
Polymorphism
Polymorphism is the ability of an object to take on multiple forms. This can be achieved through
method overriding or method overloading. Polymorphism allows objects of different classes to be
treated as objects of a common superclass, making it possible to write more generic and flexible
code.
Importance of Modelling
Modelling is an essential step in software development as it helps to create a conceptual
representation of the system being designed. Modelling allows developers to identify the key
components, relationships, and behaviors of the system, making it easier to design and implement
the system.
Principles of Modelling
Object-Oriented Modelling
Introduction to UML
UML (Unified Modelling Language) is a standardized language used for creating models of
systems. It provides a set of graphical notations and rules for creating diagrams that represent the
structure and behavior of systems.
Architecture
Architecture refers to the high-level design of a system, including the components, relationships,
and interactions between them. In object-oriented system design, architecture is critical in defining
the overall structure and organization of the system.
Object-Oriented Design (OOD) is crucial in system design due to several key reasons:
o Modularity: OOD breaks down complex systems into manageable, self-contained objects,
making development and maintenance more straightforward.
o Reusability: Objects and classes can be reused across different projects, reducing
redundancy and saving time.
o Scalability: OOD supports system expansion by allowing new objects to be easily
integrated without disrupting existing functionality.
o Maintainability: Encapsulation of data and behavior within objects simplifies
troubleshooting and updates, enhancing system reliability.
o Clear Mapping to Real-World Problems: By modeling software after real-world entities and
their interactions, OOD makes systems more intuitive and easier to conceptualize.
o Flexibility and Extensibility: Through inheritance and polymorphism, OOD allows for
extending and adapting systems with minimal changes, accommodating future requirements
efficiently.
Object-Oriented Design (OOD) is governed by several key principles that help create robust,
maintainable, and scalable systems:
o Encapsulation: Bundling data with methods that operate on the data, restricting direct
access to some components and protecting object integrity.
o Abstraction: Simplifying complex systems by modeling classes appropriate to the
problem domain, highlighting essential features while hiding unnecessary details.
o Inheritance: Establishing a hierarchy between classes, allowing derived classes to
inherit properties and behaviors from base classes, promoting code reuse and extension.
o Polymorphism: Enabling objects to be treated as instances of their parent class, allowing
one interface to be used for a general class of actions, improving flexibility and
integration.
o Composition: Building complex objects by combining simpler ones, promoting reuse
and flexible designs.
o SOLID Principles:
o Single Responsibility Principle (SRP): A class should have one, and only one, reason to
change.
o Open/Closed Principle (OCP): Classes should be open for extension but closed for
modification.
o Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types
without altering the correctness of the program.
o Interface Segregation Principle (ISP): Clients should not be forced to depend on
interfaces they do not use.
o Dependency Inversion Principle (DIP): High-level modules should not depend on low-
level modules. Both should depend on abstractions.
Object-Oriented Design (OOD) involves several core concepts that facilitate the creation of robust,
scalable, and maintainable software.
1. Encapsulation
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data
into a single unit called a class. It restricts direct access to some of the object’s components, which
is a means of preventing accidental interference and misuse of the data.
2. Abstraction
Abstraction involves hiding the complex implementation details and showing only the essential
features of the object. This helps in managing complexity by allowing the designer to focus on the
interactions at a higher level.
3. Inheritance
Inheritance is a mechanism where a new class inherits properties and behaviors (methods) from an
existing class. This promotes code reuse and establishes a natural hierarchy between classes.
4. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common super class.
It enables a single interface to represent different underlying data types and allows m5.
5. Composition
Composition is a design principle where a class is composed of one or more objects of other
classes, rather than inheriting from them. This promotes flexibility and reusability.ethods to use
objects of various types.
Important Questions
Q 1. How does Object-Oriented Design (OOD) differ from Object-Oriented Programming (OOP)?
OOD is the process of planning and designing a system of interacting objects to solve a software
problem. It focuses on defining the structure and behavior of objects and their interactions. OOP,
on the other hand, is the implementation of the design using an object-oriented programming
language (such as C++, Java, or Python). While OOD deals with the design phase of software
development, OOP deals with the coding and implementation phase.
Modularity: By dividing the system into discrete classes, each responsible for specific
functionality, it becomes easier to understand, modify, and extend the system.
Encapsulation: By restricting access to the internal state of objects and exposing only necessary
interfaces, it reduces the risk of unintended interference and makes the system more robust.
Reuse: Inheritance and polymorphism promote code reuse, reducing redundancy and making it
easier to update and maintain the system.
Clear Design: Abstraction allows designers to focus on high-level design, making the system easier
to understand and reason about.
Unified Modeling Language (UML) plays a critical role in Object-Oriented Design by providing a
standardized way to visualize the design of a system. UML includes various types of diagrams,
each serving different purposes
Handling design changes in an Object-Oriented Design involves several best practices to ensure the
system remains flexible and maintainable:
o Refactoring: Regularly refactor the code to improve its structure without changing its
external behavior. This helps in accommodating new requirements and improving code
quality.
o Version Control: Use version control systems to manage changes in the codebase. This
allows tracking of changes, collaboration among team members, and reverting to previous
versions if needed.
o Design Patterns: Apply appropriate design patterns that provide tested solutions for
common design problems and enhance the flexibility of the system.
Unified Modeling Language (UML) is a standardized visual modeling language used in the field of
software engineering to provide a general-purpose, developmental, and intuitive way to visualize
the design of a system. UML helps in specifying, visualizing, constructing, and documenting the
artifacts of software systems.
The Object Management Group (OMG) adopted Unified Modelling Language as a standard in
1997. It’s been managed by OMG ever since.
The International Organization for Standardization (ISO) published UML as an approved standard
in 2005. UML has been revised over the years and is reviewed periodically.
Complex applications need collaboration and planning from multiple teams and hence require a
clear and concise way to communicate amongst them.
Businessmen do not understand code. So UML becomes essential to communicate with non-
programmers about essential requirements, functionalities, and processes of the system.
A lot of time is saved down the line when teams can visualize processes, user interactions, and the
static structure of the system.
UML is linked with object-oriented design and analysis. UML makes use of elements and forms
associations between them to form diagrams. Diagrams in UML can be broadly classified as:
Structural Diagrams
Structural UML diagrams illustrate the organization of a system by depicting its components, such
as classes, objects, and packages. They represent the elements that make up the system and the
relationships between them.
1. Class Diagram
Class diagrams are the main building blocks of every object-oriented method.
The class diagram can be used to show the classes, relationships, interface, association, and
collaboration. UML is standardized in class diagrams.
Since classes are the building block of an application that is based on OOPs, the class diagram has
an appropriate structure to represent the classes, inheritance, relationships, and everything that
OOPs have in their context.
class notation is a graphical representation used to depict classes and their relationships in object-
oriented modeling.
Class Name:
The name of the class is typically written in the top compartment of the class box and is centered
and bold.
Attributes:
Attributes, also known as properties or fields, represent the data members of the class. They are
listed in the second compartment of the class box and often include the visibility (e.g., public,
private) and the data type of each attribute.
Methods:
Methods, also known as functions or operations, represent the behavior or functionality of the
class. They are listed in the third compartment of the class box and include the visibility (e.g.,
public, private), return type, and parameters of each method.
Visibility Notation:
Visibility notations indicate the access level of attributes and methods. Common visibility notations
include:
+ for public (visible to all classes)
2. Package Diagram
A package diagram is a type of Unified Modeling Language (UML) diagram mainly used to
represent the organization and the structure of a system in the form of packages.
A package is used as a container to organize the elements present in the system into a more
manageable unit.
It is very useful to represent the system’s architecture and design as a cohesive unit and concise
manner.
3. Object Diagram
An Object Diagram can be referred to as a screenshot of the instances in a system and the
relationship that exists between them.
Since object diagrams depict behaviour when objects have been instantiated, we are able to study
the behavior of the system at a particular instant.
Object diagrams are vital to portray and understand functional requirements of a system. In other
words, “An object diagram in the Unified Modeling Language (UML), is a diagram that shows a
complete or partial view of the structure of a modelled system at a specific time.
4. Component Diagram
Component diagrams are used to represent how the physical components in a system have been
organized. We use them for modelling implementation details.
Component Diagrams depict the structural relationship between software system elements and help
us in understanding if functional requirements have been covered by planned development.
Component Diagrams become essential to use when we design and build complex systems.
Interfaces are used by components of the system to communicate with each other.
A composite structure diagram represents relationship between parts and their configuration which
determine how the classifier (class, a component, or a deployment node) behaves.
They represent internal structure of a structured classifier making the use of parts, ports, and
connectors.
They are similar to class diagrams except they represent individual parts in detail as compared to
the entire class.
6. Deployment diagram
Deployment Diagrams are used to represent system hardware and its software.It tells us what
hardware components exist and what software components run on them.
They are primarily used when a software is being used, distributed or deployed over multiple
machines with different configurations.
A state diagram is used to represent the condition of the system or part of the system at finite
instances of time. It’s a behavioral diagram and it represents the behavior using finite state
transitions.
State diagrams are also referred to as State machines and State-chart Diagrams
These terms are often used interchangeably. So simply, a state diagram is used to model the
dynamic behavior of a class in response to time and changing external stimuli.
2. Activity Diagrams
We use Activity Diagrams to illustrate the flow of control in a system. We can also use an activity
diagram to refer to the steps involved in the execution of a use case.
We model sequential and concurrent activities using activity diagrams. So, we basically depict
workflows visually using an activity diagram.
An activity diagram focuses on condition of flow and the sequence in which it happens.
Use Case Diagrams are used to depict the functionality of a system or a part of a system. They are
widely used to illustrate the functional requirements of the system and its interaction with external
agents(actors).
A use case is basically a diagram representing different scenarios where the system can be used.
A use case diagram gives us a high level view of what the system or a part of the system does
without going into implementation details.
4. Sequence Diagram
A sequence diagram simply depicts interaction between objects in a sequential order i.e. the order
in which these interactions take place.
We can also use the terms event diagrams or event scenarios to refer to a sequence diagram.
Sequence diagrams describe how and in what order the objects in a system function.
These diagrams are widely used by businessmen and software developers to document and
understand requirements for new and existing systems.
5. Communication Diagram
We can represent similar information using Sequence diagrams, however communication diagrams
represent objects and links in a free form.
6. Timing Diagram
Timing Diagram are a special form of Sequence diagrams which are used to depict the behavior of
objects over a time frame. We use them to show time and duration constraints which govern
changes in states and behavior of objects.
Unit 2
I. BASIC STRUCTURAL MODELING
1. Classes:
A class is a description of a set of objects that share the same attributes, operations, relationships,
and semantics. Graphically, a class is rendered as a rectangle.
Names
Every class must have a name that distinguishes it from other classes. A name is a textual string.
That name alone is known as a simple name; a qualified name is the class name prefixed by the
name of the package in which that class lives. A class may be drawn showing only its name
Attributes
An attribute is a named property of a class that describes a range of values that instances of the
property may hold. A class may have any number of attributes or no attributes at all. An attribute
represents some property of the thing you are modeling that is shared by all objects of that class.
For example, every wall has a height, width, and thickness; you might model your customers in
such a way that each has a name, address, phone number, and date of birth
Operations
An operation is the implementation of a service that can be requested from any object of the
class to affect behavior. In other words, an operation is an abstraction of something you can do to
an object that is shared by all objects of that class. A class may have any number of operations or
no operations at all. For example, in a windowing library such as the one found in Java's awt
package, all objects of the class Rectangle can be moved, resized, or queried for their properties.
Often (but not always), invoking an operation on an object changes the object's data or state.
Graphically, operations are listed in a compartment just below the class attributes. Operations
may be drawn showing only their names
You can specify an operation by stating its signature, which includes the name, type, and default
value of all parameters and (in the case of functions) a return type
When drawing a class, you don't have to show every attribute and every operation at once. In
fact, in most cases, you can't (there are too many of them to put in one figure) and you probably
shouldn't (only a subset of these attributes and operations are likely to be relevant to a specific
view). For these reasons, you can elide a class, meaning that you can choose to show only some
or none of a class's attributes and operations. You can indicate that there are more attributes or
properties than shown by ending each list with an ellipsis ("...").
Responsibilities
A responsibility is a contract or an obligation of a class. When you create a class, you are making
a statement that all objects of that class have the same kind of state and the same kind of
behavior. At a more abstract level, these corresponding attributes and operations are just the
features by which the class's responsibilities are carried out. A Wall class is responsible for
knowing about height, width, and thickness; a FraudAgent class, as you might find in a credit
card application, is responsible for processing orders and determining if they are legitimate,
suspect, or fraudulent; a TemperatureSensor class is responsible for measuring temperature and
raising an alarm if the temperature reaches a certain point.
You'll use classes most commonly to model abstractions that are drawn from the problem you
are trying to solve or from the technology you are using to implement a solution to that problem.
Each of these abstractions is a part of the vocabulary of your system, meaning that, together, they
represent the things that are important to users and to implementers.
To model the vocabulary of a system,
Identify those things that users or implementers use to describe the problem or solution.
Use CRC cards and use case-based analysis to help find these abstractions.
For each abstraction, identify a set of responsibilities. Make sure that each class is crisply
defined and that there is a good balance of responsibilities among all your classes.
Provide the attributes and operations that are needed to carry out these responsibilities for
each class.
A set of classes drawn from a retail system, including Customer, Order, and Product. This figure
includes a few other related abstractions drawn from the vocabulary of the problem, such as
Shipment (used to track orders), Invoice (used to bill orders), and Warehouse (where products
are located prior to shipment). There is also one solution-related abstraction, TRansaction, which
applies to orders and shipments.
Once you start modeling more than just a handful of classes, you will want to be sure that your
abstractions provide a balanced set of responsibilities.
Identify a set of classes that work together closely to carry out some behavior.
Identify a set of responsibilities for each of these classes.
Look at this set of classes as a whole, split classes that have too many responsibilities into
smaller abstractions, collapse tiny classes that have trivial responsibilities into larger
ones, and reallocate responsibilities so that each abstraction reasonably stands on its own.
Consider the ways in which those classes collaborate with one another, and redistribute
their responsibilities accordingly so that no class within a collaboration does too much or
too little.
2.Relationships:
Dependencies
A dependency is a relationship that states that one thing (for example, class Window) uses the
information and services of another thing (for example, class Event), but not necessarily the
reverse. Graphically, a dependency is rendered as a dashed directed line, directed to the thing
being depended on. Choose dependencies when you want to show one thing using another.
Generalizations
Associations
An association is a structural relationship that specifies that objects of one thing are connected to
objects of another. Given an association connecting two classes, you can relate objects of one
class to objects of the other class. It's quite legal to have both ends of an association circle back
to the same class. This means that, given an object of the class, you can link to other objects of
the same class. An association that connects exactly two classes is called a binary association.
Although it's not as common, you can have associations that connect more than two classes;
these are called n-ary associations.
Beyond this basic form, there are four adornments that apply to associations.
Name
An association can have a name, and you use that name to describe the nature of the relationship.
So that there is no ambiguity about its meaning, you can give a direction to the name by
providing a direction triangle that points in the direction you intend to read the name.
Role
When a class participates in an association, it has a specific role that it plays in that relationship;
a role is just the face the class at the far end of the association presents to the class at the near end
of the association. You can explicitly name the role a class plays in an association. The role
played by an end of an association is called an end name (in UML1, it was called a role name).
the class Person playing the role of employee is associated with the class Company playing the
role of employer.
Multiplicity
The number of objects must be in the given range. You can show a multiplicity of exactly one
(1), zero or one (0..1), many (0..*), or one or more (1..*). You can give an integer range (such as
2..5). You can even state an exact number (for example, 3, which is equivalent to 3..3).
Aggregation
A plain association between two classes represents a structural relationship between peers,
meaning that both classes are conceptually at the same level, no one more important than the
other. Sometimes you will want to model a "whole/part" relationship, in which one class
represents a larger thing (the "whole"), which consists of smaller things (the "parts"). This kind
of relationship is called aggregation, which represents a "has-a" relationship
A common kind of dependency relationship is the connection between a class that uses another
class as a parameter to an operation.
Create a dependency pointing from the class with the operation to the class used as a
parameter in the operation.
a set of classes drawn from a system that manages the assignment of students and instructors to
courses in a university. This figure shows a dependency from CourseSchedule to Course,
because Course is used in both the add and remove operations of CourseSchedule.
Modeling Single Inheritance
In modeling the vocabulary of your system, you will often run across classes that are structurally
or behaviorally similar to others. You could model each of these as distinct and unrelated
abstractions. A better way would be to extract any common structural and behavioral features
and place them in more-general classes from which the specialized ones inherit.
Given a set of classes, look for responsibilities, attributes, and operations that are
common to two or more classes.
Elevate these common responsibilities, attributes, and operations to a more general class.
If necessary, create a new class to which you can assign these elements (but be careful
about introducing too many levels).
Specify that the more-specific classes inherit from the more-general class by placing a
generalization relationship that is drawn from each specialized class to its more-general
parent.
When you model with dependencies or generalization relationships, you may be modeling
classes that represent different levels of importance or different levels of abstraction. Given a
dependency between two classes, one class depends on another but the other class has no
knowledge of the one.
For each pair of classes, if you need to navigate from objects of one to objects of another,
specify an association between the two. This is a data-driven view of associations.
For each pair of classes, if objects of one class need to interact with objects of the other
class other than as local variables in a procedure or parameters to an operation, specify an
association between the two. This is more of a behavior-driven view of associations.
For each of these associations, specify a multiplicity (especially when the multiplicity is
not *, which is the default), as well as role names (especially if they help to explain the
model).
If one of the classes in an association is structurally or organizationally a whole compared
with the classes at the other end that look like parts, mark this as an aggregation by
adorning the association at the end near the whole with a diamond.
3.Common Mechanisms:
A stereotype is an extension of the vocabulary of the UML, allowing you to create new kinds of
building blocks similar to existing ones but specific to your problem. Graphically, a stereotype is
rendered as a name enclosed by guillemets (French quotation marks of the form « »), placed
above the name of another element.
Optionally the stereotyped element may be rendered by using a new icon associated with that
stereotype.
A constraint is a textual specification of the semantics of a UML element, allowing you to add
new rules or to modify existing ones. Graphically, a constraint is rendered as a string enclosed by
brackets and placed near the associated element or connected to that element or elements by
dependency relationships. As an alternative, you can render a constraint in a note.
UML Collaboration Diagram
The collaboration diagram is used to show the relationship between the objects in a system. Both
the sequence and the collaboration diagrams represent the same information but differently.
Instead of showing the flow of messages, it depicts the architecture of the object residing in the
system as it is based on object-oriented programming. An object consists of several features.
Multiple objects present in the system are connected to each other. The collaboration diagram,
which is also known as a communication diagram, is used to portray the object's architecture in
the system.
1. Objects: The representation of an object is done by an object symbol with its name and
class underlined, separated by a colon.
1. To model collaboration among the objects or roles that carry the functionalities of use
cases and operations.
2. To model the mechanism inside the architectural design of the system.
3. To capture the interactions that represent the flow of messages between the objects and
the roles inside the collaboration.
4. To model different scenarios within the use case or operation, involving a collaboration of
several objects and interactions.
5. To support the identification of objects participating in the use case.
6. In the collaboration diagram, each message constitutes a sequence number, such that the
top-level message is marked as one and so on. The messages sent during the same call are
denoted with the same decimal prefix, but with different suffixes of 1, 2, etc. as per their
occurrence.
1. Determine the behavior for which the realization and implementation are specified.
2. Discover the structural elements that are class roles, objects, and subsystems for
performing the functionality of collaboration.
o Choose the context of an interaction: system, subsystem, use case, and operation.
3. Think through alternative situations that may be involved.
o Implementation of a collaboration diagram at an instance level, if needed.
o A specification level diagram may be made in the instance level sequence diagram for
summarizing alternative situations.
Benefits of a Collaboration Diagram
1. Multiple objects residing in the system can make a complex collaboration diagram, as it
becomes quite hard to explore the objects.
2. It is a time-consuming diagram.
3. After the program terminates, the object is destroyed.
4. As the object state changes momentarily, it becomes difficult to keep an eye on every
single that has occurred inside the object of a system.
Sequence Diagram
The sequence diagram represents the flow of messages in the system and is also termed as an
event diagram. It helps in envisioning several dynamic scenarios. It portrays the communication
between any two lifelines as a time-ordered sequence of events, such that these lifelines took part
at the run time. In UML, the lifeline is represented by a vertical bar, whereas the message flow is
represented by a vertical dotted line that extends across the bottom of the page. It incorporates the
iterations as well as branching.
Messages
The messages depict the interaction between the objects and are represented by arrows. They are
in the sequential order on the lifeline. The core of the sequence diagram is formed by messages
and lifelines.
o Recursive Message: A self message sent for recursive purpose is called a recursive message.
In other words, it can be said that the recursive message is a special case of the self message
as it represents the recursive calls.
o Create Message: It describes a communication, particularly between the lifelines of an
interaction describing that the target (lifeline) has been instantiated.
Note
A note is the capability of attaching several remarks to the element. It basically carries useful
information for the modelers.
Sequence Fragments
1. Sequence fragments have been introduced by UML 2.0, which makes it quite easy for the
creation and maintenance of an accurate sequence diagram.
2. It is represented by a box called a combined fragment, encloses a part of interaction inside
a sequence diagram.
3. The type of fragment is shown by a fragment operator.
Any online customer can search for a book catalog, view a description of a particular book, add a
book to its shopping cart, and do checkout.
Benefits of a Sequence Diagram
1. In the case of too many lifelines, the sequence diagram can get more complex.
2. The incorrect result may be produced, if the order of the flow of messages changes.
3. Since each sequence needs distinct notations for its representation, it may make the
diagram more complex.
4. The type of sequence is decided by the type of message.
Asynchronous Messages in Sequence Diagrams
In sequence diagrams, asynchronous messages are represented by a dashed arrow with an open
arrowhead. Here's a breakdown of the different types of asynchronous messages:
sequenceDiagram
participant A as "Sender"
participant B as "Receiver"
A->>B: Asynchronous Message
Represented by a dashed arrow with an open arrowhead and a priority indicator (e.g.,
high, low, medium).
Indicates that the sender does not wait for a response from the receiver, but the message
has a specific priority.
The receiver processes the message based on its priority.
sequenceDiagram
participant A as "Sender"
participant B as "Receiver"
A->>B: High Priority Asynchronous Message
Call-Back Mechanism
Broadcast Messages
Represented by a dashed arrow with an open arrowhead and a broadcast indicator (e.g.,
broadcast, multicast).
Indicates that the sender sends the message to multiple receivers.
Each receiver processes the message independently.
sequenceDiagram
participant A as "Sender"
participant B as "Receiver 1"
participant C as "Receiver 2"
A->>B: Broadcast Asynchronous Message
A->>C: Broadcast Asynchronous Message
sequenceDiagram
participant A as "Sender"
D->>A: Response