Created
December 12, 2018 12:01
-
-
Save Arsfiqball/333a2118790bb755dffeadad18d77954 to your computer and use it in GitHub Desktop.
Object Oriented Programming tentang "Komunikasi antar objek" dimana ada 4 metode dasar, diantaranya 1) Direct, 2) Injection, 3) Interface, 4) Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
class Charger { | |
public int getElectricity() { | |
return 10; | |
} | |
} | |
class Handphone { | |
private Charger charger = new Charger(); | |
private Integer battery = 0; | |
public void charge() { | |
this.battery = this.battery + this.charger.getElectricity(); | |
} | |
public int checkBatteryStatus() { | |
return this.battery; | |
} | |
} | |
public class Contoh1 { | |
public static void main(String[] args) { | |
Handphone hp = new Handphone(); | |
while (hp.checkBatteryStatus() < 100) { | |
System.out.print("Mengisi daya..."); | |
System.out.println(hp.checkBatteryStatus()); | |
hp.charge(); | |
} | |
System.out.print("Selesai..."); | |
System.out.println(hp.checkBatteryStatus()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
class ChargerSamsung { | |
public int streamElectricity() { | |
return 5; | |
} | |
} | |
class ChargerASUS { | |
public int getElectricity() { | |
return 10; | |
} | |
} | |
// ======================================================================== | |
class Handphone { | |
private ChargerASUS charger = null; | |
private Integer battery = 0; | |
public void setCharger(ChargerASUS charger) { | |
this.charger = charger; | |
} | |
public void charge() { | |
this.battery = this.battery + this.charger.getElectricity(); | |
} | |
public int checkBatteryStatus() { | |
return this.battery; | |
} | |
} | |
public class Contoh2 { | |
public static void main(String[] args) { | |
ChargerASUS charger = new ChargerASUS(); | |
// ChargerSamsung charger = new ChargerSamsung(); | |
Handphone hp = new Handphone(); | |
hp.setCharger(charger); | |
while (hp.checkBatteryStatus() < 100) { | |
System.out.print("Mengisi daya..."); | |
System.out.println(hp.checkBatteryStatus()); | |
hp.charge(); | |
} | |
System.out.print("Selesai..."); | |
System.out.println(hp.checkBatteryStatus()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
interface ConnectorUSBC { | |
public int supplyElectricity(); | |
} | |
class ChargerXiaomi implements ConnectorUSBC { | |
public int supplyElectricity() { | |
return 20; | |
} | |
} | |
// ======================================================================== | |
interface ConnectorUSB2 { | |
public int getElectricity(); | |
} | |
class ChargerSamsung implements ConnectorUSB2 { | |
public int getElectricity() { | |
return 5; | |
} | |
} | |
class ChargerASUS implements ConnectorUSB2 { | |
public int getElectricity() { | |
return 10; | |
} | |
} | |
// ======================================================================== | |
class Handphone { | |
private ConnectorUSB2 charger = null; | |
private Integer battery = 0; | |
public void setCharger(ConnectorUSB2 charger) { | |
this.charger = charger; | |
} | |
public void charge() { | |
this.battery = this.battery + this.charger.getElectricity(); | |
} | |
public int checkBatteryStatus() { | |
return this.battery; | |
} | |
} | |
public class Contoh3 { | |
public static void main(String[] args) { | |
// ChargerXiaomi charger = new ChargerXiaomi(); | |
// ChargerASUS charger = new ChargerASUS(); | |
ChargerSamsung charger = new ChargerSamsung(); | |
Handphone hp = new Handphone(); | |
hp.setCharger(charger); | |
while (hp.checkBatteryStatus() < 100) { | |
System.out.print("Mengisi daya..."); | |
System.out.println(hp.checkBatteryStatus()); | |
hp.charge(); | |
} | |
System.out.print("Selesai..."); | |
System.out.println(hp.checkBatteryStatus()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
interface ConnectorUSBC { | |
public int supplyElectricity(); | |
} | |
class ChargerXiaomi implements ConnectorUSBC { | |
public int supplyElectricity() { | |
return 20; | |
} | |
} | |
// ======================================================================== | |
interface ConnectorUSB2 { | |
public int getElectricity(); | |
} | |
class ChargerSamsung implements ConnectorUSB2 { | |
public int getElectricity() { | |
return 5; | |
} | |
} | |
class ChargerASUS implements ConnectorUSB2 { | |
public int getElectricity() { | |
return 10; | |
} | |
} | |
// ======================================================================== | |
class AdapterUSBCtoUSB2 implements ConnectorUSB2 { | |
private ConnectorUSBC charger = null; | |
public void setCharger(ConnectorUSBC charger) { | |
this.charger = charger; | |
} | |
public int getElectricity() { | |
return this.charger.supplyElectricity(); | |
} | |
} | |
// ======================================================================== | |
class Handphone { | |
private ConnectorUSB2 charger = null; | |
private Integer battery = 0; | |
public void setCharger(ConnectorUSB2 charger) { | |
this.charger = charger; | |
} | |
public void charge() { | |
this.battery = this.battery + this.charger.getElectricity(); | |
} | |
public int checkBatteryStatus() { | |
return this.battery; | |
} | |
} | |
public class Contoh4 { | |
public static void main(String[] args) { | |
ChargerXiaomi charger = new ChargerXiaomi(); | |
AdapterUSBCtoUSB2 adapter = new AdapterUSBCtoUSB2(); | |
adapter.setCharger(charger); | |
// ChargerASUS charger = new ChargerASUS(); | |
// ChargerSamsung charger = new ChargerSamsung(); | |
Handphone hp = new Handphone(); | |
hp.setCharger(adapter); | |
// hp.setCharger(charger); | |
while (hp.checkBatteryStatus() < 100) { | |
System.out.print("Mengisi daya..."); | |
System.out.println(hp.checkBatteryStatus()); | |
hp.charge(); | |
} | |
System.out.print("Selesai..."); | |
System.out.println(hp.checkBatteryStatus()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment