我应该遵循哪种模式?是策略模式还是常规的抽象方法 [英] Which pattern should i follow? Whether Strategy pattern or normal Abstract methods

查看:90
本文介绍了我应该遵循哪种模式?是策略模式还是常规的抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我被困在必须遵循的模式中.

要求:我有3种方法,分别是PopulateAccountBalance(),PopulatePayeeAccount()和PopulateDistributionDate().在这里,两种方法PopulateAccountBalance()和PopulatePayeeAccount()的实现将根据3个GroupType(即Group1,Group2和Group 3)而有所不同.
我知道我可以选择策略模式",也可以选择具有父子关系的普通抽象类,其中基类充当父类,每个组有3个单独的子类.

在第二种选择中,基类将是抽象类,并且将保留正在实现的通用方法PopulateDistributionDate(),而将基于子类而变化的其他两个方法将是抽象方法.子类的职责是按其方式实现抽象方法.

现在我应该遵循哪种方式.我应该选择策略模式还是抽象类方法.

I have a requirement and i am addled to which pattern i must go for.

Requirement : I have 3 methods namely PopulateAccountBalance(), PopulatePayeeAccount() and PopulateDistributionDate(). Here the two methods PopulateAccountBalance() and PopulatePayeeAccount() implementation is going to vary based on 3 GroupTypes namely Group1 , Group2 and Group 3.

I know i can go either for Strategy Pattern OR a normal abstract classes with Parent-child relationship where base class acts as parent and 3 separate child classes for each Group.

In the second choice the base class will be an abstract class and will hold common method PopulateDistributionDate() being implemented and the other two methods which gonna vary based on child class will be abstract methods. It is the duty of the child classes to implement abstract methods on their way.

Now which way should i follow. Whether shall i go for Strategy pattern or Abstract class way.

推荐答案

我应该选择抽象类方法(第二种方法).

更新:

这很难证明.这绝对是您的业务需求.

一个明显的优点是您可以在基类本身中部分或完全实现这些方法.

您可以阅读以下文章以获得更多信息:

抽象类与接口 [ MSDN参考 [
I would go for Abstract class way (Second Method).

Update:

It is difficult to justify. It is definitely upto your business need.

One clear advantage is you can have partial or full implementation of the methods in the base class itself.

You can read the following articles to get more information:

Abstract Class versus Interface[^]

MSDN Reference[^]

Mark it as answer if it is helpful


这篇关于我应该遵循哪种模式?是策略模式还是常规的抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