none
GUI and logic design question RRS feed

  • Question

  • Design question:

     

    I am from a Java background and have only recently started working with C# .NET. I am currently building a small html editor using WPF, and have a design question that I was hoping someone could help me out with.

     

    There are 2 main parts to the app, the GUI and the logic. For the logic, I have a main class called Editor, and several classes that extend it. For the purposes of this example, I have a HTMLEditor and a ReviewEditor, that both extend the Editor class.

     

    The GUI itself consists of several TextBox’s. Here is a pic.

    http://www.kommi.com/editor.jpg

    For now lets just focus on the html editor panel and the preview panel.

     

    Since I am trying to follow the MVC pattern, the only link between the GUI and the logic is a reference held by the GUI to the Editor base class. This means that communication is 1 way. The GUI passes data to the Editor, and then queries the editor for updates.

     

    Every time a textbox throws a textChanged event, the data in the TextBox is passed to either the HTMLEditor object or the ReviewEditor object.

     

    Is this the correct way of designing an application? In terms of how the GUI interacts with the logic? I have my reservations, especially considering the situation where the logic does a bunch of operations and has no way of signaling the GUI of a specific change. I guess that is where the Observer pattern comes in. Still, I am wondering how others design their applications.

    Friday, May 25, 2007 6:58 PM

Answers

  • A key design goal is to make the Editor class completely unaware of the GUI but the GUI completely aware of the Editors it hosts.  You'd achieve this by adding events to the Editor class that the GUI can subscribe to.  At the very least, you'll need a Changed event.
    Sunday, May 27, 2007 2:30 PM
    Moderator

All replies

  • A key design goal is to make the Editor class completely unaware of the GUI but the GUI completely aware of the Editors it hosts.  You'd achieve this by adding events to the Editor class that the GUI can subscribe to.  At the very least, you'll need a Changed event.
    Sunday, May 27, 2007 2:30 PM
    Moderator
  • More design questions:

     

    I am building an application that will allow me to create and edit content for a website. The application consists of several main sections. There is a content editor, a preview window, meta editor and keywords editor. There is also a section that will allow me to look up words. And finally I have a tool bar that will allow me to have the standard formatting abilities, such as underline or bold text. Here is a link to what the GUI look like. http://www.kommi.com/editor.jpg Its pretty self explanatory. Yes there are some weird buttons such as randomize, but that’s not important.

     

    I am again trying to figure more about how to properly design using WPF. How should the GUI and the data operations be separated? Currently, my GUI (the .xaml.cs file) handles all of the events and has a reference to the Strategy Pattern that I use for data processing.

     

    But lets say I want to have a RichTextBox, a a few regular TextBox’s to all use the same tool bar. So when I click on a tool bar item, it fires an event, but how does that even know which Control I currently have focus on?

     

    Also, let say that I wish to do a lot of processing of the text in the RichTextBox. The operations I want do not fit into my strategy pattern. So where would I put that logic? IN the xaml.cs file?

     

    I hope some of this is making sense. My problem overall is that I do not know (and cannot find text) about how to properly design an OO application using the WPF model. How exactly is the GUI and the business logic suppose to be designed in relation to each other?

     

    Currently, I have the different Controls all call different event handlers. Those event handlers call the same methods that I have defined. So for example if you want to have bold formatting in the RichTextBox, it will call the RichTextBox_boldText(object sender, RoutedEventArgs args) method, which will then call a boldText() method. The plain TextBox will do the same, calling its corresponding event handler, which then also calls boldText(). Is this correct?

     

    Any and all advice will be greatly appreciated.

     

     

    Wednesday, May 30, 2007 9:30 PM
  • You are talking about WPF, you might get a better answer at the WPF forum.  It has a different object model than Windows Forms.  Answering your specific questions, Form.ActiveControl tells you what control has the focus when you click an ToolStripButton.  A XAML file is very limited in what kind of logic it can implement, there's no IF clause.  It specifies appearance first, some timeline behavior.

    You very much seem to be aware of the Gang-of-Four design patterns.  That's certainly a Good Thing and an essential guide when you design your program.  In real life, things tend to get complicated.  There is no real separation between logic and GUI, the logic better knows what the GUI should know.  And there are probably several things you overlooked, things that coupled the GUI to the logic, and reverse, when it was time to implement your design.  That happens, you can't really avoid it.

    The only way to move ahead is to acknowledge you couldn't foresee everything.  Or that you original design goals might have been a bit ambitious.  You are now at a stage where the Gang-of-Four are running out of answers.  And, classically, the Extreme Programming crowd starts to get noisy.  Now you need get at the stage where the text books run out of answers and you'll actually have to make it work.  It is messy, it is painful to decide what you should give up.  And it will be a lot of fun to see your vision materialize.

    Make sure you've allocated at least as much time to making it work 90% as you did to designing it.  The last 8% takes three times longer.  Good luck!
    Wednesday, May 30, 2007 11:02 PM
    Moderator
  • Haha, awsome awsome answer. Yeah I guess I came full cirlce. Before I would just start coding and then realize I had a bad design later. Now I try to have the academically correct design first, and realize that I did not forsee a lot of real world situations. Ah sigh.... ah well, the more you know...
    Thursday, May 31, 2007 12:31 PM
  • Yes, that was a pretty good post.  I was a bit nervous about ticking you off with it, sure appreciate the response.  Good luck, I'm sure you'll do well.  You have the power of introspection, a rare quality in the software development business.
    Friday, June 1, 2007 9:32 PM
    Moderator