jgs 460 00001100 Patterns are these Blocks § 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).
jgs 460 00001100 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(); } } }
jgs 460 00001100 Main 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(); } }
jgs 460 00001100 Student import java.util.Observable; import java.util.Observer; public class Student implements Observer { public String answerQuestion (String question) { return "I am thinking about \'" + question +"\'"; } @Override public void update(Observable o, Object arg) { String x = ((Tutor)o).getQuestion(); String y = this.answerQuestion(x); System.out.println(y); } }
jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez [email protected] Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.