Foundations of Low Level Design

ยท

4 min read

What is LLD how is it different from HLD?

  • LLD, or Low-Level Design, focuses on detailed planning for individual components of a system, including algorithms and data structures. HLD, or High-Level Design, provides a broader overview of the system's architecture and major components, without going into fine-grained details.

  • For LLD we focus on writing maintainable, extensible and modular code, following SOLID principles

What is OOPS?

OOPS, or Object-Oriented Programming, is a programming paradigm that uses objects to model real-world entities and their interactions in software. It revolves around principles like encapsulation, inheritance, and polymorphism to create modular and reusable code.

  1. Encapsulation:

    • Encapsulation is the concept of bundling data (attributes or properties) and methods (functions) that operate on that data into a single unit called an object.

    • It hides the internal state of an object from the outside, allowing controlled access through defined interfaces.

  1. Inheritance:

    • Inheritance allows a new class (subclass or derived class) to inherit properties and behaviours (attributes and methods) from an existing class (base class or superclass).

    • It promotes code reuse and establishes a hierarchical relationship between classes.

    • There are types of inheritance and modes of inheritance in C++

      1. Types of Inheritance in C++

        1. Single Inheritance (B is a A):

          • In single inheritance, a derived class (B) inherits from a single base class (A).
        2. Multilevel Inheritance (B is a A and C is a B):

          • In multilevel inheritance, a class (B) inherits from another class (A), and then a third class (C) inherits from the intermediate class (B).
        3. Multiple Inheritance (B is a A and C):

          • In multiple inheritance, a class (B) inherits from two or more base classes, which can be either classes (A and C) or a class and an interface.
        4. Hierarchical Inheritance:

          • In hierarchical inheritance, multiple derived classes (B, C, etc.) inherit from a single base class (A).
        5. Hybrid Inheritance:

          • Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, or hierarchical inheritances, within a single program or class hierarchy..
      2. Modes of inheritance in C++ (this affects the access status of derived members)

  1. Polymorphism:
  • Polymorphism means "many forms", which means a single function or operator will behave differently in different contexts

  • There are two types of Polymorphism

    1. Compile Time Polymorphism - early/static binding

      1. Function overloading

      2. Operator overloading

    2. Run Time Polymorphism- dynamic/late binding

      1. Function overriding using virtual functions

        In the above example both dog and cat are derived in the same way, the member functions are called in the same way but results are different because of run time polymorphism

        • Marking a function virtual simply signals to the compiler that the function is to be bound art run time - seeing the context of the pointer rather than just the name
  1. Abstraction:
  • The idea is to expose only necessary things ,for example, if you have a utility that uses several other smaller internal functions to generate output- don't expose those smaller internal functions to the user.

How to structure classes together? (Relationships and UML)

SymbolMeaningUsage
# (Hash Symbol)Protected membersPrefix before member name to indicate protected access.
+ (Plus Symbol)Public membersPrefix before member name to indicate public access.
- (Minus Symbol)Private membersPrefix before member name to indicate private access.
_ (Underline)Static membersUnderline before member name to indicate static nature.
<<abstract>>Abstract classes or methodsPrefix before class or method name to indicate abstraction.
<<interface>>InterfacesPrefix before interface name to indicate it's an interface.
<<enumeration>>EnumerationsPrefix before enumeration name to indicate it's an enumeration.

Additional Notes:

  • Public Access : Members accessible everywhere.

  • Private Access: Members only within the class.

  • Protected Access: Members accessible in derived classes.

  • A pure virtual function HAS to be implemented

  • A virtual function may or may not be overriden

  • KISS: It emphasizes simplicity in code. For example, using a basic loop to iterate through a list instead of an overly complex algorithm for a straightforward task.

  • YAGNI: It discourages adding unnecessary features or code that's not currently required. For instance, refraining from implementing advanced customization in software until it's requested by users.

  • DRY: It promotes code reuse by avoiding duplication. For example, creating a reusable function or class to handle common tasks like database connections, rather than copying the same code across the application.

ย