Software Design
Principles
Instructor: Engineer Sonia
2
Software Design
Principles
Software design principles are fundamental concepts that guide the
process
of designing software solutions. They help developers create systems that
are maintainable, scalable, efficient, and adaptable to change. Here are
some
7. High Cohesion, Low Coupling
key software design principles:
8. Composition over Inheritance
9. POLA
10. Performance Considerations
11. Scalability and Flexibility
12. Testability
Instructor:
© 2018 CreativeEngineer Sonia
– Presentation template
Rafaqat
Software Design 3
1. SOLID Principles:
Principles
Single Responsibility Principle (SRP): A class should have only one
reason to change, meaning it should have only one responsibility.
Example: In an e-commerce system, an Order class should only
handle order- related operations, while a separate Invoice class
should handle invoice generation.
Open/Closed Principle (OCP): Software entities should be open for
extension but closed for modification, allowing for adding new
functionality without altering existing code.
Example: A Shape class can define an interface for calculating area,
and new shape types (e.g., Circle, Rectangle) can be added without
modifying the original class.
Liskov Substitution Principle (LSP): Subtypes must be substitutable
for their base types, meaning objects of a superclass should be
replaceable with objects of its subclass without affecting the correctness
of the program.
Example: A Bird class should have a fly() method only if all
subclasses (e.g., Sparrow) can fly. A subclass like Penguin should not
Instructor:
©
inherit fly() if
2018 CreativeEngineer
it
Sonia
– Presentation
cannot fly.
template
Rafaqat
4
Software Design
Principles
Interface Segregation Principle (ISP): Clients should not be
forced to depend on interfaces they do not use. This principle
encourages the creation of fine-grained interfaces rather than large,
monolithic ones.
Example: Instead of a single Machine interface with print(),
scan(), and
fax() methods, separate interfaces like Printer, Scanner, and
FaxMachine
should be created for specific devices.
Dependency Inversion Principle (DIP):High-level modules should
not depend on low-level modules. Both should depend on
abstractions, and abstractions should not depend on details. In other
words, dependency should be on interfaces, not concrete
implementations.
Example: A NotificationService should depend on an
INotification interface
instead of concrete classes like EmailNotification or
SMSNotification, making
Instructor:
© 2018 it Engineer
Creativeeasier to Sonia
add
– Presentation new notification types.
template
Rafaqat
5
Software Design Principles
2. DRY (Don't Repeat Yourself): Avoid duplicating code by abstracting
common functionality into reusable components, functions, or modules.
Example: If multiple modules need to log messages, create a Logger class
instead
of implementing logging separately in each module.
3. KISS (Keep It Simple, Stupid): Keep the design simple and avoid
unnecessary complexity. Simplicity leads to easier maintenance,
debugging, and understanding
of the codebase.
Example: Instead of over-engineering a complex configuration system, use
simple JSON or YAML configuration files when appropriate.
4. YAGNI (You Aren't Gonna Need It): Only implement features that are
needed at
the present moment. Avoid over-engineering by anticipating future
requirements that may never materialize.
Example: Avoid adding complex reporting features to an application that
only requires basic data retrieval for now.
5. Separation of Concerns: Divide the software into distinct sections,
each addressing a separate concern. This makes the system easier to
understand, maintain, and modify.
© Example:
Instructor:
2018 A webSonia
CreativeEngineer application
– Presentation template should separate concerns into Model, View,
and Controller (MVC architecture) instead of mixing database logic with UI
Rafaqat
6
Software Design
Principles
6. Encapsulation: Encapsulate related functionality into cohesive units
(e.g., classes, modules) and hide the internal workings from the outside
world. This promotes information hiding and reduces dependencies
between components.
Example: A BankAccount class should expose deposit() and withdraw()
methods while keeping the account balance private.
7. High Cohesion, Low Coupling: Aim for high cohesion within
modules (i.e., the elements within a module should be closely related)
and low coupling between modules (i.e., modules should interact with
each other through well-defined interfaces with minimal dependencies).
Example: A CustomerService module should handle customer-related
tasks only, while the OrderService module handles order processing,
with both interacting through well-defined APIs.
8. Composition over Inheritance: Prefer composing objects over
inheritance to promote code reuse and maintainability while avoiding
the complexities and tight coupling associated with inheritance
hierarchies.
Example: Instead of an ElectricCar class inheriting from a Car class, an
ElectricCar can have a Battery component, promoting flexibility.
Instructor:
© 2018 CreativeEngineer Sonia
– Presentation template
Rafaqat
7
tware Design Principles
9. Principle of Least Astonishment (POLA): Design interfaces and
behaviors in a way that is intuitive and unsurprising to users and developers,
reducing the likelihood of errors and confusion.
Example: A ‘Save’ button in a text editor should save the file, not trigger a
print action.
10. Performance Considerations: Design with performance in mind,
considering factors such as algorithmic efficiency, data structures, and
resource utilization.
Example: Use a HashMap instead of a List for quick lookups when handling a
large dataset.
11. Scalability and Flexibility: Design systems that can accommodate
growth in terms of users, data, and features without significant architectural
changes. Allow for flexibility to adapt to changing requirements and
environments.
Example: A cloud-based application should be designed to scale horizontally
by adding more servers rather than relying on a single powerful server.
12. Testability: Design code that is easy to test, promoting the development
of automated tests that verify the correctness of the system's behavior.
Example: Using the MVC pattern in a web application ensures the business
logic (Model) can be tested independently from the user interface (View),
© making
Instructor:
2018 unit testing
CreativeEngineer easier.
Sonia
– Presentation template
Rafaqat