Wzorce projektowe
Transkrypt
Wzorce projektowe
Wzorce projektowe Design Patterns Gang Czworga Design Patterns: Elements of Reusable Object-Oriented Software; Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides; Addison-Wesley, 1995 (the "Gang of Four" or just "GoF") Pattern-Oriented Software Architecture: A System of Patterns (POSA) Frank Buschmann, Regine Meunier, Hans Rohnert, Peter Sommerlad, Michael Stal; John Wiley and Sons, 1996 Czym są wzorce projektowe ● ● ● ● Wzorzec (projektowy) jest formą dokumentacji rozwiązania problemu programistycznego, w której w sposób zrozumiały przedstawiono istotę (problemu i jego rozwiazania). Wzorce próbują opisać sprawdzone rozwiązywania praktyczne (a nie spekulacje teoretyczne). Jednym z najbardziej podstawowych aspektów wzorców jest to Ŝe opisują one problemy, które były wielokrotnie rozwiązywane przez wiele osób (powtarzają się), które „dochodziły” niezaleŜnie do podobnych sposobów rozwiązania. Wzorce projektowe są zatem formą przekazywania doświadczeń programistycznych następnym pokoleniom. Singleton Pattern Jak skonstruować klasę aby zapwnić Ŝe bedzie moŜna utworzyć tylko jeden obiekt (jedną instancję) tej klasy? (Np. Centralny obiekt konfiguracyjny dostępny z dowolnego miejsca (dowolnego wątku) aplikacji) public class SingletonObject { private SingletonObject() { // no code req'd } } Teraz nie moŜemy utworzyć obiektu tak: SingletonObject singiel = new SingletonObject() ; Ale potrzebujemy jakiegoś sposobu pozyskania obiektu ... Singleton c.d. public class SingletonObject { private SingletonObject() { // no code req'd } public static SingletonObject getSingletonObject() { if (ref == null) // it's ok, we can call this constructor ref = new SingletonObject(); return ref; } private static SingletonObject ref; } getSingletonObject() z 2 wątków ? public class SingletonObject { private SingletonObject() { // no code req'd } public static synchronized SingletonObject getSingletonObject() { if (ref == null) // it's ok, we can call this constructor ref = new SingletonObject(); return ref; } private static SingletonObject ref; } getSingletonObject() z 2 wątków ? public class SingletonObject { private SingletonObject() { // no code req'd } public static synchronized SingletonObject getSingletonObject() { if (ref == null) // it's ok, we can call this constructor ref = new SingletonObject(); return ref; } private static SingletonObject ref; } Observator Pattern Simply, the Observer pattern allows one object (the observer) to watch another (the subject). The Observer pattern allows the subject and observer to form a publish-subscribe relationship. Through the Observer pattern, observers can register to receive events from the subject. When the subject needs to inform its observers of an event, it simply sends the event to each observer. Schemat wzorca obserwator Observable /Observator pattern import java.util.Observable ; public class Licznik51 extends Observable ... public void impuls () { if (TR == true) { stan ++; if (stan > max) { stan=startValue(); TF = true; setChanged() ; notifyObservers() ; } } Observable /Observator pattern import java.util.Observer ; import java.util.Observable ; public class TestLicznik51 implements Observer ... l51 = new Licznik51() ; l51.addObserver(this) ; ... public void update(Observable subject, Object arg) { message.setText("Got event from " + subject) ; // it is like interrupt routine } Organizacja obsługi zdarzeń W Javie obowiązuje delegacyjny model obsługi zdarzeń, (idiomatyczna implementacja wzorca Obsrwator), zgodnie z którym obiekt-źródło zdarzenia zawiadamia obiektodbiorca (słuchacz) o wystąpieniu zdarzenia. Typowym źródłem zdarzeń są komponenty GUI, które mogą zawiadamiać słuchaczy o działaniach uŜytkownika, który poprzez te komponenty przekazuje dane (polecenia) do programu.