Design Patterns Assignment - 2
Design Patterns Assignment - 2
KAVYA SREE-21BQ1A0515
2. Implementation (6 Marks):
Q2) Implement one structural and one behavioral design pattern discussed in the
theoretical section. (e.g., Java).
Ensure the implementation demonstrates the key aspects of each pattern. Include proper
class structures, and interfaces, and showcase object creation.
Write a short explanation (200-300 words each) for each implementation, covering:
a)The problem is solved.
b)How the chosen pattern provides a solution.
c)Key parts of the code that demonstrate the pattern's usage
Answer:
Here is the implementation for one structural design pattern (Adapter Pattern) and one behavioral design
pattern (Observer Pattern) in Python.
A)Structural Pattern: Adapter Pattern (Python)
Explanation
• Problem Being Solved: In many software applications, there is a need to integrate third-party libraries
or components that do not follow the exact interface required by the client code. In this example, the
client expects a process_payment() function, but the third-party class PayPalPayment provides a
method called send_payment() instead. This mismatch in interfaces creates a compatibility issue, as
the client cannot directly interact with PayPalPayment.
• How the Chosen Pattern Provides a Solution: The Adapter Pattern solves this problem by creating a
wrapper (PayPalAdapter) that implements the PaymentProcessor interface expected by the client.
The adapter translates the process_payment() call into the send_payment() method of
PayPalPayment, enabling compatibility between the client and the third-party service without
modifying either. This pattern is ideal for situations where the existing code cannot be changed,
allowing seamless integration.
• Key Parts of the Code Demonstrating Pattern Usage: The PayPalAdapter class is the core of the
Adapter Pattern. It adapts the send_payment() method of PayPalPayment to be compatible with
process_payment(), the method expected by the PaymentProcessor interface. This is evident in the
adapter's process_payment() method, which calls send_payment() internally. The client code can now use
the adapter without any change, demonstrating how the pattern facilitates seamless integration.
2
BANDI.KAVYA SREE-21BQ1A0515
Explanation
• Problem Being Solved: In applications like news services or stock trading platforms, updates need to
be broadcasted to multiple subscribers whenever new data is available. Without a proper mechanism,
each subscriber would have to continuously poll the server for updates, which is inefficient and may
lead to missed information.
• How the Chosen Pattern Provides a Solution: The Observer Pattern provides an efficient solution by
allowing the NewsPublisher (subject) to maintain a list of Subscriber (observers) that want to be
notified of updates. When new news is added, the publisher automatically notifies all registered
subscribers. This setup allows the publisher to push updates to subscribers in real-time, eliminating
the need for polling and reducing overhead.
• Key Parts of the Code Demonstrating Pattern Usage: In this code, NewsPublisher maintains a list of
subscribers and notifies them via the notify_subscribers() method. The add_news() method sets the
latest news and triggers the notification process. Each Subscriber implements an update() method
that gets called with the new data. This demonstrates the Observer Pattern by decoupling the
publisher from subscribers and allowing seamless, automatic updates to all registered observers.
3
BANDI.KAVYA SREE-21BQ1A0515