diff --git a/Adapter_Ilalov/Android.ts b/Adapter_Ilalov/Android.ts new file mode 100644 index 0000000..93dd367 --- /dev/null +++ b/Adapter_Ilalov/Android.ts @@ -0,0 +1,4 @@ +export interface Android { + //method that belongs only to Android + useMicroUSB(); +} \ No newline at end of file diff --git a/Adapter_Ilalov/AndroidImpl.ts b/Adapter_Ilalov/AndroidImpl.ts new file mode 100644 index 0000000..18737de --- /dev/null +++ b/Adapter_Ilalov/AndroidImpl.ts @@ -0,0 +1,9 @@ +import {Android} from "./Android"; + + +export class AndroidImpl implements Android { + //realisation method + useMicroUSB() { + console.log("Using micro USB") + } +} \ No newline at end of file diff --git a/Adapter_Ilalov/IPhone.ts b/Adapter_Ilalov/IPhone.ts new file mode 100644 index 0000000..50f3b39 --- /dev/null +++ b/Adapter_Ilalov/IPhone.ts @@ -0,0 +1,4 @@ +export interface IPhone { + //method that belongs only to iPhone + useLightning(); +} \ No newline at end of file diff --git a/Adapter_Ilalov/LightningToMicroUSBAdapter.ts b/Adapter_Ilalov/LightningToMicroUSBAdapter.ts new file mode 100644 index 0000000..cd42910 --- /dev/null +++ b/Adapter_Ilalov/LightningToMicroUSBAdapter.ts @@ -0,0 +1,17 @@ +import {Android} from "./Android"; +import {IPhone} from "./IPhone"; + +export class LightningToMicroUSBAdapter implements Android { + // Class iPhoneImpl does not have a useMicroUSB, so we can create an adapter + iphoneDevice: IPhone; + + constructor(iphone: IPhone) { + this.iphoneDevice = iphone; + } + + public useMicroUSB() { + console.log("Want to use micro USB, converting to Lightning") + //calls the method useLightning instead + this.iphoneDevice.useLightning(); + } +} \ No newline at end of file diff --git a/Adapter_Ilalov/README.md b/Adapter_Ilalov/README.md new file mode 100644 index 0000000..efa16d5 --- /dev/null +++ b/Adapter_Ilalov/README.md @@ -0,0 +1,12 @@ +# Adapter Pattern +# The adapter pattern allows you to make different classes with different interfaces work together, without changing their source code. +## Execution + +* **Step 1**: Run the typescript file with the following command. This will create a javascript file from typescript automatically with the same name. +```bash +tsc main.ts +``` +* **Step 2**: Now run the javascript file +```bash +node main.js +``` \ No newline at end of file diff --git a/Adapter_Ilalov/iPhoneImpl.ts b/Adapter_Ilalov/iPhoneImpl.ts new file mode 100644 index 0000000..68d09ff --- /dev/null +++ b/Adapter_Ilalov/iPhoneImpl.ts @@ -0,0 +1,8 @@ +import {IPhone} from "./IPhone"; + +export class iPhoneImpl implements IPhone { + //realisation method + useLightning() { + console.log("Using lightning port") + } +} \ No newline at end of file diff --git a/Adapter_Ilalov/main.ts b/Adapter_Ilalov/main.ts new file mode 100644 index 0000000..b81d4d3 --- /dev/null +++ b/Adapter_Ilalov/main.ts @@ -0,0 +1,8 @@ +import {iPhoneImpl} from "./iPhoneImpl"; +import {LightningToMicroUSBAdapter} from "./LightningToMicroUSBAdapter"; + +//functional test +let iphone7 = new iPhoneImpl(); +let chargeAdapter = new LightningToMicroUSBAdapter(iphone7); + +chargeAdapter.useMicroUSB() \ No newline at end of file