Implementing Backward Classes

I have a set of classes, each of which at some point must decide which of the two or three approaches they should use internally in order to implement the same functionality from the outside. Ideally, this should include fallback functions, where if ApproachA fails, it tries to try ApproachB (and possibly goes with C, D, etc.). So far, I just used an encoding like if (!success) { ApproachB code }. The problem is that a few later methods also need to be aware of which approach has been chosen, and they all develop their own instructions if (MethodChosen) { } else { }. I really want to solve this problem with something less cumbersome ... except that none of the other options that I have considered seem so "stubborn." Here are three approaches I was thinking about:

  • Implement a static .Create method that decides which of the two derived classes to create, where both classes have an interface that supports them. The disadvantage of this is that you write a lot of the same code twice, and in fact it does not create a “back-up” code, since it forces all decisions to make decisions in the .Create method. This should work 9/10 times, but there will be another 1/10 time when I want the backup to hit only when the primary tried and failed.
  • Same as above, but with a base or abstract class, either as the base class for both, or with the base as the base class to return. This has the same drawback, but at least the code is little or not repeated.
  • Introduce a normally constructed abstract class with child classes that can be changed at runtime: i.e.

    public void SomeMethodOrConstructor()
    {
        if (someConditions)
            MyChild = ChildClassA;
        else
            MyChild = ChildClassB;
    }
    
    
    public void Execute()
    {
        MyChild.Execute();
    }
    

The problem with option 3 is the transfer of data between them when necessary. Since some of these methods model external objects, this will be quite common. Nested classes share data with parent class? Or should I pass it all on with every call?

Anything else I should consider?


: . , , . , , , , . , , , , .

, !

:

  • Handler, Wikipedia, public abstract Handler GetHandler() , Load, Save ..
  • handler ( , ... ). , .
  • / Chain of Responsibility ( , ), FirstHandler.GetHandler(this) , , .
  • Handler.MethodName().
+5
3

.

- , . , , , . .

, .

+5

I think you need to Task/ Task<T>especially ContinueWith(Action<Task>)and 4 properties: IsCanceled, IsCompleted, IsFaultedand Result.

0
source

All Articles