Archived

This topic is now archived and is closed to further replies.

sprinter_trueno

patterns and other problems

Recommended Posts

Since Magmai Kai Holmlor said it''s ok to post beginner-like stuff (concernig design though) here I''ll do so without any shame Lets get straight to the point. Imagine the following: I have a class CTimerApp which instantiated provides a GUI with a display and some buttons. The class keeps a reference to the GUI. Second the class keeps an object to a CTimer class which provides the timer logic. Lets say: CTimer : public CThread { public: void Start(); void Stop(); private: void SecondPassed(); void ThreadFunction(); DWORD dwMilliSeconds; } Start lets the timer increment the variable in the ThreadFunction function until Stop is called. Everytime a second passed the timer makes a call to it''s SecondPassed function and thus informs the application, which would update the GUI. This is the general idea which might be bull but the following thoughts made me think like that: 1. The timer should not know about the application. 2. The timer should not know about the GUI. 3. Polling the timer would be inefficient, thus information flow has to be event triggered. Now to reassure Magmar Kai Holmlor that I did my homework, if I wanna use a pattern, I''d go for either the Observer, Visitor, or Mediator pattern. All of them would work but: 1. For the observer pattern I''d expect more than one observer. 2. For the visitor I''d expect more than one object to visit. 3. For the Mediator I''d like to have more than one object in my group of triggering events. But isn''t there a better way ? Even if I wanted to make the application known to the Timer class it wouldn''t work since the implementation of the timer is already included in the app class. I mean the only thig that came up to my mind is to make my app class a subclass of another (BaseAppTimer for example) and make that one known to the timer. In that I could force that subclass to implement an timing update function and make the timer call it through the interface of the base class. Looks weird though and I cannot find a good reason why I''d make my application a subclass of one just to update. Now this really looks like homework but it isn''t. I just try to understand the communicaton between objects and the way to establish that kind of communication. Another thing: I mentioned the GUI. Since I wanna platform independent, I''d like to use the GUI framework of a platform to visualize my application but adapt it to my own GUI interface. Thus if I change the platform I just recreate the GUI around the interface of my application. I''m using MFC right now and thought about using ''Adapter'' or ''Bridge'' but since I am already stuffed with getting myself into alot of other problems I skipped the two and thought about implementing the ''Command'' pattern. Now what I don''t get is the following: The command objects need to know about the application they should run their logic on, thus when you create them or whenever, you pass them a reference to the app. In my example, I''d have one button to start the timer, thus I''ll have one Command object CStartTimerCmd derived from a base class CCommand which forces the subclasses to implement a ToDo function like ''Execute''. Cool. Everything fine. The CStartTimerCmd implements the Execute and implements the logic but of course needs to know about the CTimerApp to use it''s functionality. If I understood right then the deal with the Command pattern is that you have command objects at hand that you know what they do but do not know how thus things are decoupled by calling execute through the interface CCommand. My problem is the initialization. I don''t know when to instantiate the command object. The MFC GUI should merely know about the command interface and the object addresses, nothing more. But if I don''t create the object there then where ? and when ? Maybe I''m missing something terribly obvious but so far couldn''t make it out. One last question: Lets say I don''t instantiate my TimerApp class but still I have an include of the header file in the header of the MFC diag class although I use it completely decoupled, is this violating OO ? A bunch of questions and most probably a lot of beginner looking ones. If anybody needs a headache, you''re all welcome I have tons of questions like that Thanks in advance for reading al of this and even more for any comment ( Advice to stop programming is also ok ) Lorenz

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Have you looked into the Model-View-Control design pattern? Maybe this is what you are looking for.

Share this post


Link to post
Share on other sites