dimanche 29 juillet 2018

What is appropriate design pattern

I have some problem on deciding wich design pattern solve my situation. My problem is as fallows: i have to build testing platform to perfome some tests on physicall devices the company i work for produces. So i have: 1) ITest interface, which is parent for all interfaces:

interface ITest
{
   string Description {get; set;}
   void Run();
}

So particular test should inherite from this interfase and implement Description property and Run method. 2) TestManager class, which holds the collection of tests to run. It also may perfome some actions on tests, say for example it has RunAll method which runs all tests it holds.

class TestManager
{
   public List<ITest> Tests {get; run;}
   void RunAll()
   {
       foreach (var t in Tests)
       {
           t.Run();  
       }
   }
}

Now suppose that some tests need some preparations before running. So i have subtype of ITest which have a preparation text for test:

interface IPreparableTest : ITest
{
   string PreparationMessage {get; set; }
}

This PreparationMessage is gonna be prompted to user in some way so that the user can perfome some actions on device we wanna test. But it is the TestManager's responsability to decide what tests need to be prepared and in what way the PreparationMessage is gonna be shown to user. The problem is that it holds the collection of ITest and therefore it have to distinguish between preparable test and ordinary test. Of course i could've written something like this:

RunAll()
{
   foreach (var test in tests)
   {
       if (test is IPreparableTest)
       {
          MessageBox.Show(((IPreparableTest)test).PreparationMessage);
       }
       test.Run();
   }
}

But i don't wanna write like this because it is not OOP style. I wanna use some pattern here, but i don't have any idea which one i should use. Also i don't wanna put the preparatation logic on test, because it's not the responsability of test, test should have only the information on how to prepare the device, but how this message is gonna be shown to user is up to TestManager. So basically i wanna learn the way to avoid the switch on type of object. I aprreciate if someone can help me on this issue! Thank you!

Aucun commentaire:

Enregistrer un commentaire