Introduction to Design Patterns


Healthy Introduction
Throughout this article, we will explore design patterns from first principles: what exactly a design pattern is, why design patterns are needed, and how each design pattern is implemented both theoretically and through basic programming principles.
Before diving deep into design patterns, we need to understand the approach to learning this topic from a basic perspective.
For every topic we discuss, we will dive deep into the ins and outs of that specific pattern. Before diving further, here are the prerequisites for understanding this topic in detail.
What is System Design?
System design is the process of
Defining the elements of a system
Their interactions and relationships
To satisfy a set of specified requirements
It involves taking a problem statement, breaking it down into smaller components and designing each component to work together effectively to achieve the overall goal of the system. It is an iterative process that may involve multiple rounds of design, testing, and refinement.
In software engineering, system design is a phase in the software development process that focuses on the high-level design of a software system, including the architecture and components.
How to Approach System Design?
There are several steps that can be taken when approaching a system design:
Understand the problem: Gather information about the problem you are trying to solve. Identify the users and their needs, with the constraints or limitations of the system.
Identify the scope of the system: Define the boundaries of the system, including what the system will do and what it will not do.
Research and analyze existing systems: Look at similar systems that have been built in the past and identify what worked well and what didn’t. Use this information to inform your design decisions.
Create a high-level design: Outline the main components of the system and how they will interact with each other via a rough diagram or flowchart.
Refine the design: As you work on the details of the design, iterate and refine it until you have a complete and detailed design that meets all the requirements.
Document the design: Create detailed documentation of your design for future reference and maintenance.
Continuously monitor and improve the system: The system design is not a one-time process, it needs to be continuously monitored and improved to meet the changing requirements.
Prerequisite Knowledge
You should be familiar with at least one object-oriented, statically typed programming language, preferably Java, C++, or C#.
All the code snippets provided are in Java, but they can be applied to other languages because they are written from first principles.
You should have a basic understanding of how Object-Oriented Principles work. You don't need to know them in great depth, but having foundational knowledge will help. If you have studied OOP at any point in your life, whether during your studies or while learning a programming language, that will be sufficient.
Basic Object-Oriented Principles
OOP is the foundational and most useful approach for bottom-up system design.
What is bottom-up system design? It is the process of breaking a large problem into smaller, more manageable problems. Solving each smaller problem will eventually solve the larger one.
OOP concepts can be tricky, as they build upon each other. Here are the core pillars of OOP:
Classes and Objects
- Concept: Objects represent real-world entities with certain features, and a class is a blueprint for creating objects. In other words class defines the properties and behaviors of an object, and object is an instance of the class.
Data Encapsulation
Concept: Encapsulation involves binding the behaviors (methods) and properties (attributes) of an object into a single entity called class.
Encapsulation hides the internal state of an object and restricts direct access to it. This is done using access modifiers like
private
,protected
, andpublic
, ensuring that only authorized code can interact with certain parts of an object.
Data Abstraction
Concept: Abstraction hides the internal implementation of a class while exposing only the relevant behavior.
It allows users to interact with an object without needing to understand its internal workings. This is often achieved by using abstract classes or interfaces, which provide a simplified interface for the user.
Data Inheritance
Concept: Inheritance allows one class (the child) to inherit properties and behaviors from another class (the parent).
It enables the reuse of existing code. However, not all properties or methods are inherited—typically, private members are not accessible from the child class. Also, methods in the child class can be overridden to provide more specific functionality.
Data Polymorphism
Concept: Polymorphism means that the same entity (such as a method) can take on many forms depending on the context.
This allows methods to behave differently in different situations. For example, method overloading (static polymorphism) and method overriding (dynamic polymorphism) are common in OOP, where the method’s behavior is determined based on the object it is called on.
Dynamic Binding
Concept: Dynamic binding refers to the runtime decision of which method to invoke based on the actual object type.
This is a key aspect of dynamic polymorphism. With dynamic binding, method calls are resolved at runtime rather than compile time, which is crucial for method overriding in OOP languages like Java and C++.
Message Passing
Concept: Objects communicate with each other by passing messages, typically through method calls or function invocations.
It allows objects from different classes to collaborate and share information. It is a fundamental concept in OOP as it reflects how objects interact to achieve more complex behavior.
Remember, knowing concepts like abstraction, inheritance, and polymorphism do not make you a good object oriented designer.
The best way to use patterns is to load your brain with them and then recognize places in your designs and existing applications where you can apply them.
Book Recommendation
Following is one of the best books to understand this topic in detail
You can also explore the following roadmap if you want to learn System Design in detail. Roadmap link : System Design Roadmap
References
All of the content and knowledge shared in this article have been referenced from the specified book and the roadmap provided by roadmap.sh.
Subscribe to my newsletter
Read articles from S. Apurba directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
