Object Oriented Methods

snottybugbearΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

47 εμφανίσεις

16.11.2003 Dr Andy Brooks 1
Object Oriented Methods
Laboratory Exercise 12
(Observer Pattern)
rocket changing state
observers of rocket changing state
Acknowledgements: code examples from M Wood and M Roper.
16.11.2003 Dr Andy Brooks 2
Create a Patterns directory...
• Create a Patterns directory on your networked drive.
• In JCreator, create a new workspace called Observer at
location Patterns (in the Patterns directory)
• In JCreator, create three new empty projects in the
Observer workspace called ObsStart, ObsFinish,
ObsSlider
• Download the Java files for the ObsStart project from the
class website and in JCreator add the files to the project.
Compile and execute the project.
– PDemoSwing.java Person.java
• Understand ObsStart project code
16.11.2003 Dr Andy Brooks 3
Java Swing package
• Builds on the AWT classes
• Platform independent look and feel
– AWT is platform dependent
• Swing classes in a package called Javax
import javax.swing.*;
• Many Swing classes are named simply by
putting a J in front of their AWT equivalent
• java.awt.event enables event handling for
GUI components in the AWT
• javax.swing.event enables event handling
for GUI components in Swing
16.11.2003 Dr Andy Brooks 4
Inheritance hierarchy
The getContentPane() method in the
JFrame class returns the content pane
holding the frame´s components.
Different from the AWT Frame.
16.11.2003 Dr Andy Brooks 5
ObsFinish project
• Download the Java files for the ObsFinish
project from the class website and in
Jcreator, add the files to the project.
Compile and execute the project.
– Driver1.java Observer.java PDemoSwing.java
Person.java Subject.java
• Understand ObsFinish project code
16.11.2003 Dr Andy Brooks 6
Observer pattern
Name
– Observer
Problem
– How can a relationship be defined between a group of
objects so that whenever one object is updated all the others
are notified automatically?
Context
– When changes to one object in a group requires changes to
the others in the group and the number of objects in the
group may vary. When there is a need to reduce coupling
i.e. minimise assumptions about the other objects in the
group.
Forces
– Who triggers the updating? The state altering methods in the
observed object or the Client that calls these methods?
16.11.2003 Dr Andy Brooks 7
Solution
– see diagrams provided
Consequences
– A Subject object has limited knowledge of the Observer objects.
• Only that they conform to the Observer interface
– Update notices are automatically broadcast to all interested
objects regardless of number.
– The cost of updating may be greater than you might expect.
– Update notices may occur too frequently.
• Change management might be necessary.
Code example
– see examples provided.
Documenting…
16.11.2003 Dr Andy Brooks 8
Observer pattern participants
• Subject
– knows observers
– mechanism for attaching and detaching observers
• Observer
– interface for objects that should be notified of changes in subject
• ConcreteSubject
– has state
– notifies observers of changes in state
• ConcreteObserver
– implements Observer interface
– has a reference to ConcreteSubject
16.11.2003 Dr Andy Brooks 9
http://www.tml.hut.fi/~pnr/GoF-models/html/Observer.html
16.11.2003 Dr Andy Brooks 10
http://patterndigest.com/
16.11.2003 Dr Andy Brooks 11
Figure 1: Observer Pattern (UML notation)
www.burn-rubber.demon.co.uk/articles/Obs.doc
16.11.2003 Dr Andy Brooks 12
ObsSlider project
• Download the Java files for the ObsSlider
project from the class website and in
JCreator add the files to the project.
Compile and execute the project.
– AgeSlider.java Driver2.java Observer.java
PDemoSwing.java Person.java Subject.java
• Understand ObsSlider project code.