Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My preference, for this type of situation, is to make a set of interfaces, and work against the interfaces. You can then use Dependency Injection to inject a concrete version of the interface depending on whether you're running in console, gui, or as a service. Something like (this is a very simple version):</p> <pre><code>public interface IAlgorithm { void UpdateProgress(double percent); void Complete(bool success); void Error(string error); // Or (Exception error), if you want to pass an exception } public interface IAlgorithmFactory { IAlgorithm StartAlgorithm(string name); } </code></pre> <p>Then, let's take the console application version. You just need to provide the correct <code>IAlgorithmFactory</code> for a console app:</p> <pre><code>class ConsoleAlgorithm : IAlgorithm { string name; public ConsoleAlgorithm(string algorithmName) { this.name = algorithmName; } public void UpdateProgress(double percent) { if(percent == 0) Console.WriteLine(); else if (percent == 1.0) Console.WriteLine("100% "); else Console.Write("\r{0}%", percent * 100); } public void Complete(bool success) { if (success) Console.WriteLine("{0} completed successfully.", this.name); else Console.WriteLine("{0} failed."); } public void Error(string error) { Console.WriteLine("{0} received error: {1}", this.name, error); } } public class ConsoleAlgorithmFactory: IAlgorithmFactory { public IAlgorithm StartAlgorithm(string name) { return new ConsoleAlgorithm(name); } } </code></pre> <p>When your program runs as a console, you'd provide it a <code>ConsoleAlgorithmFactory</code>, and it would just use it as an <code>IAlgorithmFactory</code>. You can call the methods on it to report progress, and your main program doesn't care how it's run - it just reports as needed.</p> <p>When you run as a GUI, on the other hand, you'd make something like <code>WindowsFormsAlgorithmFactory</code>, pass it in, and now the reporting would be done via forms... Nothing in your program changes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COWhat benefit would this have over using events? In your model if somebody didn't want to listen to certain actions, you would have degenerate methods. You could create an interface for every method, but it seems like this is already built into events.
      singulars
    2. COYou can pass a lot more information in a very rich fashion, more simply than using events. Granted, in this situation (what I posted), events would work as well. The other "problem" with events is that events are subscriber based - so the "forms" need to know about, understand, and have access to the program internals, at least the portions with the events. That can be simple, but can also be (in some cases) difficult and require major api changes. This is more push based - the "logic" in the program just says "I want to do this", and it's up to the presentation to decide whether to handle it.
      singulars
    3. COIt really comes down to whether you want the outside world to need to know about your algorithm (in which events, ie: the observer pattern makes sense), or you want your logic to work, and be able to create "reporting" or "gui" classes without knowing what concrete version is being used, which is what I did (abstract factory pattern). This is one situation where you could use either approach - it just depends on how much information you are passing, how you want to structure your API, and where you want to introduce the abstraction levels.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload