Strategy Template Right?

Hope you can help me with my problem:

I have a class making soap calls. But if changing the definition of soap, I will need to write a new class or inherit it, etc. So I came to the decision to write something like this:

switch(version)
{
  case "1.0":
     saopV1.getData()
  case "2.0":
     soapV2.getData()
}

Well, very bad code, I know. Then I read about the Strategy template, and I thought, wow, that I need to get rid of this bad thing with switching:

abstract SoapVersion
{
    public SoapVersion GetSoapVersion(string version)
    {
         //Damn switch-case thing
         //with return new SoapV1() and return new SoapV2()
    }
    public string[] virtual getData()
    {
          //Basic Implementation
    }
}

class SoapV1:SoapVersion
{
       public override string[] getData()
       {
           //Detail Implementation
       }
}

class SoapV2:SoapVersion
{//the same like soapv1}

But I cannot avoid using "ifs" or switching cases in my code. Is this possible using OO methods?

Edit: The GetSoapVersion function must be static.

+3
source share
7 answers

. - , v1 v2, ( ) . factory (factory factory) .

factory static. , : , GetData, () , .

public abstract class SoapProcessor
{

    protected SoapProcessor() { /* protected constructor since public is of no use */  }

    public static SoapProcessor Create( SoapVersion version )
    {
          switch( version )
          {
               case SoapVersion.Version1 : return new SoapV1Processor();
               case SoapVersion.Version2 : return new SoapV2Processor();
               default: throw new NOtSupportedException();
          }
    }


    public string[] GetData()
    {
         return GetDataCore();
    }

    protected abstract GetDataCore();
 }

}

+4

, . ( ) .

+1

if/case, : ​​ (, ), , - if/case. , factory . , Strategy , , , .

+1

switch if.
.
(.. SoapV1, SoapV2 ..) .
, . ( ). . ( : . )

public abstract class SoapHandler
{

    protected abstract string[] getData();
 }

public class SoapHandlerV1 extends SoapHandler
{

    public string[] getData(){
        //V1 implementation
    }

}
public class SoapHandlerV2 extends SoapHandler
{

    public string[] getData(){
        //V2 implementation
    }

}


public class SoapProcessor{

    public SoapHandler soapHandler;

    public setSoapHandler(SoapHandler h)
    {
                soapHandler = h;
    }

    public String[] getData(){
        //delegate to specific version
        soapHandler->getData();
    }
}


//in your code
SoapProcessor soap = new SoapProcessor();
soap.setSoapHandler(new SoapHandlerV1());
String[] soapData = soap.getData();//Will get the appropriate version
//use soap data
//do stuff

GoF , , ,

+1

version , ( ..).

, - / .

0

,

Dictionary<string, SoapVersion> Registry : "1.0" and new SoapV1() "2.0" and new SoapV2(),

. .

0

, .

, .

public interface IService
{
    string[] GetData();
}

-

IService srvice = ServiceFactory.GetProxy();
string[] value = service.GetData();

, - .

- ServiceFactory. , , ,

  • .
  • .
0

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


All Articles