An input stream reads bytes from a file. This is an example of which pattern?
You attach action listeners to a JButton
. In the terminology of the OBSERVER pattern, the JButton
is a(n)
Consider a program that contains two frames, one with a column of text fields containing numbers, and another that draws a bar graph showing the values of the numbers. When the user edits one of the numbers, the graph should be redrawn.
Which of the following statements are true? Select all that apply.
java.util.Scanner
an instance of the ITERATOR pattern?lab9
directory and make a project in that directory. Run the programChangeListener
interface type. Read the API documentation for
JSlider
and ChangeListener
. Make a table of pattern names and actual names that shows how this is a manifestation of the observer pattern.Iterator
is actually an observer that notifies you whenever the next value is available. Is it? Why or why not?ChangeListener
is actually an iterator through a sequence of JSlider
values. Show why the Iterator
pattern doesn't apply.SliderIterator
that actually iterates over the slider values. Make it so that when the slider value changes, the slider value is inserted to a queue. The next
method takes the next value (blocking if the queue is empty).
public class SliderIterator implements Iterator<Integer> { public SliderIterator(JSlider slider) { slider.addChangeListener(event -> { JSlider source = (JSlider) event.getSource(); int value = source.getValue(); ... }); } public boolean hasNext() { return true; } public Integer next() { try { return ... } catch (InterruptedException ex) { return null; } } public void remove() { throw new UnsupportedOperationException(); } private BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); }
main
:
Iterator<Integer> iter = new SliderIterator(frame.getSlider(0)); while (iter.hasNext()) { System.out.println(iter.next()); }Run the program. What happens when you adjust the first slider?