Facade Design Pattern
Pengertian
Facade pattern adalah jenis design pattern yang dapat menyembunyikan kompleksitas sistem dan menyediakan interface untuk klien yang dimana klien dapat mengakses sistem. Facade pattern ini merupakan structural design pattern karena facade pattern ini menambahkan interface untuk sistem yang ada untuk menyembunyikan kompleksitasnya..Secara mudahnya facade pattern menyembunyikan kompleksitas dari suatu subsistem ke dalam satu class facade. Yang dimana class tersebut akan diakses oleh klien sehingga klien dimudahkan untuk mengakses sistem.
Untuk memudahkan pemahaman, facade pattern dapat dianalogikan dengan kehidupan nyata. Sebagai contoh customer service. Ketika klien memiliki masalah atau keluhan seperti masalah pengiriman atau masalah kualitas barang, maka klien cukup hanya menghubungi customer service. Klien tidak perlu tahu bagaimana prosedur customer service dalam menyelesaikan masalah tersebut.
Kelebihan
memudahkan klien untuk mengakses sistem, karena klien cukup mepelajari class facadenya saja, sehingga mudah untuk di implementasikanKonsekuensi
jika ada perubahan pada design pada sistem maka class facade juga mengalami perubahan.UML Class Diagram
Implementation
Kasus : Penjual Handphone
Pada diagram class terdapat client, facade class, dan subsistem. FacadePatternClient menjadi client, shopKeeper adalah facade class. Client dapat mengakses subsistem hanya dengan menggunakan facade class.
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 MobileShop { | |
public void modelNo(); | |
public void price(); | |
} |
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 Iphone implements MobileShop { | |
@Override | |
public void modelNo() { | |
System.out.println(" Iphone 6 "); | |
} | |
@Override | |
public void price() { | |
System.out.println(" Rs 65000.00 "); | |
} |
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 ShopKeeper { | |
private MobileShop iphone; | |
private MobileShop samsung; | |
private MobileShop blackberry; | |
public ShopKeeper(){ | |
iphone= new Iphone(); | |
samsung=new Samsung(); | |
blackberry=new Blackberry(); | |
} | |
public void iphoneSale(){ | |
iphone.modelNo(); | |
iphone.price(); | |
} | |
public void samsungSale(){ | |
samsung.modelNo(); | |
samsung.price(); | |
} | |
public void blackberrySale(){ | |
blackberry.modelNo(); | |
blackberry.price(); | |
} | |
} |
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 Samsung implements MobileShop { | |
@Override | |
public void modelNo() { | |
System.out.println(" Samsung galaxy tab 3 "); | |
} | |
@Override | |
public void price() { | |
System.out.println(" Rs 45000.00 "); | |
} | |
} |
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 Blackberry implements MobileShop { | |
@Override | |
public void modelNo() { | |
System.out.println(" Blackberry Z10 "); | |
} | |
@Override | |
public void price() { | |
System.out.println(" Rs 55000.00 "); | |
} | |
} |
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 FacadePatternClient { | |
public static void main(String args[]){ | |
ShopKeeper sk=new ShopKeeper(); | |
sk.iphoneSale(); | |
sk.samsungSale(); | |
} | |
} |
Comments
Post a Comment