Docsity

Design Patterns Observer - Object-Oriented Languages and Systems | ECE 517, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Lecture 23 Object-Oriented Languages and Systems 1
Design Patterns: Observer
[Skrien §8.0] Skrien uses a case study of a
figure-drawing application to introduce
several important design patterns.
Today we will consider two of them,
• Observer, and
• Model/View/Controller
Outline for Lecture 23
I. The figure-drawing
application
II. The Observer pattern
Both of these are used to allow the GUI of an application to
communicate with the “business logic.”
The figure-drawing application
The figure-drawing application was introduced in Section 3.8 of the
text, and we first saw it in Lecture 17.
Here is the window in which the application runs:
Note that there is
a toolbar across
the top with three
tools
The rest of the
window is a
canvas on which
figures can be
drawn.
At any given time,
one of the tools
will be selected.
When the user
clicks on the
canvas, a new
figure of fixed size will appear where the mouse was clicked.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Design Patterns Observer - Object-Oriented Languages and Systems | ECE 517 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Lecture 23 Object-Oriented Languages and Systems 1

Design Patterns: Observer

[Skrien §8.0] Skrien uses a case study of a figure-drawing application to introduce several important design patterns.

Today we will consider two of them,

  • Observer, and
  • Model/View/Controller

Outline for Lecture 23

I. The figure-drawing application II. The Observer pattern

Both of these are used to allow the GUI of an application to communicate with the “business logic.”

The figure-drawing application

The figure-drawing application was introduced in Section 3.8 of the text, and we first saw it in Lecture 17.

Here is the window in which the application runs:

Note that there is a toolbar across the top with three tools

The rest of the window is a canvas on which figures can be drawn.

At any given time, one of the tools will be selected.

When the user clicks on the canvas, a new

figure of fixed size will appear where the mouse was clicked.

CSC/ECE 517 Lecture Notes © 2007, Edward F. Gehringer 2

Let’s take (just) a quick look at the code. We will modify it as we go along.

package drawer0;

import javax.swing.; import java.awt.;**

public class DrawingFrame extends JFrame { public DrawingFrame() { super("Drawing Application"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = getContentPane();

JComponent drawingCanvas = createDrawingCanvas(); contentPane.add(drawingCanvas, BorderLayout.CENTER);

JToolBar toolbar = createToolbar(); contentPane.add(toolbar, BorderLayout.NORTH); }

private JComponent createDrawingCanvas() { JComponent drawingCanvas = new JPanel(); drawingCanvas.setPreferredSize(new Dimension(400, 300)); drawingCanvas.setBackground(Color.white); drawingCanvas.setBorder (BorderFactory.createEtchedBorder()); return drawingCanvas; }

private JToolBar createToolbar() { JToolBar toolbar = new JToolBar(); JButton ellipseButton = new JButton("Ellipse"); toolbar.add(ellipseButton); JButton squareButton = new JButton("Square"); toolbar.add(squareButton); JButton RectButton = new JButton("Rect");

CSC/ECE 517 Lecture Notes © 2007, Edward F. Gehringer 4

Now, when it’s time to draw a figure, the CanvasEditor just needs to poll the buttons to determine which was clicked most recently.

Here’s a UML collaboration diagram that shows how this is done. Names that are underlined in the diagram refer to objects, not classes.

This approach is rather awkward. Why?

Instead of the CanvasEditor keeping track of the ToolButton s, perhaps the ToolButton s could keep track of the CanvasEditor.

How would this help?

Here’s a class diagram for this approach:

Lecture 23 Object-Oriented Languages and Systems 5

However, the classes ToolButton and CanvasEditor classes are still tightly coupled together.

Observer

We can reduce the coupling by using the Observer pattern, which will allow ToolButton s and CanvasEditor s to be unaware of what type of objects they are communicating with.

The easiest way of understanding the Observer pattern is to consider its alternate name, Publish-Subscribe.

  • Publishers produce information that is of interest to other objects in the system.
  • An object that is interested in a piece of this information subscribes to this publisher.

Then when an event of interest happens in the system, a publisher sends it to its subscribers.

Subscribers can also be thought of as “observers,” and hence the name of the pattern.

In our example, who’s the publisher and who’s the subscriber?

In Java, many observer classes are called listeners.

Since our ToolButton no longer needs to maintain the time at which it was last clicked, it can be a JButton instead. Event handling for a JButton is performed by an ActionListener.

Here’s the class diagram for that:

Lecture 23 Object-Oriented Languages and Systems 7

CanvasEditor(ellipseButton); ellipseButton.addActionListener(CanvasEditor); squareButton.addActionListener(CanvasEditor); rectButton.addActionListener(CanvasEditor);

Which class is it in?

Which method is it in?

How does this approach reduce coupling between the buttons and the CanvasEditor?

Here’s a synopsis of the Observer pattern.

Observer

Intent: Define a one-to-many dependence between objects so that when one object changes state, all its dependents are notified automatically.

Problem: A varying list of objects needs to bet notified that an event has occurred.

Solution: Observers delegate the responsibility for monitoring for an event to a central object, the Subject.

Implementation: • Have objects (Observers) that want to know when an event happens attach themselves to another object (Subject) that is watching for the event, or that triggers the event itself.

  • When the event occurs, the Subject tells the list of Observers that it has occurred.

What other kind of user input—besides clicking on drawing tools-- does our application need to handle?

CSC/ECE 517 Lecture Notes © 2007, Edward F. Gehringer 8

These events can be handled similarly, but by a different kind of listener. Which?

Here’s our first attempt at the code:

public void mouseClicked(MouseEvent e) { //handle clicks in the canvas int x = e.getX(); int y = e.getY(); JPanel canvas = (JPanel) e.getSource(); if (currentButton.getText().equals ("Ellipse")) canvas.getGraphics().drawOval (x - 30, y - 20, 60, 40); else if (currentButton.getText().equals ("Rect")) canvas.getGraphics().drawRect (x - 30, y - 20, 60, 40); else //if( currentButton.getText().equals //("Square") ) canvas.getGraphics().drawRect (x - 25, y - 25, 50, 50); }

What’s not nice about this code?

A good way to get around this problem is to have an abstract class Figure with subclasses for the various kinds of figures.

The code, except for accessor methods, is shown below.

package drawer2.figure; import java.awt.;*

Figure

Rect

Square

Ellipse

CSC/ECE 517 Lecture Notes © 2007, Edward F. Gehringer 10

public void draw(Graphics g) { int width = getWidth(); int height = getHeight(); g.drawRect(getCenterX() - width/2, getCenterY() - height/2, width, height); } }

public class Square extends Rect { public Square(int centerX, int centerY, int w) { super(centerX, centerY, w, w); } }

Unfortunately, this just moves the conditional from the mouseClicked method to the actionPerformed method:

public void actionPerformed(ActionEvent e) { JButton currentButton = (JButton) e.getSource(); if (currentButton.getText().equals ("Ellipse")) currentFigure = new Ellipse(0, 0, 60, 40); else if (currentButton.getText().equals ("Rect")) currentFigure = new Rect(0, 0, 60, 40); else //if( currentButton.getText().equals // ("Square") ) currentFigure = new Square(0, 0, 50); }

This problem can be solved by giving each of the buttons their own ActionListener (instead of making the CanvasEditor the ActionListener for all buttons):

Lecture 23 Object-Oriented Languages and Systems 11

ellipseButton.addActionListener(canvasEditor);

⇒

ellipseButton.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { canvasEditor.setCurrentFigure (new Ellipse(0, 0, 60, 40)); } });