0% found this document useful (0 votes)
27 views4 pages

Design Patterns Assignment - 2

This document is an assignment on structural and behavioral design patterns, specifically focusing on the Adapter Pattern and Observer Pattern. It includes theoretical explanations, real-world applications, and implementations in Python, demonstrating how these patterns solve specific problems in software design. Additionally, it discusses the Interpreter Pattern as a less common design pattern with applications in AI, providing a brief explanation and code implementation.

Uploaded by

kavya sree bandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Design Patterns Assignment - 2

This document is an assignment on structural and behavioral design patterns, specifically focusing on the Adapter Pattern and Observer Pattern. It includes theoretical explanations, real-world applications, and implementations in Python, demonstrating how these patterns solve specific problems in software design. Additionally, it discusses the Interpreter Pattern as a less common design pattern with applications in AI, providing a brief explanation and code implementation.

Uploaded by

kavya sree bandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BANDI.

KAVYA SREE-21BQ1A0515

Design Patterns Assignment#2:


Structural and Behavioural Design Patterns
Question: Implement any ONE structural and ONE behavioral design pattern.
Assignment Tasks:
1. Theory and Explanation (4 Marks):
Q1)List and describe the two selected structural and behavioural design patterns. Provide
real-world scenarios where each pattern could be effectively applied.
Answer: In this assignment, I discuss two structural and behavioral design patterns. Each pattern has unique
applications in software design, helping to make code more reusable, maintainable, and adaptable to
changing requirements.
A)Structural Design Patterns
1. Adapter Pattern
• Description: The Adapter Pattern acts as a bridge between two incompatible interfaces, allowing
classes with incompatible interfaces to work together. The main idea is to create an "adapter" class
that translates calls from the interface expected by the client into calls that are compatible with the
existing interface.
• Real-World Scenario: A common example of
the Adapter Pattern can be found in
payment systems. For instance, consider an
application that needs to integrate both
PayPal and Stripe payment providers. Each
provider has its own API, so their interfaces
are different. By using the Adapter Pattern,
we can create an adapter for each payment
system, allowing the application to
communicate with both systems through a
single, unified interface
B) Behavioral Design Patterns
1. Observer Pattern
• Description: The Observer Pattern establishes a one-to-many relationship where one object (the
subject) notifies multiple observer objects of any changes to its state. This pattern is beneficial when
changes in one object should trigger updates in multiple others, without needing to tightly couple
these objects.
• Real-World Scenario: A news
subscription service is a practical
example of the Observer Pattern. In
this scenario, the news platform
(subject) allows users to subscribe
to specific topics of interest. When
new articles are published, all
subscribers (observers) receive a
notification, keeping them informed
of the latest updates. This allows
users to dynamically subscribe and
unsubscribe to content without
needing any changes to the
notification system.
1
BANDI.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

B) Behavioral Design Pattern: Observer Pattern

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

3. Bonus Task (Optional for 2 Extra Marks):


Q3) Research and implement a less common structural and/or behavioural Design Patterns
not covered in the main course scope. ( AI Based design patterns etc..) - Provide a brief
explanation (150-200 words) of the pattern and a real-world application scenario.
Answer:
The Less Common Design Pattern Explained Here in this Assignment is Interpreter Pattern (Behavioral
Pattern)
Explanation
The Interpreter Pattern is a behavioral design pattern used for designing a simple language interpreter. This
pattern is ideal for situations where a problem requires interpreting a set of rules or expressions in a domain-
specific language. It defines a grammar for the language, with each rule represented by a separate class.
These classes implement an interface that interprets expressions within the language.
The Interpreter Pattern is useful for scenarios such as evaluating mathematical expressions, parsing SQL-like
queries, and analyzing AI rule-based systems where a series of commands or conditions must be interpreted.
By representing each rule or command as a class, the pattern allows the easy addition of new expressions
without modifying the existing ones. The result is a flexible interpreter structure that can process complex
rules and languages with minimal changes.
Real-World Application Scenario
In the field of AI, the Interpreter Pattern can
be applied to design a simple chatbot that
interprets user input as commands. Each
command, like “create a reminder” or
“check weather,” is represented by a class
that interprets the command and executes
the respective action. This approach allows
the chatbot to grow by adding new
command classes without rewriting existing
code, making it adaptable and extensible.
Code Implementation
Below is a basic Python implementation of
the Interpreter Pattern that evaluates
arithmetic expressions.
In this example, Number represents
terminal expressions (constants), while Add
and Subtract are non-terminal expressions
that combine values. The interpret()
method calculates the final result based on
the parsed expression tree. This approach is
particularly useful for parsing simple,
domain-specific languages in AI systems or
any application needing a flexible
interpreter.

You might also like