12.07.2015 Views

Podsumowanie wzorców projektowych GoF

Podsumowanie wzorców projektowych GoF

Podsumowanie wzorców projektowych GoF

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6. ProxyPrzeznaczenie (cytat z ksiki <strong>GoF</strong>):Zapewnia substytut lub reprezentanta innego obiektu w celu sterowania dostpem do niego.Przeznaczenie (cytat z fluffycat):Jedna klasa steruje tworzeniem i dostpem do obiektów innej klasy.Opis: Klient w restauracji zamawia filiank herbaty. Zamawia j u kelnera. Kelnerprzekazuje danie do osoby zajmujcej si parzeniem herbaty. Sam si nie zajmuje jednakrealizacj zamówienia. Wydaje jedynie polecenia: we filiank i zaparz w niej herbat.Kelner działa tu jak proxy.Sens tworzenia proxy z samej filianki herbaty jest dla mnie niejasny.Problem: JakRozwizanie: PrzezKlasa główna:_DP_S_<strong>GoF</strong>_Proxypackage pk.dydakt.to.dp.s.gof.proxy;public class _DP_S_<strong>GoF</strong>_Proxy {}public static void main(String[] args) {System.out.println("TestProxy: instantiating PotOfTeaProxy");PotOfTeaInterface potOfTea = new PotOfTeaProxy();System.out.println(" ");System.out.println("TestProxy: pouring tea");potOfTea.pourTea();}Klasy wzorca:PotOfTeaInterfacepackage pk.dydakt.to.dp.s.gof.proxy;//PotOfTeaInterface will insure that the proxy//has the same methods as it's real subjectpublic interface PotOfTeaInterface {public void pourTea();}PotOfTeapackage pk.dydakt.to.dp.s.gof.proxy;public class PotOfTea implements PotOfTeaInterface {public PotOfTea() {System.out.println("Making a pot of tea");}}public void pourTea() {System.out.println("Pouring tea");}PotOfTeaProxy41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!