Skip to content

Adapter design pattern #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Adapter Design Pattern
* Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
* The Adapter Pattern is also known as Wrapper.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/adapter-pattern/diagrams/4_1%20Adapter%20Design%20Pattern.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/adapter-pattern/diagrams/4_1%20Adapter%20design%20pattern%20-%20sequence%20diagram.png "Diagram")


### When to use Adapter Design Pattern
When you want to create a reusable class that cooperates with classes which don't have compatible interfaces.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added diagrams/4_1 Adapter Design Pattern.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package com.premaseem;

import com.premaseem.phones.GooglePhone;
import com.premaseem.phones.IPhone;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Adapter Design Pattern Example ");

// Created instances of devices
IPhone iPhone = new IPhone();
GooglePhone googlePhone = new GooglePhone();
EarPlug earPlug = new EarPlug();

// Ear plug is able to take iPhone sound signal
String soundSignal = iPhone.getSoundOutput();
earPlug.takeSoundInput(soundSignal);

// Ear plug is not able to take google phone sound signal
Integer soundSignals = googlePhone.getSoundOutput();

// Created adapter to convert signals expected by client
EarPlugAdapter earPlugAdapter = new EarPlugAdapter();
earPlug.takeSoundInput(earPlugAdapter.convertSoundSignal(soundSignals));

}
}
15 changes: 15 additions & 0 deletions pattern/src/com/premaseem/EarPlug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class EarPlug {

public void takeSoundInput(String inputSignal){
System.out.println("playing input signal");
System.out.println(inputSignal);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
public class EarPlugAdapter {

public String convertSoundSignal(Integer soundSignal){
return soundSignal.toString();
}
}
14 changes: 14 additions & 0 deletions pattern/src/com/premaseem/phones/GooglePhone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.premaseem.phones;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class GooglePhone {

public Integer getSoundOutput(){

return 1001010101;
}
}
14 changes: 14 additions & 0 deletions pattern/src/com/premaseem/phones/IPhone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.premaseem.phones;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class IPhone {

public String getSoundOutput(){

return "1001010101";
}
}
66 changes: 66 additions & 0 deletions patternBonus/src/com/premaseem/adapterPattern/Adapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.premaseem.adapterPattern;

public interface Adapter {

}

class AmazonCloudAdapter implements ClientBrokerInterface {
AmazonCloudProvider adapteeProvider = new AmazonCloudProvider();

@Override
public void start(String id) {
adapteeProvider.startVM(id);

}

@Override
public void stop(String id) {
adapteeProvider.stopVM(id);
}

@Override
public void restart(String id) {
adapteeProvider.rebootVM(id);
}
}

class AzureCloudAdapter implements ClientBrokerInterface {
AzureCloudProvider adapteeProvider = new AzureCloudProvider();

@Override
public void start(String id) {
adapteeProvider.bootVM(id, "");

}

@Override
public void stop(String id) {
adapteeProvider.terminateVM(id, "");
}

@Override
public void restart(String id) {
adapteeProvider.rebootVM(id);
}
}

class GoogleCloudAdapter implements ClientBrokerInterface {
GoogleCloudProvider adapteeProvider = new GoogleCloudProvider();

@Override
public void start(String id) {
adapteeProvider.startVM(id);

}

@Override
public void stop(String id) {
adapteeProvider.shutdownVM(id);
}

@Override
public void restart(String id) {
adapteeProvider.shutdownVM(id);
adapteeProvider.startVM(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.premaseem.adapterPattern;

public interface ClientBrokerInterface {
/*
* This method will help you to start your vm
*/
void start(String id);
void stop(String id);
void restart(String id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.premaseem.adapterPattern;

import java.util.Scanner;

public class ClientForCloudAdapter {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int repeatRunFlag = 1;


System.out.println("This is the Cloud Broker POC which uses command pattern for provisioning for your resources on different cloud provider ");

System.out.println("Please pick the provider to create VDC (virtual data Center) ");
System.out.println(" Press 1 for Amazon Cloud");
System.out.println(" Press 2 for Azure Cloud");
System.out.println(" Press 3 for Google Cloud ");
int vdcType = scan.nextInt();

ClientBrokerInterface clientinterface;

switch (vdcType) {
case 1:
clientinterface = new AmazonCloudAdapter();
break;
case 2:
clientinterface = new AzureCloudAdapter();
break;
case 3:
clientinterface = new GoogleCloudAdapter();
break;
default :
clientinterface = new AmazonCloudAdapter();
}


while (repeatRunFlag == 1) {
System.out.println(" What do you want to do ");
System.out.println(" 1. Start a VM ");
System.out.println(" 2. Stop a VM ");
System.out.println(" 3. Reboot a VM");

int taskType = scan.nextInt();
switch (taskType) {
case 1:
clientinterface.start("");
break;
case 2:clientinterface.stop("");
break;
case 3:clientinterface.restart("");
break;
}
System.out.println("Do you want to add more tasks before provisioning 1-yes / 0-No and trigger provisioning ");
try {
repeatRunFlag = scan.nextInt();
} catch (Exception e) {
repeatRunFlag = 0;
}
}
}
}
45 changes: 45 additions & 0 deletions patternBonus/src/com/premaseem/adapterPattern/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.premaseem.adapterPattern;

class AmazonCloudProvider {
public void startVM(String id){
System.out.println(this.getClass().getSimpleName() + " start ");
}

public void stopVM(String id){
System.out.println(this.getClass().getSimpleName() + " stop ");
}

public void rebootVM(String id){
System.out.println(this.getClass().getSimpleName() + " reboot ");
}
}

class AzureCloudProvider {

public void bootVM(String id, String vdc){
System.out.println(this.getClass().getSimpleName() + " start ");
}

public void terminateVM(String id, String vdc){
System.out.println(this.getClass().getSimpleName() + " stop ");
}

public void rebootVM(String id){
System.out.println(this.getClass().getSimpleName() + " reboot ");
}
}

class GoogleCloudProvider {
public void startVM(String id){
System.out.println(this.getClass().getSimpleName() + " start ");
}

public void shutdownVM(String id){
System.out.println(this.getClass().getSimpleName() + " stop ");
}
}

public interface Provider {

}