Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes refactoring question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "refactoring question" Watch "refactoring question" New topic
Author

refactoring question

chihwah li
Greenhorn

Joined: Apr 23, 2010
Posts: 25

I have the following situation clock that has a analog and digital view in the same JFrame:

Classes:
- ClockFrame extends JFrame
- ClockPanel extends JPanel implements Observer,
- TimeGenerator class extends Observable implements Runnable

What happends:
- I run timeGenrator's main, creates TimeGenerator and ClockPanel.
- timeGenerator calls method addObserver(ClockPanel)
- The ClockPanel creates ClockPanel, then calls method ClockFrame.addPanel(this). To add this JPanel to the JFrame.

I must follow the situation above because:
- TimeGenerator can only add an instance if it created it. I don't know how to call method addObserver() if it did not create that ClockPanel instance.
- Because the panel is created, I let ClockPanel create an instance of ClockFrame and call method ClockPanel.addPanel(this).

Problem:
- I am trying to create 1 class for each clock view: Clockanalog and a class ClockDigital. but How can I do that? The problem is that only one ClockPanel (analog or digital) can create the ClockFrame.
Without creating the instance in a specific ClockPanel, I don't know how to add it to the ClockPanel.

Hope it's clear... I can provide my code if needed. What Java principle do I lack to successfully refactor this code? If you have suggestions, please do tell. =)
 
jQuery in Action, 2nd edition
 
subject: refactoring question