c# - 使用带有子方法的父子类的设计模式是什么?
问题描述
当我想建立一个父子关系类时,我多次遇到这个问题。
我有一个基AuthenticateRequest
类。就我而言,我有 2 个子请求,但它们有自己的逻辑GetContent()
。
它并不真正属于复合模式或Liskov 替换,因为基本方法是唯一的并被调用。
我应该使用哪种设计模式?
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
};
}
解决方案
你看到工厂模式了吗?
但是对于您的问题,要求该类具有正确的实现。您可以像这样简单地使用抽象关键字
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);
}
}
推荐阅读
- python - python - 如何将dict中的csv格式值转换为python中的JSON格式?
- sql - 在oracle中从字符串中删除重复值
- python - JSON 文件:使用 Python 对不同对象进行单独的字数统计
- ruby - 弃用:HTTParty 将不再覆盖 `response#nil?`。这个弃用警告是什么意思?
- javascript - 更新类 javascript 中函数的结果
- javascript - TypeORM 自定义存储库,从指定列中选择不同的值
- php - Array_combine 错误:输出错误的值并仅给出唯一值 php
- python - 将条件格式应用于excel中的整行
- linux - 从linux中的top命令计算CPU使用率
- python - 无法导入名称“类型”