Software Design & Architecture
Why use design pattern?
Design pattern was created to fix known problems and issues
Design patterns provide reusable solutions to common problems
IT represent the best practices to write better code
Design Pattern accelerate the development process and save time
It make increase the readability of the code
By following a known pattern, the code becomes easier to maintain and update because the structure is familiar to other developers
Optimize the code efficiency
What is SOLID? Describe briefly?
Principle
Description
Single Responsibility Principle
Each class should be responsible for a single part or functionality of the system
Open-Closed Principle
Software components should be open for extension, but not for modification.
Liskov Substitution Principle
Objects of a superclass should be replaceable with objects of its subclasses without breaking the system.
Interface Segregation Principle
No client should be forced to depend on methods that it does not use.
Dependency Inversion Principle
High-level modules should not depend on low-level modules, both should depend on abstractions.
What is the Singleton Pattern? Provide an example.
The Singleton design pattern in Java is a way to ensure that a class has only one instance and provides a global point of access to it. It's like having a single president in a country; no matter where you go, there's only one president.
Here’s a simple explanation and step-by-step guide to implementing the Singleton pattern:
Explanation:
Single Instance: Only one object (instance) of the class exists throughout the application.
Global Access: Everyone can access that one instance from anywhere in the application.
Steps to Implement Singleton in Java:
Private Constructor: Prevents other classes from creating new instances.
Private Static Variable: Holds the single instance of the class.
Public Static Method: Provides access to the single instance.
Example Code:public class Singleton { // Step 2: Create a private static variable to hold the single instance private static Singleton instance; // Step 1: Make the constructor private so no other class can instantiate it private Singleton() { // Private constructor } // Step 3: Provide a public static method to get the instance public static Singleton getInstance() { // Create the instance if it does not exist if (instance == null) { instance = new Singleton(); } // Return the single instance return instance; } // Example method public void showMessage() { System.out.println("Hello from Singleton!"); } } public class Main { public static void main(String[] args) { // Get the single instance of Singleton Singleton singleton = Singleton.getInstance(); // Call a method on the Singleton instance singleton.showMessage(); } }
Architectural Patterns Visualize
Monolithic Approach vs. Microservices Approach: Which is Right for Your Application?
Monolithic architecture is a traditional software design pattern where an entire application is constructed as a single, tightly integrated unit
The Microservices architecture breaks down an application into a collection of smaller, independently deployable services. Each service is responsible for specific functionalities and communicates with others through well-defined APIs.
Monolithic applications typically use a single technology stack throughout the entire application. If you choose a specific technology stack for a Monolithic app, you are committed to it for the entire application.
Microservices are platform and technology stack independent. Each service within a Microservices architecture can be developed using different programming languages, databases, and technologies. For example, you could have one service built with Node.js using a NoSQL database, while another service might be developed in Java using a relational database.
In a Monolithic architecture, scaling typically involves deploying the entire application. So, if one part of the application requires more resources, the entire monolith needs to scale, potentially leading to inefficient resource allocation.
Microservices allow for individual service scaling. Let's consider an e-commerce application: the product search service might experience heavy traffic during a sale event, requiring additional resources to handle the load, while the shopping cart service might be performing well and require no additional scaling at that moment.
In a Monolithic architecture, different teams may work on different parts of the monolith, but there's a higher risk of conflicts and coordination challenges, as changes in one part of the monolith can impact others.
With Microservices, development teams can focus on individual services. For example, one team can be responsible for the product catalog service, while another team handles user authentication. This allows for specialized expertise and faster development cycles within each team.
In contrast, Monolithic applications are deployed as a single unit. When you need to release a new feature or update, you must redeploy the entire monolith. This can be a riskier and more complex process, especially if the changes are substantial.
One of the key advantages of Microservices is their flexibility in deployment. Each service within a Microservices architecture can be independently deployed. This means that when it's time to release a new feature or upgrade a specific functionality in your software, you don't need to redeploy the entire application. Instead, you can deploy only the service(s) that have been modified.
Last updated