One case for adaptive design under multi-class inheritance and one case for multiple classes

Source: Internet
Author: User

One case for adaptive design under multi-class inheritance and one case for multiple classes

In the Implementation Layer of the CITIC payment channel in the payment center, the class diagram design method for the Interconnection Implementation of each payment interface is as follows (the procedure framework of the payment center-hierarchical structure is attached ), api implementation of each payment interface, such as obtaining dynamic Payment Code, public number/service window, order query, order close, refund, pay-as-you-go, and pay-as-you-go query, all inherit the same base class.


ClassDiagrampublic abstract class CiticAPIBase <TRequestModel, TResponseModel> where TRequestModel: RequestDTOBase where TResponseModel: ResponseDTOBase {readonly string LOG_FLAG = string. format ("[{0} CITIC {1}]", DateTime. now. toString ("HHmmss"), new Random (). next (9999); protected LogHelperUtil _ LogHelperUtil; public CiticAPIBase () {_ LogHelperUtil = new LogHelperUtil (LOG_FLAG);} public abstract trew.semodel Invoke (TRequestModel reqModel ); public string Communicate (RequestModelCommon citicReqModel) {try {CiticCommon citicCommon = new CiticCommon (LOG_FLAG); var json = citicCommon. invoke (_ reqModel); // _ LogHelperUtil. writeLog ("channel interface processing result: {0}", json); return json;} catch (ResponseErrorException ex) {throw new ResponseErrorException ("[upstream channel]" + ex. message );}}}

 

One of the derived classes _ 61 InitJSAPI (the implemented payment interface is the public account/service window), override the Invoke method:

Public class _ 61 InitJSAPI: CiticAPIBase <strong, JSPayResponseDTO> {public override partial Invoke (Response reqDto) {var citicReqDto = new _ 61 partial () {out_trade_no = reqDto. order_no, body = reqDto. goods_name, total_defaults = reqDto. pay_money, mch_create_ip = ReadIp. ip_GetIPAddress (), notify_url = PartnerConfig. payNotifyUrl, callback_url = reqDto. return_url, is _ Raw = "1", // reqModel1.is _ raw, our company and Qingdao CITIC are allocated with original ecological js payment. Therefore, it is hard to write. Sub_openid = reqDto. user_client_name, // TODO: mch_id = reqDto. merchant_id,}; if (citicReqDto. mch_id = PartnerConfig. MCH_ID_TEST) {citicReqDto. sub_openid = string. empty;} // ---- call the var json = base of the CITIC payment channel. communicate (citicReqDto); var respModel = JsonConvert. deserializeObject <_ 61 InitJSAPIResponseModel> (json); string pay_url = string. format (" https://pay.swiftpass.cn/pay/jspay?token_id= {0} & showwxtitle = 1 ", model. token_id); var returnDto = new JSPayResponseDTO () {StatusIsSuccess = true, ReturnCodeIsSuccess = true, pay_info = respModel. pay_info, pay_url = pay_url, }; return returnDto ;}}

 

The Code of _ 8Reverse, one of the derived classes (the implemented payment interface is to close the ticket), override the Invoke method:

/// <Summary> /// 8 close the order interface /// </summary> public class _ 8 Reverse: CiticAPIBase <ReverseRequestDTO, responseDTOBase> {public _ 8 Reverse (string logFlag): base (logFlag) {} public override ResponseDTOBase Invoke (ReverseRequestDTO reqDto ){......}}

 

Next we will discuss the implementation scheme.

I started with the constructor. The constructor adds a logFlag parameter. The caller passes logFlag when initializing a specific object. The base class is changed to the following:

public abstract class CiticAPIBase<TRequestModel, TResponseModel>    where TRequestModel : RequestDTOBase    where TResponseModel : ResponseDTOBase{    readonly string LOG_FLAG = string.Format("[{0}CITIC{1}]", DateTime.Now.ToString("HHmmss"), new Random().Next(9999));    protected LogHelperUtil _LogHelperUtil;    public CiticAPIBase(string logFlag)    {        LOG_FLAG = logFlag + LOG_FLAG;        _LogHelperUtil = new LogHelperUtil(LOG_FLAG);    }    public abstract TResponseModel Invoke(TRequestModel reqModel);    public string Communicate(RequestModelCommon citicReqModel)    {        ... ...    }}

 

Then, each derived class must explicitly declare the constructor with the logFlag parameter added:

public class _61InitJSAPI : CiticAPIBase<JSPayRequestDTO, JSPayResponseDTO>{    public _61InitJSAPI(string logFlag) : base(logFlag) { }    public override JSPayResponseDTO Invoke(JSPayRequestDTO reqDto)    {        ... ....    }}public class _8Reverse : CiticAPIBase<ReverseRequestDTO, ResponseDTOBase>{    public _8Reverse(string logFlag) : base(logFlag) { }    public override ResponseDTOBase Invoke(ReverseRequestDTO reqDto)    {        ... ...    }}

 

It is quite troublesome to modify all eight derivatives,It also violates the OCP Principle. In addition, from the perspective of the field, the logFlag parameter has no relationship with the entire function. This parameter is added simply to improve the logging. Therefore, the above implementation scheme is inappropriate. To encapsulate a LogFlag attribute. In this way, you only need to modify the base class, and the derived class does not need to be changed. After instantiating an object, the caller can assign a value to the LogFlag attribute (if possible ).

public abstract class CiticAPIBase<TRequestModel, TResponseModel>    where TRequestModel : RequestDTOBase    where TResponseModel : ResponseDTOBase{    public string LogFlag { set { _LOG_FLAG = value + _LOG_FLAG; } }    string _LOG_FLAG = "";    protected LogHelperUtil _LogHelperUtil;    public CiticAPIBase()    {        _LOG_FLAG = string.Format("[{0}CITIC{1}]", DateTime.Now.ToString("HHmmss"), new Random().Next(9999));        _LogHelperUtil = new LogHelperUtil(_LOG_FLAG);    }    public abstract TResponseModel Invoke(TRequestModel reqModel);    public string Communicate(RequestModelCommon citicReqModel)    {        ... ...    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.