Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE564 Lecture 08

CSE564 Lecture 08

Software Design
Design Patterns: Observer
(202102)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

September 08, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 08: Design Patterns Dr.

    Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs 564 00001000 Homework Read: § Design Patterns: Abstraction and

    Reuse of Object-Oriented Design Gamma, Helm, Johnson, Vlissides § European Conference on Object-Oriented Programming October 1993 § This will become:
  3. jgs 564 00001000 Questions about your Assignment 02

  4. jgs Previously Object-Oriented Design and Programming

  5. jgs 564 00001000 Object Oriented Relationships Association Directed Association Reflexive

    Association Multiplicity Aggregation Composition Generalization Realization
  6. jgs 564 00001000 Not Everything that Can be … Should

    be …
  7. jgs Patterns Software Design

  8. jgs 564 00001000 Definition § Design patterns are solutions to

    software design problems you find again and again in real-world application development. § Patterns are about reusable designs and interactions between objects. § The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns (Gamma, Helm, Johnson, and Vlissides).
  9. jgs 564 00001000 Timeline 1989 Beck OO Thinking Architecture 1993

    Gamma et al. GoF Patterns 1994
  10. jgs 564 00001000 GoF Patterns

  11. jgs 564 00001000 GoF Patterns

  12. jgs Observer Software Design

  13. jgs 564 00001000 Observer

  14. jgs 564 00001000 Observer

  15. jgs 564 00001000 Observer abstract class Observer { public abstract

    void update(); }
  16. jgs 564 00001000 ConcreteObserver class ConcreteObserver extends Observer { private

    string name; private string observerState; private ConcreteSubject subject; public Subject getSubject() { return subject; } public void setSubject(Subject s) { subject = s; } // Constructor public ConcreteObserver(ConcreteSubject s, string n){ this.subject = s; this.name = n; } public override void update() { observerState = subject.subjectState; println("Observer %s's new state is %s", name, observerState); } }
  17. jgs 564 00001000 Subject abstract class Subject { private List<Observer>

    observers = new List<Observer>(); public void attach(Observer observer) { observers.add(observer); } public void detach(Observer observer){ observers.remove(observer); } public void notify(){ foreach (Observer o in observers){ o.update(); } } }
  18. jgs 564 00001000 ConcreteSubject class ConcreteSubject extends Subject { private

    String subjectState; public String getState () { return subjectState; } public void setState (s) { subjectState = s; } }
  19. jgs 564 00001000 Observer Public class Main { public static

    void Main() { // Configure Observer pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(s, "X"); ConcreteObserver eyeTwo = new ConcreteObserver(s, "X"); s.Attach(eyeOne); s.Attach(eyeTwo); // Change subject and notify observers s.subjectState = "ABC"; s.notify(); } }
  20. jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez javiergs@asu.edu Fall

    2020 Disclaimer. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.