PatternAdapter: Initial commit
This commit is contained in:
10
System Development/PatternAdapter/.classpath
Normal file
10
System Development/PatternAdapter/.classpath
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
17
System Development/PatternAdapter/.project
Normal file
17
System Development/PatternAdapter/.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>PatternAdapter</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=11
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=11
|
||||||
94
System Development/PatternAdapter/src/Adapter.java
Normal file
94
System Development/PatternAdapter/src/Adapter.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
interface ILightningPhone {
|
||||||
|
void recharge();
|
||||||
|
void useLightning();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IMicroUsbPhone {
|
||||||
|
void recharge();
|
||||||
|
void useMicroUsb();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Iphone implements ILightningPhone {
|
||||||
|
private boolean connector;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void useLightning() {
|
||||||
|
connector = true;
|
||||||
|
System.out.println("Lightning connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recharge() {
|
||||||
|
if (connector) {
|
||||||
|
System.out.println("Recharge started");
|
||||||
|
System.out.println("Recharge finished");
|
||||||
|
} else {
|
||||||
|
System.out.println("Connect Lightning first");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Android implements IMicroUsbPhone {
|
||||||
|
private boolean connector;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void useMicroUsb() {
|
||||||
|
connector = true;
|
||||||
|
System.out.println("MicroUsb connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recharge() {
|
||||||
|
if (connector) {
|
||||||
|
System.out.println("Recharge started");
|
||||||
|
System.out.println("Recharge finished");
|
||||||
|
} else {
|
||||||
|
System.out.println("Connect MicroUsb first");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* exposing the target interface while wrapping source object */
|
||||||
|
class LightningToMicroUsbAdapter implements IMicroUsbPhone {
|
||||||
|
private final ILightningPhone lightningPhone;
|
||||||
|
|
||||||
|
public LightningToMicroUsbAdapter(ILightningPhone lightningPhone) {
|
||||||
|
this.lightningPhone = lightningPhone;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void useMicroUsb() {
|
||||||
|
System.out.println("MicroUsb connected");
|
||||||
|
lightningPhone.useLightning();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recharge() {
|
||||||
|
lightningPhone.recharge();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Adapter {
|
||||||
|
static void rechargeMicroUsbPhone(IMicroUsbPhone phone) {
|
||||||
|
phone.useMicroUsb();
|
||||||
|
phone.recharge();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rechargeLightningPhone(ILightningPhone phone) {
|
||||||
|
phone.useLightning();
|
||||||
|
phone.recharge();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Android android = new Android();
|
||||||
|
Iphone iPhone = new Iphone();
|
||||||
|
|
||||||
|
System.out.println("Recharging android with MicroUsb");
|
||||||
|
rechargeMicroUsbPhone(android);
|
||||||
|
|
||||||
|
System.out.println("Recharging iPhone with Lightning");
|
||||||
|
rechargeLightningPhone(iPhone);
|
||||||
|
|
||||||
|
System.out.println("Recharging iPhone with MicroUsb");
|
||||||
|
rechargeMicroUsbPhone(new LightningToMicroUsbAdapter (iPhone));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user