Aoop Skill Week-3
Aoop Skill Week-3
interface PaymentProcessor {
void processPayment(double amount);
}
class PayPalAPI {
void pay(double amount) {
System.out.println("Processing PayPal payment of $" + amount);
}
}
class StripeAPI {
void makePayment(double amount) {
System.out.println("Processing Stripe payment of $" + amount);
}
}
1|P a ge
class PayPalAdapter implements PaymentProcessor {
private PayPalAPI payPalAPI;
PayPalAdapter(PayPalAPI payPalAPI) {
this.payPalAPI = payPalAPI;
}
@Override
public void processPayment(double amount) {
payPalAPI.pay(amount);
}
}
StripeAdapter(StripeAPI stripeAPI) {
this.stripeAPI = stripeAPI;
}
@Override
public void processPayment(double amount) {
stripeAPI.makePayment(amount);
}
}
paypal.processPayment(100.00);
stripe.processPayment(200.00);
2|P a ge
}
}
Output:
@Override
public String process(String text) {
return wrapped.process(text);
}
}
4|P a ge
public static class SpellCheckDecorator extends
TextProcessorDecorator {
public SpellCheckDecorator(TextProcessor wrapped) {
super(wrapped);
}
@Override
public String process(String text) {
String correctedText = text.replace("teh",
"the").replace("recieve", "receive");
return super.process(correctedText);
}
}
@Override
public String process(String text) {
String formattedText = text.toUpperCase();
return super.process(formattedText);
}
}
}
Output
5|P a ge
3) You are tasked with designing a video player application that supports
playing different types of video formats (e.g., MP4, AVI, MKV) on
different operating systems (e.g., Windows, macOS, Linux).
Using the Bridge Design Pattern, implement the following:
a. Create a common interface for video formats.
b. Implement concrete classes for each video format (e.g.,
MP4Player, AVIPlayer).
c. Create a common interface for operating systems.
d. Implement concrete classes for each operating system (e.g.,
WindowsOS, LinuxOS).
e. Demonstrate how a video player can play any video format on any
operating system by bridging the two hierarchies.
Requirements:
a. The implementation should demonstrate the decoupling of
abstraction and implementation using the Bridge Design Pattern.
b. Provide a code snippet showing how a specific video format is
played on a particular operating system.
interface VideoFormat {
void play();
}
}
}
6|P a ge
class AVIPlayer implements VideoFormat {
@Override
public void play() {
System.out.println("Playing AVI video format");
}
}
interface OperatingSystem {
void run(VideoFormat videoPlayer);
}
class VideoPlayer {
private VideoFormat videoFormat;
private OperatingSystem os;
Output
8|P a ge
4) You are developing a financial application that tracks stock prices. Your
system fetches live stock data from two different APIs:
a. StockAPI_A provides data in the format:
i. public class StockAPIA {
ii. public double getPrice(String stockSymbol) {
iii. // Returns stock price
iv. }
v. }
b. StockAPI_B provides data in a different format:
i. public class StockAPIB {
ii. public String fetchStockPrice(String stockSymbol) {
iii. // Returns stock price as a String
iv. }
v. }
Your application should work with both APIs seamlessly and display the
stock price in a unified format.
Requirements:
a. Implement an Adapter Design Pattern to unify the two APIs.
b. Create an interface StockPriceProvider with a method double
getStockPrice(String stockSymbol).
c. Implement two adapters (StockAPIAAdapter and StockAPIBAdapter)
to integrate StockAPI_A and StockAPI_B respectively.
d. Write a client class StockPriceViewer that accepts a
StockPriceProvider and displays the stock price.
9|P a ge
double getStockPrice(String stockSymbol);
}
@Override
public double getStockPrice(String stockSymbol) {
return stockAPIA.getPrice(stockSymbol);
}
}
10 | P a g e
public static class StockAPIBAdapter implements
StockPriceProvider {
private StockAPIB stockAPIB;
@Override
public double getStockPrice(String stockSymbol) {
String priceString =
stockAPIB.fetchStockPrice(stockSymbol);
return Double.parseDouble(priceString);
}
}
public StockPriceViewer(StockPriceProvider
stockPriceProvider) {
this.stockPriceProvider = stockPriceProvider;
}
11 | P a g e
StockPriceProvider stockAPIAProvider = new
StockAPIAAdapter(stockAPIA);
StockPriceProvider stockAPIBProvider = new
StockAPIBAdapter(stockAPIB);
}
}
Output
12 | P a g e