aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Pattern for converting console program to GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Pattern for converting console program to GUI" Watch "Pattern for converting console program to GUI" New topic
Author

Pattern for converting console program to GUI

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi All,
I have a tool that is written in Java that uses the console UI. It has been in use for some time, and now I have built a GUI for it. I was wondering if there is some design pattern approach for plugging this interface into the already existing program. I want the console interface to still exist, basically switching between the 2 interfaces with a command line parameter. I don't want to go through the entire program and say:

because first of all it is ugly, and secondly it would be hard to maintain. I have only one constructor, which just calls a few methods from the same class. These methods then just make a bunch of api calls. I was thinking of just creating another constructor which would be for the GUI control, (Not the GUI itself. The GUI is a separate object in another class) and then overload the methods in this same class to make calls to the GUI. I hope I am making some sense here. Would this be a good approach, or is there a more elegate way to do this task.

many thanks,
Barry
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
what about creating a common interface to the GUI and the console and using an Abstract Factory to get the appropriate object at run time ?
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

I think this is a great idea! You mentioned interface, but should it be an interface or abstract class? I believe it should be abstract class, then the console and GUI objects would extend this, right?

thanks,
Barry
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
Whether you use an interface or an abstract class largely depends on whether your console & GUI classes share any functionality. Implementing an interface is more flexible if in doubt.
code would be something like

you could then implement an Abstract Factory according to GOF book and do something like

I assume that you are just writing out to the console rather than reading data in. If not then going to be very difficult to construct a program that is event driven for the gui but logic driven for console.
Hope this helps.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The main thing you need to do for this sort of project is to comb out the "model" (business logic) code from "UI" code.
I'd start by refactoring the text/console version to make the text UI as simple and "thin" as possible, and as separate from the "model" as possible; then make another very thin graphical UI which uses the same model code.
Can you tell us a bit more about your application and the kinds of user interactions you are dealing with?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Ok, I think I see how this is to be done. Lucklily it's not a large program, so it won't be too difficult to refactor ( I think). Frank, the console program starts out by asking 5 questions that are the basis for how the program runs. So yes, there is user input. Then, while the program is running there may be problems along the way, where I need to get more user input, like "There is some problem. What do you want me to do? [T]his or T[h]at?" So for the GUI side, this would be a message box. Hey, I believe I can handle this! I appreciate you guys helping me. Do you have any more suggestions/thoughts?

thanks,
Barry
 
Don't get me started about those stupid light bulbs.
 
subject: Pattern for converting console program to GUI