Adapter - Sam647254/Programetoj GitHub Wiki
Java
interface LightningPhone {
void recharge();
void useLightning();
}
interface MicroUSBPhone {
void recharge();
void useMicroUSB();
}
class IPhone implements LightningPhone {
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 MicroUSBPhone {
private boolean connector;
@Override
public void useMicroUSB() {
connector = true;
System.out.println("Micro USB connected");
}
@Override
public void recharge() {
if (connector) {
System.out.println("Recharge started");
System.out.println("Recharge finished");
} else {
System.out.println("Connect MicroUSB first");
}
}
}
class LightningtoMicroUSBAdapter implements MicroUSBPhone {
private final LightningPhone lightningPhone;
public LightningToMicroUSBAdapter(LightningPhone phone) {
lightningPhone = phone;
}
@Override
public void useMicroUSB() {
System.out.println("Micro USB connected");
lightningPhone.useLightning();
}
@Override
public void recharge() {
lightningPhone.recharge();
}
}
// main
public class Example {
static void rechargeMicroUSBPhone(MicroUSBPhone phone) {
phone.useMicroUSB();
phone.recharge();
}
static void rechargeLightningPhone(LightningPhone phone) {
phone.useLightning();
phone.recharge();
}
public static void main(String[] arguments) {
var android = new Android();
var iPhone = new IPhone();
System.out.println("Recharge Android with Micro USB");
rechargeMicroUSBPhone(android);
System.out.println("Recharge iPhone with Lightning");
rechargeLightningPhone(iPhone);
System.out.println("Recharging iPhone with Micro USB");
rechargeMicroUSBPhone(new LightningToMicroUSBAdapter(iPhone));
}
}
TypeScript
interface LightningPhone {
recharge: () => void;
useLightning: () => void;
}
interface MicroUSBPhone {
recharge: () => void;
useMicroUSB: () => void;
}
function create_iPhone(): LightningPhone {
return {
connector: false,
recharge() {
if (this.connector) {
console.log("Recharge started");
console.log("Recharge finished");
} else {
console.log("Connect lightning first");
}
},
useLightning() {
this.connector = true;
console.log("Lightning connected");
}
};
}
function createAndroid(): MicroUSBPhone {
return {
connector: false,
recharge() {
if (this.connector) {
console.log("Recharge started");
console.log("Recharge finished");
} else {
console.log("Connect micro USB first");
}
},
useMicroUSB() {
this.connector = true;
console.log("Micro USB connected");
}
};
}
function adaptToMicroUSB(iPhone: LightningPhone): MicroUSBPhone {
return {
useMicroUSB() {
iPhone.useLightning();
console.log("Micro USB connected");
},
recharge() {
iPhone.recharge();
}
};
}
function chargeLightning(phone: LightningPhone) {
phone.useLightning();
phone.recharge();
}
function chargeMicroUSB(phone: MicroUSBPhone) {
phone.useMicroUSB();
phone.recharge();
}
// main
const iPhone = create_iPhone();
chargeLightning(iPhone);
const android = createAndroid();
chargeMicroUSB(android);
chargeMicroUSB(adaptToMicroUSB(iPhone));