当前位置: 首页 > 知识库问答 >
问题:

C#支付模块的设计模式

郜谦
2023-03-14

由于我正在学习通过设计模式的概念,也希望实现支付模块在我的项目中使用适当的设计模式。为此,我创建了一些示例代码。

目前,我有两个具体的实现支付PayPal信用卡。但具体实施将在项目上进一步补充。

支付服务

public interface IPaymentService
{
    void MakePayment<T>(T type) where T : class;
}

信用卡和付费朋友服务

public class CreditCardPayment : IPaymentService
{
    public void MakePayment<T>(T type) where T : class
    {
        var creditCardModel = (CreditCardModel)(object)type;
        //Implementation CreditCardPayment
    }
}

class PayPalPayment : IPaymentService
{
    public void MakePayment<T>(T type) where T : class
    {
        var payPalModel = (PayPalModel)(object)type;
        //Further Implementation will goes here
    }
}

客户端代码实现

var obj = GetPaymentOption(payType);
obj.MakePayment<PayPalModel>(payPalModel);
private static IPaymentService GetPaymentOption(PaymentType paymentType)
{
        IPaymentService paymentService = null;

        switch (paymentType)
        {
            case PaymentType.PayPalPayment:
                paymentService = new PayPalPayment();
                break;
            case PaymentType.CreditCardPayment:
                paymentService = new CreditCardPayment();
                break;
            default:
                break;
        }
        return paymentService;
}
static void Main(string[] args)
{
    PaymentStrategy paymentStrategy = null;


    paymentStrategy = new PaymentStrategy(GetPaymentOption((PaymentType)1));
    paymentStrategy.Pay<PayPalModel>(new PayPalModel() { UserName = "", Password = "" });

    paymentStrategy = new PaymentStrategy(GetPaymentOption((PaymentType)2));
    paymentStrategy.Pay<CreditCardModel>(
       new CreditCardModel()
    {
        CardHolderName = "Aakash"
    });

    Console.ReadLine();

}
public class PaymentStrategy
{
    private readonly IPaymentService paymentService;
    public PaymentStrategy(IPaymentService paymentService)
    {
        this.paymentService = paymentService;
    }

    public void Pay<T>(T type) where T : class
    {
        paymentService.MakePayment(type);
    }
}

此更新是否与策略模式内联?

共有1个答案

拓拔辰钊
2023-03-14

为此使用抽象工厂的一个主要缺点是它包含一个switch case语句。这意味着如果您想要添加支付服务,就必须更新工厂类中的代码。这违反了开放-封闭原则,即实体应该对扩展开放,但对修改关闭。

请注意,由于同样的原因,使用枚举在支付提供商之间切换也是有问题的。这意味着每次添加或删除支付服务时,服务列表都必须更改。更糟糕的是,支付服务可以从策略中删除,但仍然是它的枚举符号,即使它是无效的。

另一方面,使用strategy模式不需要switch case语句。因此,在添加或删除支付服务时,不会对现有类进行更改。这一点,以及支付选择的数量可能会被限制在一个小的两位数数字的事实,使得战略模式更适合这种情况。

// Empty interface just to ensure that we get a compile
// error if we pass a model that does not belong to our
// payment system.
public interface IPaymentModel { }

public interface IPaymentService
{
    void MakePayment<T>(T model) where T : IPaymentModel;
    bool AppliesTo(Type provider);
}

public interface IPaymentStrategy
{
    void MakePayment<T>(T model) where T : IPaymentModel;
}
public class CreditCardModel : IPaymentModel
{
    public string CardHolderName { get; set; }
    public string CardNumber { get; set; }
    public int ExpirtationMonth { get; set; }
    public int ExpirationYear { get; set; }
}

public class PayPalModel : IPaymentModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
}
public abstract class PaymentService<TModel> : IPaymentService
    where TModel : IPaymentModel
{
    public virtual bool AppliesTo(Type provider)
    {
        return typeof(TModel).Equals(provider);
    }

    public void MakePayment<T>(T model) where T : IPaymentModel
    {
        MakePayment((TModel)(object)model);
    }

    protected abstract void MakePayment(TModel model);
}
public class CreditCardPayment : PaymentService<CreditCardModel>
{
    protected override void MakePayment(CreditCardModel model)
    {
        //Implementation CreditCardPayment
    }
}

