Low Level Design
Basics Of LLD

class diagrams
they reflect the real world problem in terms of classes, their relationship w other classes, responsibility of every class in terms of class methods and functions, and the data and info those classes hold in terms of class attributes.

uml sequence diagrams
help in understanding how different instances of classes are interacting with each other, what messages they are passing to each other in what order, etc.

designing low-level apps using objects and classes
OOP != OOD : OOP can be done while violating OOD principles
inheritance


Design Principles
SOLID
S - Single Responsibility Principle
O - Open/Closed Principle
- Open for extension, closed for modification
Liskov Substitution Principle
- Any class that is the child of a parent class should be usable in place of it's parent without any unexpected behavior.
- Ensures reliability when using polymorphism
- Avoids unexpected behaviors in subclass implementations
-
- : i.e. "You should be able to replace the actual class instance by the sub-class instance."
- this principle helps one understand how to inherit properly
-
Interface Segregation Principle
- Applies to interfaces
- Segregates interfaces based on functionality(avoids fat interfaces)
- Prefer many interfaces rather than one general interface, giving preference to many small interfaces each w a specific responisiblity
- Reduces unnecessary dependencies
- Simplifies implementation for specific use cases.
-
-
-
-
Dependency Inversion Principle
- Promotes decoupled architecture
- Facilitates testing and maintainability
-
-
DRY
- Don't Repeat Yourself
- Avoid duplicating code
- Create reusable components, functions or modules that can be utilized in various parts of the codebase
- Makes the code more maintainable and reduces the chance of errors
Design Patterns
Creational
-
Instead of creating objects directly all over your code, creational patterns provide a smart, controlled way to handle the object creation process.
-
They deal with the process of object creation in a flexible and reusable way.
-
Examples:
- Singleton Pattern : ensures there's only one instance of a class throughout the entire system, ensuring consistent behavior and preventing resource wastage
- Factory Method Pattern : Think of it like an automatic assembly line in a factory, it creates the products dynamically
- Abstract Factory Pattern : Think of it like a mega factory, where it creates families of objects, i.e. multiple related products. (Like a set of furniture)
- Builder Pattern : Think of it as a custom order system, where you break down the creation process in multiple steps. (Like building a luxury car)
- Prototype Pattern : Instead of creating objects from scratch, you clone an existing one. Saves time and resources.
-
Advantages: Simplifies Object Creation, Flexibility, Maintainability
-

Behavioral
-
Enhancing communication b/w objects
-
The term "behavioral" comes from the fact that these patterns define the behavior of objects in relation to one another.
-
They don't focus on the objects themselves(like their properties).
-
Instead, they focus on how objects communicate & work together.
-
Examples:
- Observer Pattern : When something imp happens, all subscribers(observers) get updates automatically. No need to check in constantly-it happens in real-time.(Think of it like a social media newsfeed)
- Strategy Pattern : One can switch b/w strategies dynamically depending on the context. It's like having different game plans for different situations.
- Command Pattern : Used to give commands. For example, a TV remote. Each button pressed is treated as a command - execute, undo, or queue up commands. Encapsulating requests as objects makes them more reusable and flexible.
- Chain Of Responsibility Pattern : Requests handle through a chain until they reach the right handler. For example, a tech support system. If one help desk can't solve a problem, they pass it on to the next level.
- Mediator Pattern : All communication goes through a mediator. This avoids unnecessary complexity & keeps things structured. For example, a project manager.
- State Pattern : Object's behavior changes dynamically based on it's state.
- Template Method Pattern : A template is provided, where customization is allowed. Ensures consistency while allowing flexibility. For example, a cooking recipe
- Iterator Pattern : Lets you iterate through a collection w/o worrying about how it's structured. Works great w/ lists, arrays, and collections.
- Visitor Pattern : Allows new operations to be added to objects w/o modifying their structure. Extends functionality w/o changing existing code.
- Memento Pattern : Allows you to capture an object's state and restore it later. Useful for undo features and rollback functionality.
-
Structural
-
Structural patterns focus on the architecture of your code.
-
Help you establish well-defined, flexible connections b/w components, reducing dependencies and making your system easier to understand, extend and maintain.
-
Examples:
- Adapter Pattern(The Universal Translator) : acts as a bridge b/w two incompatible interfaces, allowing them to work together w/o altering their underlying code.
- Bridge Pattern(Separation of Concerns) : lets you split a large class into two separate hierarchies-abstraction and implementation-which can be developed independently of each other.
- Composite Pattern(Building a Hierarchy) : lets you treat individual objects and groups of objects uniformly, making it easier to work with complex hierarchies.
-
Advantages : organized code, reduced dependencies, reusability, simplified maintenance
-