Uploaded by Mohamed Nufail

Adapter Pattern EG 2017 3254

advertisement
EE7205: OBJECT ORIENTED
DESIGN PATTERNS AND PRINCIPLES
ASSIGNMENT 02
NAME
: NUFAIL M.F.M
REG NO
: EG/2017/3254
SEMESTER
: 07
DATE
: 10.21.2021
ADAPTER PATTERN
I.
The Problem
The Adapter Pattern one of the Gang of Four structural patterns. As the name of the pattern,
it suggests that different types of power cables can be plugged into a socket through a power adapter.
Similarly using the adapter pattern, convert the interface of a class into another interface the client
expects. Adapter lets classes work together that couldn’t otherwise because of incompatible
interfaces.
Consider an Online Class Scheduling System updating their features like notifying the
students when they schedule or cancel a session for a class. But there are many methods of sending
notification to the students such as email, SMS, app notification and automated calls. Therefore,
each notification service needs different type of compatibilities and information to notify the
students. In this problem, design pattern that allows the interface of an existing class to be used as
another interface. It is often used to make existing classes work with others without modifying their
source code.
II.
How The Problem Solved by The Adapter Pattern
FIGURE 01 : UML DIAGRAM FOR THE ONLINE CLASS SCHEDULING SYSTEM
According to the above Online Class Scheduling System UML diagram, a Notification
Service Adapter class is used as an adapter between the incompatible interfaces such as Notification
Service, SMS Sender, Email Sender and Push Notification Sender. In this system SMS service
provider and Email service provider need different type of context and information from the
Notification Service to notify the students. This pattern is typically used when an incompatible
module needs to be integrated with an existing module without making any source code
modifications.
Source Code
Class Service Class
Notification Service Interface
Notification Service Interface is the existing service which is compatible with account
service, but now some new third-party SMS providers, Email providers and Push notification
providers should integrate with the system. Therefore, Notification Service Adapter class is used
to act as a middle layer between incompatible interfaces.
Notification Service Adapter
Notification Service Adapter act as a middle layer between Notification Service and third
libraries such SMS sender, Email sender and Push notification. No changes made to existing Class
Service and Notification Service classes.
Third Party Service Provider Interfaces
III.
Benefit Of Using the Adapter Pattern
Here the Notification Service Adapter depend on SMS sender, Email sender and Push
notification sender. Therefore, it is not tightly coupled with the services and it gives the benefit of
future updates without changing the existing source code and it follows Open-Closed principle.
•
•
•
IV.
A reusable Adapter class that cooperates with unrelated or unforeseen classes, that
is, classes that don’t necessarily have compatible interfaces.
Third party libraries use adapters as a middle layer between the application. It
decouples the application from the library. New libraries can be integrated without
having to change the application code.
Library updates can be done easily without changing the application code.
Drawbacks Of Using the Adapter Pattern
•
•
Adapter class override some behavior of adaptee’s class, since Adapter class is a
subclass of Adaptee, as a consequence, a class adapter won’t work when we want
to adapt a class and all its subclasses.
Some Adaptee class’s behaviors makes it harder to override by the Adapter class.
Download