Sunday, January 5, 2025

Can you provide an example of loose coupling?

 Loose coupling refers to a design principle in software engineering where components or modules of a system are independent of each other, meaning they have minimal knowledge of or dependencies on each other. This makes the system more flexible, easier to maintain, and more scalable.

Example of Loose Coupling: Using an Event Listener Pattern in a Web Application

Consider a web application that processes user actions, such as clicking a button to submit a form. If we tightly couple the form handler (which processes the form submission) to the button click, it would be hard to modify or add new actions (e.g., analytics tracking) without affecting the form handler itself.

Instead, we can use loose coupling by separating concerns using an event listener pattern. Here's how:

  1. Button Component: The button is only responsible for triggering an event when clicked.

    // Button.js
    class Button {
        constructor(elementId) {
            this.button = document.getElementById(elementId);
            this.events = [];
        }
    
        addEventListener(event, callback) {
            this.events.push({ event, callback });
            this.button.addEventListener(event, callback);
        }
    }
    
  2. Form Handler: The form handler listens for the "submit" event but doesn't know anything about other actions like analytics or logging.

    // FormHandler.js
    class FormHandler {
        handleSubmit(event) {
            console.log("Form submitted");
            // Perform form submission logic here
        }
    }
    
  3. Analytics Tracker: This component tracks analytics independently of the form handler.

    // AnalyticsTracker.js
    class AnalyticsTracker {
        trackSubmission(event) {
            console.log("Tracking form submission analytics");
            // Send data to analytics service
        }
    }
    
  4. Integration: The components are loosely coupled. The button only triggers events, and other components listen to the events.

    // App.js
    const button = new Button('submitButton');
    const formHandler = new FormHandler();
    const analyticsTracker = new AnalyticsTracker();
    
    button.addEventListener('click', formHandler.handleSubmit);
    button.addEventListener('click', analyticsTracker.trackSubmission);
    

Key Points of Loose Coupling in this Example:

  • The Button doesn't need to know what will happen when it is clicked; it only triggers an event.
  • The FormHandler and AnalyticsTracker listen for the same event but are independent of each other.
  • You can easily modify or add new actions without affecting the other parts of the system (e.g., adding a new component to track different analytics, or changing the form handler).
  • The system is flexible and maintainable because each module only knows about the interface (event listeners) and not about the internal workings of other modules.

No comments:

Post a Comment