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

CSE564 Lecture 09

CSE564 Lecture 09

Software Design
Design Patterns: Observer
(202102)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 09, 2020
Tweet

More Decks by Javier Gonzalez

Other Decks in Programming

Transcript

  1. jgs CSE 564 Software Design Lecture 09: 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 Previously Object-Oriented Design and Programming

  4. jgs 564 00001000 Design Principles There are five key design

    principles to consider in Object-Oriented: § Single Responsibility Principle (SRP) § Open-Closed Principle (OCP) § Liskov Substitution Principle (LSP) § Interface Segregation Principle (ISP) § Dependency Inversion Principle (DIP)
  5. jgs 564 00001000 DIP Example What about ISP?

  6. jgs Patterns Software Design

  7. jgs 564 00001000 Not Everything that Can be … Should

    be …
  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

  16. jgs 564 00001000 Subject import java.util.LinkedList; public abstract class Subject

    { private LinkedList<Observer> observers = new LinkedList<Observer>(); public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void notifying() { for (Observer ob : observers) { ob.update(this); } } }
  17. jgs 564 00001000 Observer public abstract class Observer { public

    abstract void update(Subject s); }
  18. jgs 564 00001000 ConcreteSubject class ConcreteSubject extends Subject { private

    String data; public String getData() { return data; } public void setData(String s) { data = s; notifying(); } }
  19. jgs 564 00001000 ConcreteObserver class ConcreteObserver extends Observer { @Override

    public void update(Subject s) { String info = ((ConcreteSubject)s).getData(); System.out.println("Subject is doing this: "+info); } }
  20. jgs 564 00001000 Main public class Main { public static

    void main(String[]a) { // Configure pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(); ConcreteObserver eyeTwo = new ConcreteObserver(); s.addObserver(eyeOne); s.addObserver(eyeTwo); // Change subject s.setData("ABC"); s.setData("XYZ"); } }
  21. jgs 564 00001000 Main public class Main { public static

    void main(String[]a) { // Configure pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(); ConcreteObserver eyeTwo = new ConcreteObserver(); s.addObserver(eyeOne); s.addObserver(eyeTwo); // Change subject s.setData("ABC"); s.setData("XYZ"); } }
  22. jgs 564 00001000 Observer

  23. jgs 564 00001000 Questions about Observer

  24. jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. javiergs@asu.edu Fall

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