将父级和子级类与子方法一起使用的设计模式是什么?

时间:2018-06-25 09:11:02

标签: c# design-patterns object-oriented-analysis

当我想建立一个父子关系类时,我已经多次面对这个问题。
我有一个AuthenticateRequest基类。就我而言,我有2个子请求,但它们对GetContent()有自己的逻辑。
它并不是真正属于Composite PatternLiskov Substitution,因为基本方法是唯一的且被调用。
我应该使用哪种设计模式?

public class AuthenticateRequest
{
    public string Url { get; set; }

    public string ContentType { get; set; }

    public string Method { get; set; }

    public virtual HttpContent GetContent()
    {
        return new StringContent("");
    }
} 

public class SoapAuthenticateRequest : AuthenticateRequest
{
    public string SoapMethodName { get; set; }

    public string SoapAction { get; set; }

    public string KeyForUserNameParameter { get; set; }

    public string ValueForUserNameParameter { get; set; }

    public string KeyForPasswordParameter { get; set; }

    public string ValueForPasswordParameter { get; set; }

    public override HttpContent GetContent()
    {
        var methodName = this.SoapMethodName;
        var keyUsername = this.KeyForUserNameParameter;
        var keyPwd = this.KeyForPasswordParameter;
        var valueUsername = this.ValueForUserNameParameter;
        var valuePwd = this.ValueForPasswordParameter;
        var soapAction = this.SoapAction ?? @"http://tempuri.org/"; 

        var soap = $@"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body><{methodName} xmlns=""{soapAction}""><{keyUsername}>{valueUsername}</{keyUsername}><{keyPwd}>{valuePwd}</{keyPwd}></{methodName}></soap:Body></soap:Envelope>";

        return new StringContent(soap, Encoding.UTF8, ContentTypes.XmlSoap);
    }
}

public class JsonAuthenticateRequest : AuthenticateRequest
{
    public string SoapMethodName { get; set; }

    public string SoapAction { get; set; }

    public Dictionary<string, string> ParameterKeyValues { get; set; }

    public override HttpContent GetContent()
    {
        var json = JsonConvert.SerializeObject(ParameterKeyValues);
        return new StringContent(json, Encoding.UTF8, ContentTypes.Json);
    }
}

public async Task<AuthenticateResponse> Authenicate(AuthenticateRequest request)
{
    var requestMsg = new HttpRequestMessage
    {
        RequestUri = new Uri(request.Url),
        Method = new HttpMethod(request.Method.ToString()),
        Content = request.GetContent(),
    };

    var responseMsg = await _httpClient.SendAsync(requestMsg).ConfigureAwait(false);
    var responseContent = await responseMsg.Content.ReadAsStringAsync().ConfigureAwait(false);

    return new AuthenticateResponse
    {
        Message = responseContent,
        IsSuccess = Regex.Match(responseContent, (string)request.RegexForValidUser).Success
    };
}

1 个答案:

答案 0 :(得分:0)

您看到工厂模式了吗?

但是对于您的问题,要求类具有正确的实现。您只需使用this

之类的抽象关键字即可
public abstract class AuthenticateRequest
{    
    public abstract HttpContent GetContent();
} 

public class SoapAuthenticateRequest : AuthenticateRequest
{
    public override HttpContent GetContent()
    {
        // your logic

        return new StringContent(soap, Encoding.UTF8, ContentTypes.XmlSoap);
    }
}

public class JsonAuthenticateRequest : AuthenticateRequest
{
    public override HttpContent GetContent()
    {
        var json = JsonConvert.SerializeObject(ParameterKeyValues);
        return new StringContent(json, Encoding.UTF8, ContentTypes.Json);
    }
}