Aoop Week 3
Aoop Week 3
K.Neeraja
// PaymentProcessor Interface
interface PaymentProcessor {
void processPayment(double amount);
}
// PayPal Adapter
static class PayPalAdapter implements PaymentProcessor {
private PayPalAPI payPalAPI;
@Override
public void processPayment(double amount) {
payPalAPI.makePayment(amount);
}
}
// Stripe Adapter
static class StripeAdapter implements PaymentProcessor {
private StripeAPI stripeAPI;
@Override
public void processPayment(double amount) {
2300032041 Week-3
K.Neeraja
stripeAPI.charge(amount);
}
}
// Payment Client
static class PaymentClient {
private PaymentProcessor paymentProcessor;
// Main Method
public static void main(String[] args) {
2300032041 Week-3
K.Neeraja
// Using PayPal
PayPalAPI payPalAPI = new PayPalAPI();
PaymentProcessor payPalAdapter = new PayPalAdapter(payPalAPI);
PaymentClient payPalClient = new PaymentClient(payPalAdapter);
payPalClient.makePayment(100.0);
// Using Stripe
StripeAPI stripeAPI = new StripeAPI();
PaymentProcessor stripeAdapter = new StripeAdapter(stripeAPI);
PaymentClient stripeClient = new PaymentClient(stripeAdapter);
stripeClient.makePayment(200.0);
}
}
// Base Component
static class TextProcessor {
public String process(String text) {
return text; // Basic processing (no changes)
}
2300032041 Week-3
K.Neeraja
// SpellCheckDecorator
static class SpellCheckDecorator extends TextProcessor {
private TextProcessor textProcessor;
@Override
public String process(String text) {
text = textProcessor.process(text);
return checkSpelling(text);
}
// TextFormatDecorator
static class TextFormatDecorator extends TextProcessor {
private TextProcessor textProcessor;
@Override
public String process(String text) {
2300032041 Week-3
K.Neeraja
text = textProcessor.process(text);
return formatText(text);
}
// Main Method
public static void main(String[] args) {
// Base TextProcessor
TextProcessor baseProcessor = new TextProcessor();
// Bridge Class
static class VideoPlayer {
private OperatingSystem os;
private VideoFormat format;
// Main Method
public static void main(String[] args) {
VideoPlayer windowsPlayer = new VideoPlayer(new WindowsOS(), new MP4Player());
windowsPlayer.play("movie.mp4");
// StockPriceProvider Interface
interface StockPriceProvider {
double getStockPrice(String stockSymbol);
}
// StockAPI_A Adapter
static class StockAPIAAdapter implements StockPriceProvider {
private StockAPIA stockAPIA;
@Override
public double getStockPrice(String stockSymbol) {
return stockAPIA.getPrice(stockSymbol);
}
}
// StockAPI_B Adapter
static class StockAPIBAdapter implements StockPriceProvider {
private StockAPIB stockAPIB;
@Override
public double getStockPrice(String stockSymbol) {
// Convert the String returned by StockAPIB to a double
return Double.parseDouble(stockAPIB.fetchStockPrice(stockSymbol));
}
2300032041 Week-3
K.Neeraja
// Main Method
2300032041 Week-3
K.Neeraja
// Using StockAPI_B
StockAPIB stockAPIB = new StockAPIB();
StockPriceProvider adapterB = new StockAPIBAdapter(stockAPIB);
StockPriceViewer viewerB = new StockPriceViewer(adapterB);
viewerB.displayStockPrice("GOOGL");
}
}