Skip to content

Commit 6b10f4b

Browse files
committed
Adding appropriate comments on classes and full description in App.java. Removing added function in ServiceAmbassador as it's not appropriate for the example.
1 parent 73925ce commit 6b10f4b

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.ambassador;
224

25+
/**
26+
*
27+
* The ambassador pattern creates a helper service that sends network requests on behalf of a
28+
* client. It is often used in cloud-based applications to offload features of a remote service.
29+
*
30+
* An ambassador service can be thought of as an out-of-process proxy that is co-located with
31+
* the client. Similar to the proxy design pattern, the ambassador service provides an interface
32+
* for another remote service. In addition to the interface, the ambassador provides extra
33+
* functionality and features, specifically offloaded common connectivity tasks. This usually
34+
* consists of monitoring, logging, routing, security etc. This is extremely useful in
35+
* legacy applications where the codebase is difficult to modify and allows for improvements
36+
* in the application's networking capabilities.
37+
*
38+
* In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while the
39+
* ({@link RemoteService}) class represents a remote application.
40+
*
41+
*/
342
public class App {
443

544
/**
@@ -9,11 +48,7 @@ public static void main(String[] args) {
948

1049
Client host1 = new Client();
1150
Client host2 = new Client();
12-
1351
host1.useService(12);
1452
host2.useService(73);
15-
16-
host1.useNewService(12);
17-
host2.useNewService(73);
1853
}
1954
}

ambassador/src/main/java/com/iluwatar/ambassador/Client.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
/**
26+
* A simple Client
27+
*/
2528
public class Client {
2629

2730
private ServiceAmbassador serviceAmbassador;
@@ -34,9 +37,4 @@ void useService(int value) {
3437
long result = serviceAmbassador.doRemoteFunction(value);
3538
System.out.println(result);
3639
}
37-
38-
void useNewService(int value) {
39-
long result = serviceAmbassador.doAddedFunction(value);
40-
System.out.println(result);
41-
}
4240
}

ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import static java.lang.Thread.sleep;
2626

27+
/**
28+
* A remote legacy application represented by a Singleton implementation.
29+
*/
2730
public class RemoteService implements RemoteServiceInterface {
2831

2932
private static RemoteService service = null;
@@ -39,6 +42,12 @@ private RemoteService() {
3942

4043
}
4144

45+
/**
46+
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
47+
* Will sometimes return -1. This immitates connectivity issues a client might have to account for.
48+
* @param value integer value to be multiplied.
49+
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
50+
*/
4251
@Override
4352
public long doRemoteFunction(int value) {
4453

ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
/**
26+
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
27+
*/
2528
interface RemoteServiceInterface {
2629

2730
long doRemoteFunction(int value) throws Exception;

ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727

2828
import static java.lang.Thread.sleep;
2929

30+
/**
31+
*
32+
* ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}).
33+
* The interface adds logging, latency testing and usage of the service in a safe way that will not
34+
* add stress to the remote service when connectivity issues occur.
35+
*
36+
*/
3037
public class ServiceAmbassador implements RemoteServiceInterface {
3138

3239
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
@@ -43,10 +50,6 @@ public long doRemoteFunction(int value) {
4350
return safeCall(value);
4451
}
4552

46-
long doAddedFunction(int value) {
47-
return safeCall(value) * 5;
48-
}
49-
5053
private long checkLatency(int value) {
5154
RemoteService service = RemoteService.getRemoteService();
5255
long startTime = System.currentTimeMillis();

0 commit comments

Comments
 (0)