Can I name different classes (with different methods and types) that perform similar functions?

I have many methods (in this case, from web services, but maybe this does not affect?) On the call. They are already fixed versions of the release and will not be changed , it is up to me to adapt to them. I already have proxies in my project, and in fact I already name them, and the project is fine.

This main class method receives some input parameters (transaction type and XML string containing transaction data). Based on TransactionType, I know which class and method I should name. I also need to provide him with the type variable that he expects, already built from the provided XML. This is how it happens today (I don’t have the code here, so I apologize for any syntax errors), approximately:

public class MyClass ()
{
  public void MyMethod( string TransactionType, string XML )
  {
    switch( TransactionType ) {
       case "1":
         type1VO type1Object = ( new Deserializer<Type1>() ).XML2Object( XML );
         ws = new WSProxy1();
         string response = ws.Method1( type1VO );
         //
         // lots of other lines of code that use type1VO, type1Object, the response, etc.
         //
         break;
       case "2":
         type2VO type2Object = ( new Deserializer<Type2>() ).XML2Object( XML );
         ws = new WSProxy2();
         string response = ws.Method2( type2VO );
         //
         // same structure here, but handling types specific for "case 2"
         //
         break;
    }
    ...
  }
}

. , 15 , , . ( , ), , . , : , , .

, , "". , - , , . , , , "" , .

# .NET v2.0, , , . . , , .

+3
3

.

, . , , . , , .

:

public interface IExecuteStrategy
{
    string TransactionType {get;}
    void Execute( string xmlData );
}

public class WsProxy1Adapter : IExecuteStrategy
{
    public string TransactionType
    {
        get { return "1"; }
    }

    public void Execute(string xmlData)
    {
        Type1 type1Object = ( new Deserializer<Type1>() ).XML2Object( XML );
        var ws = new WSProxy1();
        string response = ws.Method1( type1Object );
        //
        // lots of other lines of code that use type1VO, type1Object, the response, etc.
        //
    }
}

public class WsProxy2Adapter : IExecuteStrategy
{
    public string TransactionType
    {
        get { return "2"; }
    }

    public void Execute(string xmlData)
    {
        Type2 type2Object = ( new Deserializer<Type2>() ).XML2Object( XML );
        var ws = new WSProxy2();
        string response = ws.Method1( type2Object );
        //
        // lots of other lines of code that use type1VO, type1Object, the response, etc.
        //
    }
}

public class MyClass
{
    private static Dictionary<string, IExecuteStrategy> _transactionHandlers;

    static MyClass()
    {
        _transactionHandlers = new Dictionary<string,IExecuteStrategy>();

        IExecuteStrategy obj = new WsProxy1Adapter();
        _transactionHandlers.Add(obj.TransactionType, obj);

        obj = new WsProxy2Adapter();
        _transactionHandlers.Add(obj.TransactionType, obj);
    }


    public void MyMethod( string TransactionType, string XML )
    {
        _transactionHandlers[TransactionType].Execute( XML );
    }
}
+1

, , . , . , :

abstract class Base<T, U>
{
  private U _obj;
  public Base(string xml)
  {
    _obj = (new Deserializer<T>()).XML2Object(xml);
  }

  public abstract void process();
  protected abstract String getResponse();
}

factory, . - :

public void MyMethod(string transactionType, string xml)
{
    Base.getByTransactionType(transactionType).process();
}
+1

.

0

Source: https://habr.com/ru/post/1790010/


All Articles