程序代写 Property of Penn Engineering

Property of Penn Engineering

Observer Pattern

Copyright By PowCoder代写 加微信 powcoder

Property of Penn Engineering

• Creational: how objects are created
• Examples: Singleton, Factory Method

• Structural: how objects are composed
• Example: Bridge

• Behavioral: how objects communicate
• Example: Strategy

Review: Design Patterns

Property of Penn Engineering

• Problem: Object needs to notify a varying set of other objects
that something has occurred

• Concern: We don’t know which other objects to use in
advance, and want to dynamically link the object to its
dependencies

• Solution: Maintain a set of Observers to which the object can
send events, and create a method for sending messages to
each Observer in the set

Design Problem

Property of Penn Engineering

Property of Penn Engineering

public abstract class Subject {

protected Set observers = new HashSet<>();

public boolean attach(Observer o) {
return observers.add(o);

public boolean detach(Observer o) {
return observers.remove(o);

protected void notifyObservers(Event e) {
for (Observer o : observers) {

o.notify(this, e);

Subject Class

Property of Penn Engineering

• A library for developing a graphical user interface has
elements that can be rendered in the visual display

• When a user clicks on a UI element, the application needs to
log the location of the click

• In some cases, a click should also lead to updating the
graphics, and in other cases, some background activities need

Property of Penn Engineering

Property of Penn Engineering

public class UIElement extends Subject {

public void handleClick(int x, int y) {
ClickEvent event = new ClickEvent(x, y);
notifyObservers(event);

Subject and Event Classes

public class ClickEvent extends Event {
protected int x, y;
public ClickEvent(int x, int y) {

this.x = x;
this.y = y;

Property of Penn Engineering

public class ClickLogger extends Observer {
public void notify(Subject s, Event e) {

// write info to log file

public class BackgroundHandler extends Observer

public void notify(Subject s, Event e) {
// perform some tasks in the background

public class GraphicsHandler extends Observer {
public void notify(Subject s, Event e) {

// update the graphical elements

Property of Penn Engineering

UIElement element = new UIElement();
element.attach(new ClickLogger());
Observer o1 = new GraphicsHandler();
element.attach(o1);

element.handleClick(100, 200);
element.detach(o1);
Observer o2 = new BackgroundHandler();
element.attach(o2);

element.handleClick(50, 180);

Using the Observer Pattern

Property of Penn Engineering

• Observer Pattern: a Subject has a set of Observers that can be
notified when an event occurs

• Observers can be added and removed as the program runs

• Typically used in GUI systems, e.g. Android and Java Swing

• Sometimes referred to as Publish-Subscribe

Property of Penn Engineering

• General reusable solutions to commonly occurring problems

• Allow programmers to reuse solutions

• Categories
• Creational: how objects are created (Singleton, Factory Method)
• Structural: how objects are composed (Bridge)
• Behavioral: how objects communicate (Strategy, Observer)

Summary: Design Patterns

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com