How to call a method in Java through an intermediary layer?

Raphael Jones :

This is part of a challenging programming assignment for a Software Engineering course. The objective is to add a layer of security between a NetworkLayerInterface and an ApplicationLayerInterface. This intermediary layer is called PresentationLayerInterface.

The abstract methods which are part of the UML diagrams for the Network Layer are:

openConnection()
closeConnection()
sendMessage(String)
receiveMessage(String)
getPresentationLayer()
setPresentationLayer()

The abstract methods which are part of the UML diagrams for the Application Layer are:

start()
stop()
sendMessage(String)
receiveMessage(String)
getPresentationLayer()
setPresentationLayer()

The abstract methods which are part of the UML diagrams for the Presentation Layer are:

start()
stop()
sendMessage(String)
receiveMessage(String)
getNetworkLayer()
setNetworkLayer()
getApplicationLayer()
setApplicationLayer()

The start() method looks like the following in the initial implementation of the Application Layer that does not use the PresentationLayer as an intermediary.

@Override
public void start() {
    //TODO: Part 2: Replace with method invocation of the the presentation layer
    networkLayer.openConnection();
}

The openConnection() method inside of the start() method is implemented in the TCPNetwork.java file which implements the Network Layer Interface. It is as follows:

@Override
public void openConnection() {
    System.out.println("Connecting to server ...");
    try {
        socket = new Socket(host, port);
        socketInput = new Scanner(new InputStreamReader(socket.getInputStream()));
        socketOutput = new PrintWriter(socket.getOutputStream(), true);
        waitForIncommingMessages();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Connection established.");
 }

How do I call the method start() if I can no longer (according to the assignment) do so using the networkLayer.openConnection(); inside of it?

I have tried the following for example:

public void start() {
//TODO: Part 2: Replace with method invocation of the the presentation layer
//networkLayer.openConnection(); Originally this
//presentationLayer.start();
//presentationLayer.openConnection(); Can't add method to Presentation Interface

}

I am really amateur Java user so please do not make assumptions about pre-existing knowledge.

MAZAR Abdellah :

My answer will join @codebrane comment.

if you would like to call openConnection() methode from PresentationLayer, you have to get your NetworkLayer object to make the call, so you need to call presentationLayer.getNetworkLayer().openConnection()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24196&siteId=1