Last active
November 10, 2018 03:20
-
-
Save adammb86/1ade8a78c4386d8ef22c9038535b92f7 to your computer and use it in GitHub Desktop.
Design Pattern - Factory - Sample Case
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
(Factory) Langkah Pembuatan: | |
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