1 / 67

CS 415 N-Tier Application Development

National University of Computer and Emerging Sciences. Lecture # 3 Design Patterns (Observer,Factory,Singleton). CS 415 N-Tier Application Development. By Umair Ashraf June 22 ,2013. Contents. Observer Pattern Factory Pattern Abstract Factory Pattern Singleton Pattern

Download Presentation

CS 415 N-Tier Application Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. National University of Computer and Emerging Sciences Lecture # 3 Design Patterns (Observer,Factory,Singleton) CS 415 N-Tier Application Development By Umair Ashraf June 22 ,2013

  2. Contents • Observer Pattern • Factory Pattern • Abstract Factory Pattern • Singleton Pattern • Practical Demonstration • Assignment 0

  3. The observer pattern Keeping your Objects in the know

  4. First attempt…

  5. What’s wrong…

  6. A day in the life of Observer Pattern

  7. The power of loose coupling • The only thing the subject knows about an observer is that it implements a certain interface • We can add new observers at any time • We never need to modify the subject to add new type of observers • We can reuse subjects or observers independently of each other • Changes to either the subject or an observer will not affect the other

  8. public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); } public interface Observer { public void update(float temp, float humidity, float pressure); } public interface DisplayElement { public void display(); }

  9. public class WeatherDataimplements Subject { private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList(); } public void registerObserver(Observer o) { observers.add(o); } public void removeObserver(Observer o) { inti = observers.indexOf(o); if (i >= 0) { observers.remove(i); } }

  10. public void notifyObservers() { for (inti = 0; i < observers.size(); i++) { Observer observer = (Observer)observers.get(i); observer.update(temperature, humidity, pressure); } } public void measurementsChanged() { notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } // other WeatherData methods here

  11. public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display(); } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } }

  12. public class WeatherStation{ public static void main(String[] args) { WeatherDataweatherData = new WeatherData(); CurrentConditionsDisplaycurrentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplaystatisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplayforecastDisplay = new ForecastDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); } }

  13. Java’s built in Observer Pattern

  14. Factory pattern All About Creating Objects

  15. The Simple Factory

  16. Abstract Factory pattern All About Creating Objects

More Related