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

CSE564 Lecture 09

CSE564 Lecture 09

Software Design
Design Patterns: Observer
(202102)

Javier Gonzalez-Sanchez

September 09, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    Javier Gonzalez-Sanchez [email protected] 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 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)
  4. 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).
  5. 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); } } }
  6. jgs 564 00001000 ConcreteSubject class ConcreteSubject extends Subject { private

    String data; public String getData() { return data; } public void setData(String s) { data = s; notifying(); } }
  7. 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); } }
  8. 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"); } }
  9. 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"); } }
  10. jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. [email protected] 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.