0% found this document useful (0 votes)
22 views12 pages

Aoop Skill Week-3

The document outlines a series of programming tasks focused on implementing design patterns, including Adapter, Decorator, and Bridge patterns, to enhance payment processing, text processing, video playback, and stock price tracking functionalities. Each task includes objectives, code snippets, and explanations of the design patterns used to solve specific problems. The document serves as a practical guide for applying these design patterns in real-world scenarios.

Uploaded by

sonusri778
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)
22 views12 pages

Aoop Skill Week-3

The document outlines a series of programming tasks focused on implementing design patterns, including Adapter, Decorator, and Bridge patterns, to enhance payment processing, text processing, video playback, and stock price tracking functionalities. Each task includes objectives, code snippets, and explanations of the design patterns used to solve specific problems. The document serves as a practical guide for applying these design patterns in real-world scenarios.

Uploaded by

sonusri778
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/ 12

AOOP SKILL WEEK-3

KATURI SONU SRI


2300032941
1) Adapting Different Payment Gateways
Objective: Integrate multiple payment gateways (e.g., PayPal, Stripe)
into a single payment processing interface.
a. Create Interfaces: Define a PaymentProcessor interface with
methods like processPayment(amount: double).
b. Implement Concrete Adapters: Implement adapters for each
payment gateway (e.g., PayPalAdapter, StripeAdapter) that
adapt their specific APIs to the PaymentProcessor interface.
c. Test Integration: Create a client class that uses the
PaymentProcessor interface to process payments, and test with
different adapters.
d. Analyze the issue in this scenario that the Adapter pattern is
meant to address.

Code : Class PaymentClient

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);
}
}

class StripeAdapter implements PaymentProcessor {


private StripeAPI stripeAPI;

StripeAdapter(StripeAPI stripeAPI) {
this.stripeAPI = stripeAPI;
}

@Override
public void processPayment(double amount) {
stripeAPI.makePayment(amount);
}
}

public class PaymentClient {


public static void main(String[] args) {
PaymentProcessor paypal = new PayPalAdapter(new
PayPalAPI());
PaymentProcessor stripe = new StripeAdapter(new StripeAPI());

paypal.processPayment(100.00);
stripe.processPayment(200.00);
2|P a ge
}
}

Output:

2) Extending Functionality of Text Processing


Objective: Enhance a basic text processing class with additional features
(e.g., spell checking, text formatting).
a. Create Base Component: Define a TextProcessor class with a
method process (text: String).
b. Create Decorators: Implement decorators like
SpellCheckDecorator, TextFormatDecorator that extend
TextProcessor and add new functionalities.
c. Test Decorators: Create a client class that uses decorated
TextProcessor objects to process text with added functionalities.
d. Analyze the issue in this scenario that the Decorator pattern is
meant to address.

Code : Class TextProcessor


3|P a ge
public class TextProcessor {
public String process(String text) {
return text;
}

public static void main(String[] args) {


String text = "This is teh example text that might recieve some
errors.";

TextProcessor textProcessor = new TextProcessor();

TextProcessor spellCheckedProcessor = new


SpellCheckDecorator(textProcessor);

TextProcessor formattedProcessor = new


TextFormatDecorator(spellCheckedProcessor);

String result = formattedProcessor.process(text);


System.out.println(result);
}

public static class TextProcessorDecorator extends TextProcessor {


protected TextProcessor wrapped;

public TextProcessorDecorator(TextProcessor wrapped) {


this.wrapped = wrapped;
}

@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);
}
}

public static class TextFormatDecorator extends


TextProcessorDecorator {
public TextFormatDecorator(TextProcessor wrapped) {
super(wrapped);
}

@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.

Code : Class BridgePatternDemo

interface VideoFormat {
void play();
}

class MP4Player implements VideoFormat {


@Override
public void play() {
System.out.println("Playing MP4 video format");

}
}

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 WindowsOS implements OperatingSystem {


@Override
public void run(VideoFormat videoPlayer) {
System.out.println("Running on Windows OS");
videoPlayer.play();
}
}

class LinuxOS implements OperatingSystem {


@Override
public void run(VideoFormat videoPlayer) {
System.out.println("Running on Linux OS");
videoPlayer.play();
}
}

class VideoPlayer {
private VideoFormat videoFormat;
private OperatingSystem os;

public VideoPlayer(VideoFormat videoFormat, OperatingSystem os) {


this.videoFormat = videoFormat;
this.os = os;
}

public void playVideo() {


os.run(videoFormat);
}
}
7|P a ge
public class BridgePatternDemo {
public static void main(String[] args) {
VideoFormat mp4Player = new MP4Player();
VideoFormat aviPlayer = new AVIPlayer();
OperatingSystem windowsOS = new WindowsOS();
OperatingSystem linuxOS = new LinuxOS();

System.out.println("Playing MP4 on Windows OS:");


VideoPlayer mp4VideoPlayer = new VideoPlayer(mp4Player,
windowsOS);
mp4VideoPlayer.playVideo();

System.out.println("\nPlaying AVI on Linux OS:");


VideoPlayer aviVideoPlayer = new VideoPlayer(aviPlayer, linuxOS);
aviVideoPlayer.playVideo();
}
}

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.

Code : Class StockPriceApp

public class StockPriceApp {

public interface StockPriceProvider {

9|P a ge
double getStockPrice(String stockSymbol);
}

public static class StockAPIA {


public double getPrice(String stockSymbol) {
if (stockSymbol.equals("AAPL")) return 150.00;
else if (stockSymbol.equals("GOOGL")) return
2800.00;
return 0.0;
}
}

public static class StockAPIB {


public String fetchStockPrice(String stockSymbol) {
if (stockSymbol.equals("AAPL")) return "150.00";
else if (stockSymbol.equals("GOOGL")) return
"2800.00";
return "0.0";
}
}

public static class StockAPIAAdapter implements


StockPriceProvider {
private StockAPIA stockAPIA;

public StockAPIAAdapter(StockAPIA stockAPIA) {


this.stockAPIA = stockAPIA;
}

@Override
public double getStockPrice(String stockSymbol) {
return stockAPIA.getPrice(stockSymbol);
}
}

10 | P a g e
public static class StockAPIBAdapter implements
StockPriceProvider {
private StockAPIB stockAPIB;

public StockAPIBAdapter(StockAPIB stockAPIB) {


this.stockAPIB = stockAPIB;
}

@Override
public double getStockPrice(String stockSymbol) {
String priceString =
stockAPIB.fetchStockPrice(stockSymbol);
return Double.parseDouble(priceString);
}
}

public static class StockPriceViewer {


private StockPriceProvider stockPriceProvider;

public StockPriceViewer(StockPriceProvider
stockPriceProvider) {
this.stockPriceProvider = stockPriceProvider;
}

public void displayStockPrice(String stockSymbol) {


double price =
stockPriceProvider.getStockPrice(stockSymbol);
System.out.println("The stock price of " +
stockSymbol + " is: " + price);
}
}

public static void main(String[] args) {


StockAPIA stockAPIA = new StockAPIA();
StockAPIB stockAPIB = new StockAPIB();

11 | P a g e
StockPriceProvider stockAPIAProvider = new
StockAPIAAdapter(stockAPIA);
StockPriceProvider stockAPIBProvider = new
StockAPIBAdapter(stockAPIB);

StockPriceViewer viewer = new


StockPriceViewer(stockAPIAProvider);
viewer.displayStockPrice("AAPL");
viewer.displayStockPrice("GOOGL");

viewer = new StockPriceViewer(stockAPIBProvider);


viewer.displayStockPrice("AAPL");
viewer.displayStockPrice("GOOGL");

}
}

Output

12 | P a g e

You might also like