Cookie Consent by Free Privacy Policy Generator 📌 Comprensión e Implementación del Patrón Observer en el Desarrollo de Software


✅ Comprensión e Implementación del Patrón Observer en el Desarrollo de Software


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Introduction

The Observer pattern is a fundamental behavioral design pattern in software engineering that allows a subscription mechanism to notify multiple objects of any event that occurs to the object they are observing. This pattern is also known by various names such as Observer, Publish-Subscription, Model-View, Event-Subscribe, and Listener.

Problem Scenario

Consider a scenario that involves two types of objects: a Customer object and a Store object. The customer is very interested in a specific brand of product, such as a new iPhone model, which will soon be available in the store. The customer could visit the store daily to check the availability of the product, but most of these trips would be in vain until the product is actually available. On the other hand, the store could send hundreds of emails to all customers every time a new product becomes available, saving customers unnecessary travel but at the same time, potentially annoying other customers not interested in the new products.

Observer Pattern Solution

The Observer pattern addresses this problem by suggesting that you add a subscription mechanism to the notifier class so that individual objects can subscribe or unsubscribe from an event stream of that notifier.

Real-World Example

A practical example of the Observer pattern is a system of interaction between customers and stores:

  • Notifier: The store that sends notifications about product availability.
  • Subscriber: The customer who receives updates about the products of their interest.

Code Example

Definition of Classes

Here we define the 'Notifier' and 'Subscriber' classes. The 'Notifier' class manages a list of subscribers and notifies each of them when an event occurs. The 'Subscriber' class simply prints the message it receives.

The Notifier class acts as the publisher (or subject) in the Observer pattern. This class is responsible for keeping a list of their observers and notifying them when something important happens.

class Notificador:
    def __init__(self):
        self._observadores = []

    def suscribir(self, observador):
        self._observadores.append(observador)

    def desuscribir(self, observador):
        self._observadores.remove(observador)

    def notificar(self, mensaje):
        for observador in self._observadores:
            observador.actualizar(mensaje)


python

The Subscriber class represents the observers in this pattern. Each subscriber who wants to receive notifications from the Notifier must implement this class.

class Suscriptor:
    def actualizar(self, mensaje):
        print(f"Actualización recibida: {mensaje}")


python

Instantiating Classes

Finally, let's look at how these components interact in a usage example.

tienda = Notificador()
cliente = Suscriptor()


python

Customer subscription to store notifications

tienda.suscribir(cliente)


python

Notification of the availability of a new product

tienda.notificar("¡El nuevo modelo de iPhone ya está disponible!")


python

Advantages of the Observer Pattern

Weak Coupling

The notifier doesn't need to know anything about subscribers. This allows the components of the system to be more independent of each other, making it easier to maintain and evolve.

Dynamic Relationships

New subscribers can join or leave at any time without the notifier needing to change. This makes the system highly adaptable and scalable to future changes.

Conclusion

The Observer pattern is particularly useful in situations where an object needs to notify other objects about changes in its state, without knowing who these objects are. It promotes a clear separation of concerns and a well-organized architecture in applications.

By implementing the Observer pattern, software developers can create flexible, maintainable code that efficiently handles dynamic relationships between objects, making it ideal for managing complex dependencies in software applications.

...

✅ Comprensión e Implementación del Patrón Observer en el Desarrollo de Software


📈 152.5 Punkte

✅ Detrás del código: revelando el lado oscuro del desarrollo de aplicaciones


📈 53.54 Punkte

✅ ** ¡MVP en desarrollo de software: la Pantera Rosa a la conquista del mundo digital! **🐾


📈 42.29 Punkte

✅ Observer Pattern: Was steckt hinter dem Observer Design Pattern?


📈 35.48 Punkte

✅ Aplicación de LLM en la Comprensión de Imágenes a través de SVG's


📈 34.67 Punkte

✅ La seguridad del software está en el corazón de la seguridad del hardware: .


📈 34.37 Punkte

✅ Procesamiento del lenguaje natural como parapeto del abuso y la violencia


📈 30.41 Punkte

✅ El papel del CIO hoy: Los agentes del cambio sólo tienen que presentarse


📈 30.41 Punkte

✅ ** La teoría del Big Bang del rendimiento de las API (API Performance) ** 🌌🌟


📈 30.41 Punkte

✅ Cecabank y Kyndryl estrechan lazos para garantizar el soporte del ‘mainframe’ del banco


📈 30.41 Punkte

✅ Big Sur: Cuáles son los requisitos del sistema del sistema macOS 11


📈 30.41 Punkte

✅ ** Breaking Code: Buenas Prácticas de Desarrollo de Software a través de Breaking Bad**⚗️


📈 27.08 Punkte

✅ Maximizando la Productividad: Cómo Finalizar tu Jornada de Desarrollo de Software de Manera Efectiva


📈 27.08 Punkte

✅ Lean Startup y MVPs: Soluciones Ágiles para el Desarrollo de Software


📈 27.08 Punkte

✅ Ingeniería de Software y el Manejo de la Ira: Estrategias para un Desarrollo Exitoso


📈 27.08 Punkte

✅ PINE64, the company that creates the PinePhone and PineBook, becomes a KDE Patron.


📈 23.13 Punkte

✅ Library Creates Fake Patron Records To Avoid Book-Purging


📈 23.13 Punkte

✅ Patrón Proxy: Una introducción teórica


📈 23.13 Punkte

✅ Manafort and His Ukraine Patron: “FinCEN Files” Further Illustrate Gaping Holes in Oversight


📈 23.13 Punkte

✅ Patron Info System - SQL Injection Vulnerability


📈 23.13 Punkte

✅ Principios de Desarrollo Frontend Avanzado: Una Guía Profesional


📈 23.13 Punkte

✅ Samsung Habla Español: Desarrollo de Aplicaciones con Tizen


📈 23.13 Punkte

✅ Canonical Is Now a Patron of KDE, as Part of the Corporate Membership Program


📈 23.13 Punkte

✅ Liberapay: Avatar URL is exposed in patron export for secret donations


📈 23.13 Punkte

✅ Introducción al Desarrollo de Aplicaciones con Tizen - Taller Práctico


📈 23.13 Punkte

✅ Patron Info System SQL Injection


📈 23.13 Punkte

✅ Möglicher "Patron des Internets" vor Seligsprechung


📈 23.13 Punkte

✅ Patron Info System - SQL Injection Vulnerability


📈 23.13 Punkte











matomo

Datei nicht gefunden!