Question: Proxy Pattern The current code includes two classes that represent the APIs for Google and AWS services, but only a subset of the methods implemented

Proxy Pattern

The current code includes two classes that represent the APIs for Google and AWS services, but only a subset of the methods implemented are required for application. Because we want to expose the application only, the methods that are really required are as follows:

  1. Implement a Proxy named GoogleSpeech2TextProxy for the Google API
  2. Implement a Proxy named AWSTranscribeProxy for the AWS API.
  3. Update the CCWorker class accordingly.

Adapter Pattern

We want to make use of the same interface (therefore, the same method) for accessing the two Proxies.

  1. Implement a CCGoogleAdapter and CCAWSAdapter that have the same method (although different logic) for triggering the closed-captioning process.
  2. Update the CCWorker class accordingly (keep the cc triggering using local methods).

package loggy;

import aws.api.AWSTranscribeAPI; import google.api.GoogleSpeech2TextAPI;

/** * @author jesus * */ public class CCWorker implements Runnable { private final Recording recording;

// Constructor to assign a message when creating a new thread public CCWorker(Recording recording) { this.recording = recording; }

@Override /** * */ public void run() { System.out.println( Thread.currentThread().getName() + " (Start closed captioning) recording = " + recording.getFileName()); // Trigger CC using the local methods. triggerGoogleClosedCaptioning(); triggerAWSClosedCaptioning();

// Trigger CC using the adapter methods. // TODO

System.out.println(Thread.currentThread().getName() + " (End closed captioning)"); }

private void triggerGoogleClosedCaptioning() { String rawFile = recording.getMediaFileMock(); Long fileSize = recording.getFileSize(); GoogleSpeech2TextAPI api = new GoogleSpeech2TextAPI(); String ccFile = ""; System.out.println("Closed captioning " + rawFile + " will take " + fileSize + " milliseconds..."); try { // Simulate the delay. Thread.sleep(fileSize); // MockUp transcript process. String speechClient = api.instantiateClient(); String audioBytes = api.fileToMemory(rawFile); String config = api.buildSyncRecognizeRequestConfig(); String audio = api.buildSyncRecognizeRequestAudio(); api.performSpeechRecognition(config, audio); String transcript = api.getFirstTranscriptAlternative(); ccFile = transcript; } catch (InterruptedException e) { e.printStackTrace(); } recording.setCcFileMock(ccFile); System.out.println(ccFile + " processed using GoogleSpeech2TextAPI"); } private void triggerAWSClosedCaptioning() { String rawFile = recording.getMediaFileMock(); Long fileSize = recording.getFileSize(); AWSTranscribeAPI api = new AWSTranscribeAPI(); String ccFile = ""; System.out.println("Closed captioning " + rawFile + " will take " + fileSize + " milliseconds..."); try { // Simulate the delay. Thread.sleep(fileSize); // MockUp transcript process. String client = api.clientCreate(); String stream = api.getStreamFromFile(rawFile); api.startStreamTranscription(client, stream); String transcript = api.getResult(); api.clientClose(client); ccFile = transcript; } catch (InterruptedException e) { e.printStackTrace(); } recording.setCcFileMock(ccFile); System.out.println(ccFile + " processed using AWSTranscribeAPI"); }

}

package google.api;

import java.util.ArrayList; import java.util.List;

/* This is a very rough and ugly Google Speech2Text API pretender * for academic purposes non related to API programming nor Speech2Text * recognition or whatsoever. * * But if you are curious on how the transcripts are actually done using * Google API you can see a real example at: * https://cloud.google.com/speech-to-text/docs/libraries#client-libraries-resources-java */ public class GoogleSpeech2TextAPI {

List transcriptAlternatives; String fileName;

public GoogleSpeech2TextAPI() { transcriptAlternatives = new ArrayList(); }

public String instantiateClient() { return "speechClient"; }

public String fileToMemory(String fileName) { this.fileName = fileName; return "audioBytes"; }

public String buildSyncRecognizeRequestConfig() { return "config"; }

public String buildSyncRecognizeRequestAudio() { return "audio"; }

public void performSpeechRecognition(String config, String audio) { // Mock-up results. System.out.println("Performing Speech Recognition based on " + config + " for " + audio); transcriptAlternatives.add(""); transcriptAlternatives.add(""); }

public String getFirstTranscriptAlternative() { for (String result : transcriptAlternatives) { // Returns the first element. System.out.println("Returning " + result); return result + " for " + this.fileName; } return null; }

public List getResultList() { return transcriptAlternatives; } public String recognitionAudio( ) { return null; } public void setRecognitionConfigParameters() { System.out.println("Set parameters"); } }

package aws.api;

import java.util.ArrayList; import java.util.List;

/* This is a very rough and ugly AWS Transcribe API pretender * for academic purposes non related to API programming nor Speech2Text * recognition or whatsoever. * * But if you are curious on how the transcripts are actually done using * AWS API you can find real examples at: * https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/transcribe */ public class AWSTranscribeAPI {

List transcript; String fileName;

public AWSTranscribeAPI() { transcript = new ArrayList(); }

public String getStreamFromFile(String audioFileName) { this.fileName = audioFileName; return "streamFromFile"; }

public void startStreamTranscription(String client, String stream) { System.out.println(client + " is starting streaming " + stream); transcript.add(" for " + this.fileName); for (int i = 1; i <= 5; i++) { transcript.add("line-" + i); } }

public String getResult() { String transcriptString = "";

for (String s : transcript) { transcriptString += s + "t"; } return transcriptString; }

public String clientCreate() { return "client"; }

public void clientClose(String client) { System.out.println("Closing " + client); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

For the Proxy Pattern implementation package loggy import googleapiGoogleSpeech2TextAPI public class GoogleSpeech2TextProxy private GoogleSpeech2TextA... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!