当前位置: 首页 > 编程语言 > C#

DesignPatternsc#forPaymentModuleShare

时间:2023-04-11 02:52:50 C#

DesignPatternsc#forPaymentModule由于我正在学习设计模式概念,我也想在我的项目模块中使用适当的设计模式来实现支付。为此,我创建了一些示例代码。目前我有两个具体的支付PayPal和信用卡实现。但具体的实现会在项目中进一步补充。支付服务公共接口IPaymentService{voidMakePayment(Ttype)whereT:class;}信用卡和PayPal服务publicclassCreditCardPayment:IPaymentService{publicvoidMakePayment(Ttype)whereT:class{//实现CreditCardPayment}}classPayPalPayment:IPaymentService{publicvoidMakePayment(Ttype)whereT:class{varpayPalModel=(PayPalModel)(object)type;//FurtherImplementationwillgoeshere}}客户端代码实现varobj=GetPaymentOption(payType);obj.MakePayment(payPalModel);获取支付选项privatestaticIPaymentServiceGetPaymentOption(PaymentTypepaymentType){IPaymentServicepaymentService=null;switch(paymentType){casePaymentType.PayPalPayment:paymentService=newPayPalPayment();休息;casePaymentType.CreditCardPayment:paymentService=newCreditCardPayment();休息;默认值:中断;}返回支付服务;我考虑过使用策略设计模式来实现这些模块,但我偏离了策略并最终走上了这条路。这是创建支付模块的正确方法吗?有没有更好的方法来处理这种情况。这是一种设计模式吗?编辑:客户端代码:staticvoidMain(string[]args){PaymentStrategypaymentStrategy=null;paymentStrategy=newPaymentStrategy(GetPaymentOption((PaymentType)1));paymentStrategy.Pay(newPayPalModel(){UserName="",Password=""});paymentStrategy=newPaymentStrategy(GetPaymentOption((PaymentType)2));paymentStrategy.Pay(newCreditCardModel(){CardHolderName="Aakash"});控制台.ReadLine();}策略:publicclassPaymentStrategy{privatereadonlyIPaymentServicepaymentService;publicPaymentStrategy(IPaymentServicepaymentService){this.paymentService=paymentService;}publicvoidPay(Ttype)whereT:class{paymentService.MakePayment(type);}}这个更新是否符合策略模式?使用抽象工厂的一个主要缺点是它包含switchcase语句。这本质上意味着如果你想添加支付服务,你必须更新工厂类中的代码。这违反了开闭原则,该原则规定实体应该对延期开放,但对修改关闭。请注意,出于同样的原因,使用枚举在支付提供商之间切换也是有问题的。这意味着每次添加或删除支付服务时,服务列表都必须更改。更糟糕的是,支付服务可以从政策中删除,但即使它无效,它仍然是它的Enum符号。另一方面,使用策略模式不需要switchcase语句。因此,添加或删除支付服务时,现有类不会发生任何变化。这一点,以及支付选项的数量可能被限制在一个小的两位数这一事实,使得策略模式更适合这种情况。接口//空接口只是为了确保如果我们传递一个不属于我们的模型//支付系统,我们会得到一个编译错误。publicinterfaceIPaymentModel{}publicinterfaceIPaymentService{voidMakePayment(Tmodel)其中T:IPaymentModel;boolAppliesTo(类型提供者);}publicinterfaceIPaymentStrategy{voidMakePayment(Tmodel)whereT:IPaymentModel;}模型publicclassCreditCardModel:IPaymentModel{publicstringCardHolderName{get;放;}publicstringCardNumber{get;放;}publicintExpirtationMonth{得到;放;}publicintExpirationYear{得到;放;}}publicclassPayPalModel:IPaymentModel{publicstringUserName{get;放;}公共字符串密码{得到;一个抽象类,用于隐藏从IPaymentService实现转换为具体模型类型的丑陋细节。publicabstractclassPaymentService:IPaymentService其中TModel:IPaymentModel{publicvirtualboolAppliesTo(Typeprovider){returntypeof(TModel).Equals(provider);}publicvoidMakePayment(Tmodel)whereT:IPaymentModel{MakePayment((TModel)(object)model);}protectedabstractvoidMakePayment(TModel模型);}支付服务实现publicclassCreditCardPayment:PaymentService{protectedoverridevoidMakePayment(CreditCardModelmodel){//ImplementationCreditCardPayment}}publicclassPayPalvoPaymentServiceoveridedService:Paymentpro(PayPalModelmodel){//ImplementationPayPalPayment}}支付策略这是绑定的类他们都在一起。其主要目的是提供基于传递的模型类型的支付服务选择功能。但与此处的其他示例不同,它松散地耦合了IPaymentService实现,因此此处未直接引用它们。这意味着可以在不更改设计的情况下添加或删除支付提供商。公共类PaymentStrategy:IPaymentStrategy{privatereadonlyIEnumerablepaymentServices;publicPaymentStrategy(IEnumerablepaymentServices){if(paymentServices==null)thrownewArgumentNullException(nameof(paymentServices));this.paymentServices=paymentServices;}publicvoidMakePayment(Tmodel)whereT:IPaymentModel{GetPaymentService(model).MakePayment(model);}privateIPaymentServiceGetPaymentService(Tmodel)whereT:IPaymentModel{varresult=paymentServices.FirstOrDefault(p=>p.AppliesTo(model.GetType()));if(result==null){thrownewInvalidOperationException($“{model.GetType().ToString()}的支付服务未注册。”);}返回结果;}}用法//我在代码中展示了这一点,但你通常会//??在你的合成根目录中使用你的DI容器来执行此操作//并且实例将通过在某处注入//它来创建。varpaymentStrategy=newPaymentStrategy(newIPaymentService[]{newCreditCardPayment(),//其他参考资料:这是您可以采用的方法从您的源代码开始没有太多内容,我真的会重新考虑将MakePayment设为void,而不是像IPayResult这样的东西。publicinterfaceIPayModel{}//值得研究此公共接口IPaymentService的公共共享方法和属性{voidMakePayment(IPayModelpayModel);}publicinterfaceIPaymentService:IPaymentServicewhereT:IPayModel{voidMakePayment(TpayModel);//这里为空?付款状态是否保存在具体的付款模型中?为什么不是IPayResult?}publicclassCreditCardModel:IPayModel{publicstringCardHolderName{get;放;}}publicclassPayPalModel:IPayModel{publicstringUserName{get;放;}公共字符串密码{得到;放;}}publicclassCreditCardPayment:IPaymentService{publicvoidMakePayment(CreditCardModelpayModel){//实现CreditCardPayment}voidIPaymentService.MakePayment(IPayModelpayModel){MakePayment(payModelasCreditCardModel);}}publicclassPayPalPayment:IPaymentService{publicvoidMakePayment(PayPalModelpayModel){//实现PayPalPayment}voidIPaymentService.MakePayment(IPayModelpayModel){MakePayment(payModelasPayPalModel);}}publicenumPaymentType{PayPalPayment=1,CreditCardPayment=2}所以在你的实现之后,它可能看起来像:staticclassProgram{staticvoidMain(object[]args){IPaymentServicepaymentStrategy=null;paymentStrategy=GetPaymentOption((PaymentType)1);paymentStrategy.MakePayment(newPayPalModel{UserName="",Password=""});paymentStrategy=GetPaymentOption((PaymentType)2);paymentStrategy.MakePayment(newCreditCardModel{CardHolderName="Aakash"});控制台.ReadLine();}privatestaticIPaymentServiceGetPaymentOption(PaymentTypepaymentType){switch(paymentType){casePaymentType.PayPalPayment:returnnewPayPalPaymentType();.CreditCardPayment:返回新的CreditCardPayment();默认值:抛出新的NotSupportedException($“不支持付款类型‘{paymentType.ToString()}’”);我还认为对于策略/工厂模式方法,手动创建IPayModel类型没有多大意义因此,您可以将IPaymentService扩展为IPayModel工厂:publicinterfaceIPaymentService{IPayModelCreatePayModel();voidMakePayment(IPayModelpayModel);}publicinterfaceIPaymentService:IPaymentServicewhereT:IPayModel{newTCreatePayModel();voidMakePayment(TpayModel);}publicclassCreditCardPayment:IPaymentService{publicCreditCardModelCreatePayModel(){returnnewCreditCardModel();}publicvoidMakePayment(CreditCardModelpayModel){//实现CreditCardPayment}IPayModelIPaymentService.CreatePayModel(){returnCreatePayModel();}voidIPaymentService.MakePayment(IPayModelpayModel){MakePayment(payModelasCreditCardModel);}}使用方法是:IPaymentServicepaymentStrategy=null;paymentStrategy=GetPaymentOption((PaymentType)1);varpayModel=(PayPalModel)paymentStrategy.CreatePayModel();payModel.UserName="";payModel.Password="";paymentStrategy.MakePayment(payModel);您的代码基础上使用工厂模型。这是处理多种支付方式的好方法http://www.dotnettricks.com/learn/designpatterns/factory-method-design-pattern-dotnet在我看来,这是对策略模式的一个很好的使用。我会说您编写一个名为PaymentStrategy的接口并创建两个具体实现。一个用于Paypal,另一个用于信用卡付款。然后,在客户端内部,你可以根据前端传来的用户选择来决定应该使用哪种支付策略。然后将PaymentStrategy传递给实际执行支付过程的上下文类。在上面的示例中,既没有使用FactoryMethod模式也没有使用AbstractFactory模式。我认为这不是工厂模式的理想选择。不,您正在做的不是策略模式。应该改成这样。publicinterfacePaymentStrategy{voiddoPayment();}publicclassPaypalStrategyimplementsPaymentStrategy{@OverridevoiddoPayment(){//实现这个。}}publicclassPaymentService{privatefinalPaymentStrategypaymentStrategy;publicPaymentService(PaymentStrategy)=paymentStrategy{paymentStrategy;}publicvoidpay(){this.paymentStrategy.doPayment();//在这里多做一些。你的客户应该是这样的。以上就是C#学习教程:适用于支付模块的设计模式c#分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注---newPaymentService(newPaypalStrategy()).pay();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: