Skip to content

Commit 2cd8809

Browse files
committed
Adapter design pattern - cloud matrix example
This example creates the adapter layer to operate various services from different clouds.
1 parent 6deb6b9 commit 2cd8809

5 files changed

Lines changed: 183 additions & 13 deletions

File tree

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.premaseem.adapterPattern;
2+
3+
public interface Adapter {
4+
5+
}
6+
7+
class AmazonCloudAdapter implements ClientBrokerInterface {
8+
AmazonCloudProvider adapteeProvider = new AmazonCloudProvider();
9+
10+
@Override
11+
public void start(String id) {
12+
adapteeProvider.startVM(id);
13+
14+
}
15+
16+
@Override
17+
public void stop(String id) {
18+
adapteeProvider.stopVM(id);
19+
}
20+
21+
@Override
22+
public void restart(String id) {
23+
adapteeProvider.rebootVM(id);
24+
}
25+
}
26+
27+
class AzureCloudAdapter implements ClientBrokerInterface {
28+
AzureCloudProvider adapteeProvider = new AzureCloudProvider();
29+
30+
@Override
31+
public void start(String id) {
32+
adapteeProvider.bootVM(id, "");
33+
34+
}
35+
36+
@Override
37+
public void stop(String id) {
38+
adapteeProvider.terminateVM(id, "");
39+
}
40+
41+
@Override
42+
public void restart(String id) {
43+
adapteeProvider.rebootVM(id);
44+
}
45+
}
46+
47+
class GoogleCloudAdapter implements ClientBrokerInterface {
48+
GoogleCloudProvider adapteeProvider = new GoogleCloudProvider();
49+
50+
@Override
51+
public void start(String id) {
52+
adapteeProvider.startVM(id);
53+
54+
}
55+
56+
@Override
57+
public void stop(String id) {
58+
adapteeProvider.shutdownVM(id);
59+
}
60+
61+
@Override
62+
public void restart(String id) {
63+
adapteeProvider.shutdownVM(id);
64+
adapteeProvider.startVM(id);
65+
}
66+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.premaseem.adapterPattern;
2+
3+
public interface ClientBrokerInterface {
4+
/*
5+
* This method will help you to start your vm
6+
*/
7+
void start(String id);
8+
void stop(String id);
9+
void restart(String id);
10+
11+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.premaseem.adapterPattern;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientForCloudAdapter {
6+
7+
public static void main(String[] args) {
8+
Scanner scan = new Scanner(System.in);
9+
int repeatRunFlag = 1;
10+
11+
12+
System.out.println("This is the Cloud Broker POC which uses command pattern for provisioning for your resources on different cloud provider ");
13+
14+
System.out.println("Please pick the provider to create VDC (virtual data Center) ");
15+
System.out.println(" Press 1 for Amazon Cloud");
16+
System.out.println(" Press 2 for Azure Cloud");
17+
System.out.println(" Press 3 for Google Cloud ");
18+
int vdcType = scan.nextInt();
19+
20+
ClientBrokerInterface clientinterface;
21+
22+
switch (vdcType) {
23+
case 1:
24+
clientinterface = new AmazonCloudAdapter();
25+
break;
26+
case 2:
27+
clientinterface = new AzureCloudAdapter();
28+
break;
29+
case 3:
30+
clientinterface = new GoogleCloudAdapter();
31+
break;
32+
default :
33+
clientinterface = new AmazonCloudAdapter();
34+
}
35+
36+
37+
while (repeatRunFlag == 1) {
38+
System.out.println(" What do you want to do ");
39+
System.out.println(" 1. Start a VM ");
40+
System.out.println(" 2. Stop a VM ");
41+
System.out.println(" 3. Reboot a VM");
42+
43+
int taskType = scan.nextInt();
44+
switch (taskType) {
45+
case 1:
46+
clientinterface.start("");
47+
break;
48+
case 2:clientinterface.stop("");
49+
break;
50+
case 3:clientinterface.restart("");
51+
break;
52+
}
53+
System.out.println("Do you want to add more tasks before provisioning 1-yes / 0-No and trigger provisioning ");
54+
try {
55+
repeatRunFlag = scan.nextInt();
56+
} catch (Exception e) {
57+
repeatRunFlag = 0;
58+
}
59+
}
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.premaseem.adapterPattern;
2+
3+
class AmazonCloudProvider {
4+
public void startVM(String id){
5+
System.out.println(this.getClass().getSimpleName() + " start ");
6+
}
7+
8+
public void stopVM(String id){
9+
System.out.println(this.getClass().getSimpleName() + " stop ");
10+
}
11+
12+
public void rebootVM(String id){
13+
System.out.println(this.getClass().getSimpleName() + " reboot ");
14+
}
15+
}
16+
17+
class AzureCloudProvider {
18+
19+
public void bootVM(String id, String vdc){
20+
System.out.println(this.getClass().getSimpleName() + " start ");
21+
}
22+
23+
public void terminateVM(String id, String vdc){
24+
System.out.println(this.getClass().getSimpleName() + " stop ");
25+
}
26+
27+
public void rebootVM(String id){
28+
System.out.println(this.getClass().getSimpleName() + " reboot ");
29+
}
30+
}
31+
32+
class GoogleCloudProvider {
33+
public void startVM(String id){
34+
System.out.println(this.getClass().getSimpleName() + " start ");
35+
}
36+
37+
public void shutdownVM(String id){
38+
System.out.println(this.getClass().getSimpleName() + " stop ");
39+
}
40+
}
41+
42+
public interface Provider {
43+
44+
}
45+

0 commit comments

Comments
 (0)