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

CSE360 Lecture 17

CSE360 Lecture 17

Introduction to Software Engineering
Software Design Patterns
(201806)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE360
    Introduction to Software Engineering
    Lecture 17:
    Software Design Patterns: Observer
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu
    Office Hours: By appointment

    View full-size slide

  2. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 2
    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 of objects.
    • The 23 Gang of Four (GoF) patterns are generally
    considered the foundation for all other patterns
    (Gamma, Helm, Johnson, and Vlissides).

    View full-size slide

  3. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 3
    GoF Patterns
    Creational Patterns
    • Abstract Factory
    • Builder
    • Factory Method
    • Prototype
    • Singleton
    Structural Patterns
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Façade
    • Flyweight
    • Proxy
    Behavioral Patterns
    • Chain of Responsibility
    • Command
    • Interpreter
    • Iterator
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Template Method
    • Visitor

    View full-size slide

  4. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 4
    Observer

    View full-size slide

  5. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 5
    Observer
    Public class MainApp{
    public static void Main() {
    // Configure Observer pattern
    ConcreteSubject s = new ConcreteSubject();
    s.Attach(new ConcreteObserver(s, "X"));
    s.Attach(new ConcreteObserver(s, "Y"));
    s.Attach(new ConcreteObserver(s, "Z"));
    // Change subject and notify observers
    s.SubjectState = "ABC";
    s.Notify();
    }
    }

    View full-size slide

  6. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 6
    Subject
    abstract class Subject {
    private List _observers=new List();
    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();
    }
    }
    }

    View full-size slide

  7. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 7
    ConcreteSubject
    class ConcreteSubject extends Subject {
    private String _subjectState;
    public String getState {
    return _subjectState;
    }
    public void setState (value) {
    _subjectState = value;
    }
    }

    View full-size slide

  8. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 8
    Observer
    abstract class Observer {
    public abstract void update();
    }

    View full-size slide

  9. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 9
    ConcreteObserver
    class ConcreteObserver extends Observer {
    private string _name;
    private string _observerState;
    private ConcreteSubject _subject;
    // 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);
    }
    public Subject getSubject() { return _subject; }
    public void setSubject(Subject value) { _subject = value; }
    }

    View full-size slide

  10. Java Implementation
    java.util.*

    View full-size slide

  11. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 11
    Java Subject (Observable)
    import java.util.Observable;
    public class ObservableDemo extends Observable {
    private String weather;
    public ObservableDemo(String weather) {
    this.weather = weather;
    }
    public String getWeather() {
    return weather;
    }
    public void setWeather(String weather) {
    this.weather = weather; setChanged();
    notifyObservers();
    }
    }

    View full-size slide

  12. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 12
    Java Observer (Observer)
    import java.util.Observable;
    import java.util.Observer;
    public class ObserverExample implements Observer {
    private ObservableDemo weatherUpdate ;
    @Override
    public void update(Observable observable, Object arg) {
    weatherUpdate = (ObservableDemo) observable;
    System.out.println("Weather Report Live. Its "+weatherUpdate.getWeather());
    }
    public static void main(String[] args) {
    ObservableDemo observable = new ObservableDemo(null);
    ObserverExample observer = new ObserverExample();
    observable.addObserver(observer);
    observable.setWeather("Bright and sunny... ");
    observable.setWeather("Raining Heavily!.. ");
    }
    }

    View full-size slide

  13. CSE360 – Introduction to Software Engineering
    Javier G onzalez-Sanchez
    javiergs@ asu.edu
    Sum m er 2017
    Disclaim er. These slides can only be used as study m aterial for the class C SE360 at ASU. They cannot be distributed or used for another purpose.

    View full-size slide