Last active
November 9, 2018 14:09
-
-
Save Tiorzfk/ee65d373379803a9901b846ab48cf55a to your computer and use it in GitHub Desktop.
(Factory) 1. Interface BangunDatar.java 2. Persegi.java dan Lingkaran.java 3. BangunDatarFactory.java 4. BangunDatarFactoryDemo.java
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
public interface BangunDatar { | |
void gambarBangun(); //abstract | |
} |
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
public class BangunDatarFactory { | |
//factory method | |
public BangunDatar getBangunDatar(String namaBangunDatar){ | |
if(namaBangunDatar.equalsIgnoreCase("persegi")) | |
return new Persegi(); | |
else if(namaBangunDatar.equalsIgnoreCase("lingkaran")) | |
return new Lingkaran(); | |
else | |
return null; | |
} | |
} |
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
public class BangunDatarFactoryDemo { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
BangunDatarFactory bangundatarfactory = new BangunDatarFactory(); | |
BangunDatar persegi=bangundatarfactory.getBangunDatar("Persegi"); | |
//otomatis menciptakan objek persegi | |
persegi.gambarBangun(); | |
} | |
} |
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
public class Lingkaran implements BangunDatar{ | |
//atribut | |
private double r; | |
//method | |
public double getR() { | |
return r; | |
} | |
public void setR(double r) { | |
this.r = r; | |
} | |
@Override | |
public void gambarBangun() { | |
// TODO Auto-generated method stub | |
System.out.println("Saya adalah lingkaran"); | |
} | |
} |
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
public class Persegi implements BangunDatar{ | |
//atribut | |
private double sisi; | |
public double getSisi() { | |
return sisi; | |
} | |
public void setSisi(double sisi) { | |
this.sisi = sisi; | |
} | |
@Override | |
public void gambarBangun() { | |
// TODO Auto-generated method stub | |
System.out.println("Saya adalah persegi"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment