searching for a better design pattern RRS feed

  • Question

  • hi,

    i spend years programming the procedural kind of style but since the projects getting bigger i want to clear things up and get into the design pattern stuff - for a beginner it is rather hard to overview this rich amount of techniques!

    so i have come up to a certain kind of problem which is solved perhaps very suboptimal (due to a lack of time and leaking knowledge) - so i am now very interest how to do this the best practise kind of way...???

    the aim to achieve:
    i want to have a protocol class which is actually only collecting some kind of information. newly added information should be notified/reflect to the ui / caller / connected class - doing like update some control containing this informations.

    for example i built a winform containing a control which is showing the data of the protocol, if i have some kind of helper-class, buisiness-layer aso it would be nice to add entries to the protocol which is the updating the forms ui...

    so here is my very simple kept sketch:

    public partial class Form1 : Form
            public Form1()
                Protocol.Device = lstbxLog; // set the global output device
            private void btnForm1_Click(object sender, EventArgs e)
                Protocol.AddEntry("Eintrag aus Form1", false);
            private void btnHelper1_Click(object sender, EventArgs e)
                new Helper1().DoSomething(); // call Protocol.AddEntry indirectly, in case to show error-msg occured
            private void btnHelper2_Click(object sender, EventArgs e)
                new Helper2().DoSomethingOther(); // call Protocol.AddEntry indirectly, in case to show error-msg occured
            private void btnGetAll_Click(object sender, EventArgs e)
                // the whole protocol could be set to a file or other device...
                StringBuilder str = new StringBuilder();
                foreach(string s in Protocol.Data)

    class Helper1
            public void DoSomething()
                Protocol.AddEntry("Eintrag aus Helper1", false);
    class Helper2
            public void DoSomethingOther()
                Protocol.AddEntry("Eintrag aus Helper2", false);

    class Protocol
            // methods are static in order to use a common data-pool
            private static List<string> _data = new List<string>();
            public static List<string> Data
                get { return _data; }
            private static object _device = null;
            public static object Device
                get { return _device; }
                set { _device = value; }
            public static void AddEntry(string Message, bool IsErrorFormated)
                if (IsErrorFormated)
                    _data.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss} -> #FEHLER# {1}", DateTime.Now, Message));
                    _data.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss} -> {1}", DateTime.Now, Message));
            private static void NotifyLastEntry()
                if (_device == null)
                    throw new Exception("Das Ausgabegerät des Protokolls wurde nicht angegeben!");
                if (_data.Count == 0)
                // notify changes back to the ui control - how to do this more generic (pass a function pointer???)
                System.Windows.Forms.ListBox lstbx = Device as System.Windows.Forms.ListBox;
                if (lstbx != null)
                    lstbx.SelectedIndex = lstbx.Items.Count - 1;
                    lstbx.SelectedIndex = -1;
    any suggestions?
    thank you so far!

    Wednesday, October 26, 2011 12:32 PM

All replies

  • May be observer pattern help you.
    • Proposed as answer by Derek Smyth Thursday, November 3, 2011 2:28 PM
    Wednesday, November 2, 2011 5:32 PM
  • Yes the observer pattern which has a few flavors.

    With Windows forms the Model-View-Controller pattern would be something to look at.

    One way to notify the UI about changes is with events. The protocol class can raises the event and the handler can update a control or whatever.

    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Thursday, November 3, 2011 1:30 PM
  • hello and thank you both!

    yes i was thinking about using some kind of observer pattern as its purpose may also be to notify and delegate - for now when i'm re-thinking i keep the focus on the easy structure - a disign pattern may be good practise but brings much too much overhead for now...


    thank you!!!

    Thursday, November 3, 2011 1:39 PM
  • Please mark as "Answer" if answer helped you. Thx
    Thursday, November 3, 2011 1:56 PM