OOMDesign Pattern 24
OOMDesign Pattern 24
Professor
Indian Institute of Information Technology, Allahabad
1
OOM
● Software Design: Design Patterns
● OO Modeling with class diagrams: Creating quality Classes
● Patterns in Engineering: Concepts wrt Software Engineering
● Design Patterns: Inheritance, Composition & Aggregation
● Design Principles
● Open Close Principles
● Dependency Inversion Principles
● Interface Segregation Principle
● Single Responsibility Principle
● Creating Quality Classes
● Liskov's Substitution Principle
● Architectural Consideration
● Concepts & Implementation issues
3
● Recent trends in Architecture
Real World: Modeling to Design
Enhancing Software Design Quality with
Object-Oriented Principles and Patterns
In the realm of medium and large-scale software projects, the quality of class design plays a
pivotal role in determining the system's overall success.
While class diagrams serve as essential tools for modeling system structure, they alone are not
sufficient to ensure a robust and extensible design.
To create systems that can evolve gracefully without compromising existing functionality,
software developers must delve deeper into advanced object-oriented methodologies.
Object-Oriented Modeling (OOM) has emerged as a cornerstone in addressing these
challenges, offering a rich set of design principles and patterns. These concepts provide
developers with powerful techniques to craft high-quality, maintainable, and scalable
software architectures.
By leveraging OOM principles and design patterns, teams can significantly enhance
their ability to create flexible systems that stand the test of time and changing
requirements.
OO Design Principles
6
OO Design Principles & Design Patterns
Adhering to these principles helps in creating loosely coupled systems where classes have well-defined responsibilities and
interact through clear interfaces. This approach not only improves code readability but also enhances testability and makes the
system more resilient to changes. By following these principles, developers can create software that is easier to extend and less
prone to bugs when modifications are needed.
Single Responsibility Principle Open-Closed Principle (OCP) Liskov Substitution Principle (LSP)
(SRP) Software entities should be open for Objects of a superclass should be
A class should have only one reason to extension but closed for modification. replaceable with objects of its
change, meaning it should have only This principle encourages the use of subclasses without affecting the
one job or responsibility. This principle abstractions and polymorphism to correctness of the program. This
promotes high cohesion and helps in allow new functionality to be added principle ensures that inheritance is
creating more maintainable and without altering existing code. used correctly and promotes the
understandable code. creation of well-designed class
hierarchies.
Advanced Object-Oriented Design Principles
Beyond the core SOLID principles, there are additional advanced object-oriented design principles that further enhance
software quality. These principles focus on creating more flexible, maintainable, and scalable systems by promoting loose
coupling and high cohesion among components.
Principles such as the Dependency Inversion Principle (DIP) and the Interface Segregation Principle (ISP) play crucial roles
in creating modular and easily extensible systems. The DIP promotes the use of abstractions to decouple high-level
modules from low-level implementations, while the ISP encourages the creation of fine-grained interfaces tailored to
specific client needs. These principles, when applied correctly, lead to systems that are more resilient to change and easier
to test and maintain.
modules. Both should they do not use. performed through an over class inheritance to
The Gang of Four (GoF) design patterns, categorized into Creational, Structural, and Behavioral patterns, offer a comprehensive
toolkit for addressing various architectural concerns. Creational patterns deal with object creation mechanisms, Structural
patterns focus on composing classes and objects into larger structures, and Behavioral patterns are concerned with
communication between objects and the assignment of responsibilities.
18
OO Design Principles
Based on concepts of Object Orientation, these Design
Principles are considered instrumental in developing a robust,
flexible and maintainable Software ;
19
Data 3.19 Prof.Dr.
Open Closed Principle
● OCP aims at achieving OO design objectives.
20
Data 3.20 Prof.Dr.
OCP
● To extend the behavior of the system, we add new code. We
do not modify old code.
Modules that conform to the OCP meet two criteria:
● Open For Extension - The behavior of the module can be
extended to meet new requirements
● Closed For Modification - the source code of the module is not
allowed to change
● How can we do this?
● Abstraction
● Polymorphism
● Inheritance
● Interfaces
21
Data 3.21 Prof.Dr.
Open Closed Principle
● The Open Closed Principle (OCP) is undoubtedly the most
important of all the class category principles. In fact, each of
the remaining class principles are derived from OCP.
● It originated from the work of Bertrand Meyer, who is
recognized as an authority on the object-oriented paradigm.
● When referring to the classes Open Closed Principle can be
ensured by use of Abstract Classes and concrete classes for
implementing their behavior.
● Some ways to do this include Polymorphism / Inheritance,
Composition, Inversion of Control, Aspect-Oriented
Programming, and many other principles, patterns, and
techniques of OOAD.
● It is not possible to have all the modules of a software system
satisfy the OCP, but we should attempt to minimize the
number of modules that do not satisfy it
22
Data 3.22 Prof.Dr.
OCP
● OCP Principle is basis for many other OO designs
● The same principle can be applied for modules,
packages, libraries.
● If you have a library containing a set of classes
there are many reasons for which you’ll prefer to
extend it without changing the code that was
already written (backward compatibility, regression
testing, …).
● This is why we have to make sure our modules follow Open
Closed Principle.
● Suppose we have to prepare Software for Graphics Editor..
23
Data 3.23 Prof.Dr.
● // Open-Close Principle
OCP
class GraphicEditor { Example to highlight Open Close Principle.
public void drawShape(Shape s) { It implements a graphic editor which handles the
if (s.m_type==1) drawing of different shapes.
drawRectangle(s);
else if (s.m_type==2) There are several disadvantages:
drawCircle(s);
} ✔for each new shape added the unit testing of the
public void drawCircle(Circle r) {....} GraphicEditor should be redone.
public void drawRectangle(Rectangle r)
{....} ✔when a new type of shape is added the time for
} adding it will be high since the developer who add
it should understand the logic of the GraphicEditor.
class Shape {
int m_type; ✔adding a new shape might affect the existing
} functionality in an undesired way, even if the new
shape works perfectly
class Rectangle extends Shape {
Rectangle() {
super.m_type=1;
}
}
In order to have more dramatic effect, just imagine that the Graphic Editor is a big class, with
a lot of functionality inside, written and changed by many developers, while the shape might
be a class implemented only by one developer.
In this case it would be great improvement to allow the adding of a new shape without
changing the GraphicEditor class.
25
Data 3.25 Prof.Dr.
OCP
26
Data 3.26 Prof.Dr.
OCP
Making a flexible design involves additional time and effort spent for it and it introduce new
level of abstraction increasing the complexity of the code.
So this principle should be applied in those area which are most likely to be changed.
There are many design patterns that help us to extend code without changing it. For instance
the Decorator pattern help us to follow Open Close principle. Also the Factory Method or the
Observer pattern might be used to design an application easy to change with minimum 27
changes in the existing code.
Data 3.27 Prof.Dr.
Single Responsibility Principle
28
Data 3.28 Prof.Dr.
Single Responsibility Principle
29
Data 3.29 Prof.Dr.
Real World: Modeling to Design