public class PayPalPayment : PaymentService<PayPalModel>
{
    protected override void MakePayment(PayPalModel model)
    {
        //Implementation PayPalPayment
    }
}
public class PaymentStrategy : IPaymentStrategy
{
    private readonly IEnumerable<IPaymentService> paymentServices;

    public PaymentStrategy(IEnumerable<IPaymentService> paymentServices)
    {  
        this.paymentServices = paymentServices ?? throw new ArgumentNullException(nameof(paymentServices));
    }

    public void MakePayment<T>(T model) where T : IPaymentModel
    {
        GetPaymentService(model).MakePayment(model);
    }

    private IPaymentService GetPaymentService<T>(T model) where T : IPaymentModel
    {
        var result = paymentServices.FirstOrDefault(p => p.AppliesTo(model.GetType()));
        if (result == null)
        {
            throw new InvalidOperationException(
                $"Payment service for {model.GetType().ToString()} not registered.");
        }
        return result;
    }
}
// I am showing this in code, but you would normally 
// do this with your DI container in your composition 
// root, and the instance would be created by injecting 
// it somewhere.
var paymentStrategy = new PaymentStrategy(
    new IPaymentService[]
    {
        new CreditCardPayment(), // <-- inject any dependencies here
        new PayPalPayment()      // <-- inject any dependencies here
    });


// Then once it is injected, you simply do this...
var cc = new CreditCardModel() { CardHolderName = "Bob" /* Set other properties... */ };
paymentStrategy.MakePayment(cc);

// Or this...
var pp = new PayPalModel() { UserName = "Bob" /* Set other properties... */ };
paymentStrategy.MakePayment(pp);
  • 依赖注入统一-条件解析
  • 带有DI和ioc的工厂方法
 类似资料:
  • 5.1版本默认采用多模块的架构,并且支持单一模块设计,所有模块的命名空间均以app作为根命名空间(可通过环境变量更改)。 目录结构 标准的应用和模块目录结构如下: ├─application 应用目录(可设置) │ ├─common 公共模块目录(可选) │ ├─module1 模块1目录 │ │ ├─common.php

  • 本文向大家介绍javascript设计模式之module(模块)模式,包括了javascript设计模式之module(模块)模式的使用技巧和注意事项,需要的朋友参考一下 模块是任何强大应用程序中不可或缺的一部分,它通常能帮助我们清晰地分离和组织项目中的代码单元。 js中实现模块的方法:  1.对象字面量表示法  2.Module模式  3.AMD模块  4.CommonJS模块  5.ECMAS

  • 问题内容: 最近,我碰到了C ++的Singleton设计模式的实现/实现。它看起来像这样(我从现实生活的示例中采用了它): 从该声明中,我可以推断出实例字段是在堆上初始化的。这意味着存在内存分配。对我来说,完全不清楚的是何时确切地将要释放内存?还是有错误和内存泄漏?似乎实现中存在问题。 我的主要问题是,如何以正确的方式实施它? 问题答案: 在2008年,我提供了Singleton设计模式的C +

  • 我有一个抽象类“A”,它具有枚举类型“OutputType”的属性,还有一个抽象方法calculation(),它需要执行certin计算,并根据OutputType值以double[]**形式输出结果。 我也有一个类定义为D1,D2,D3...D20,从A派生,其中每个Di类实现A的计算()方法不同。 问题是并非所有的OutputType值(calculation()输出类型)都在每个Di中都受

  • 问题内容: 我最近正在读《 学习JavaScript设计模式》 这本书。我没有得到的是模块模式和显示模块模式之间的区别。我觉得他们是同一回事。有人可以举一个例子吗? 问题答案: 至少有三种不同的方法来实现模块模式,但是显示模块模式是唯一具有正式名称的模块模式后代。 基本模块模式 模块模式必须满足以下条件: 私有成员住在封闭中。 公共成员暴露在返回对象中。 但是这个定义有很多歧义。通过以不同方式解决

  • 本文向大家介绍C++设计模式之享元模式,包括了C++设计模式之享元模式的使用技巧和注意事项,需要的朋友参考一下 前言 无聊的时候,也去QQ游戏大厅玩五子棋或者象棋;作为程序员,看到一个产品,总要去想想它是怎么设计的,怎么完成的,我想这个是所有程序员都会做的事情吧(强迫症???)。有的时候,想完了,还要做一个DEMO出来,才能体现自己的NB,然后还有点小成就感。 在玩五子棋或象棋的时候,我就想过,腾