Adapter Pattern
The idea behind the adapter design pattern is to take two
classes that are not Liskov-compatible due to their public
interfaces and make them so by writing an intermediate layer of
software that “translates” between one set of method calls to
another (with a little extra work).
Consider a system that delivers notifications to its users. The
Notification interface looks like this:
______________________
public interface Notification {
void setRecipient(User recipient);
void setSender(User sender);
void setMessage(String message);
String getMessage();
}
__________
Similarly, the User interface looks like the following:
public interface User {
String getName();
String getMobileNumber();
Notification getPreferredContact();
// ... }
___________
Code was written to implement the Notification interface for
in-system messages using a class for in-system messages using a
class InternalNotification. However, they would like to expand the
system to SMS notifications and purchased code that will send SMS
messages. That interface for an SMS message looks like the
following:
public interface SmsMessage {
public void setSender(String phoneNumber);
public void setRecipient(String phoneNumber);
public void setMessageBody(byte[] data);
public byte[] getMessageBody();
}
Implement a class SmsToNotificationAdapter that lets the system
seamlessly use SMS messages to notify its users.
Description
___________________________
STARTER CODE:
public class SmsToNotificationAdapter implements Notification
{
public SmsToNotificationAdapter(SmsMessage message) {
// your code here
}
// your code here
Adapter Pattern The idea behind the adapter design pattern is to take two classes that are not Liskov-compatible due to
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am