Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

Separating "Play, Pause, Stop" from "Load File & Save File" (C# WPF)

I'm designing an application which has a Model. The Model has Play(), Pause(), and Stop() methods, as well as LoadFIle(filename) and SaveFile(filename).

I'm feeling that perhaps one class should not be doing all of this, as it's become a large "God" class.

"Play, Pause, Stop" sound like they go together as "control", whereas "Load & Save" sounds like something else, which involves reading a file and setting state.


Let's say the Model class has an ElapsedTime property, and a StopTime property.

SaveFile() saves the StopTime value to a file. LoadFile() loads the StopTIme value from the file.

(There's probably a lot more being saved and restored too.)

There's a MasterLoop() method which is an endless loop that makes the program "go".

Play() calls Task.Run to launch this MasterLoop() on a thread.

Stop() terminates this MasterLoop() and resets ElapsedTime.

Pause() terminates this MasterLoop() but does not reset ElapsedTime.

The MasterLoop() may exit on its own if ElapsedTime reaches StopTime.

I actually have the whole demo thing working fine (there's a CancellationToken involved too). (And there's a View and VIewModel, so we can see what's going on; some buttons for Play, Pause, Stop; and some menu items for Save File and Load File.)

My concern is it's all in one big Model class; and I'm suspecting that LoadFile and SaveFile maybe don't belong in the Model. But it doesn't belong in the ViewModel either, as the ViewModel shouldn't be reading a file, and it shouldn't be reaching into the Model and manipulating variables.

The ViewModel does prompt for a file name; but once it gets the file name, it should hand that off to... what? The Model?

The Model also has Initialization code in it, which does a bunch of setup, making my Model class even larger.

How can I reorganize this?
  • State Variables (Elapsed Time, Stop Time).
  • MainLoop()
  • Play, Pause, Stop
  • Load FIle, Save File
Load File, Save File, and MainLoop() need to access StopTIme.
Play, Pause, Stop do not need to access StopTime.
Stop needs to access ElapsedTime to reset it.
MainLoop needs to access ElapsedTime.

It's so easy when it's all together in one large class. I'm really trying to learn something about how to handle a much larger project. This is just an imaginary application demonstrating the larger problem.
ASKER CERTIFIED SOLUTION
Avatar of cyimxtck
cyimxtck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial