简体   繁体   English

如何保持私有财产的可扩展性?

[英]How do I keep extensibility with private properties?

I'm working on the following class. 我正在上下一堂课。 I've shortened the list of properties as an example. 作为示例,我已经缩短了属性列表。

public class Paycheck 
{
   public decimal Gross { get; private set; }
   public decimal FederalWithholdingTax { get; private set; }
   public decimal StateWithholdingTax  { get; private set;  }
   // ...
}

Now my idea was to create an interface to calculate all the taxes... 现在我的想法是创建一个接口来计算所有税额...

public interface IPaycheckDeductionCalculator
{
   void Calculate();
}

The idea after that is to create methods outside of the Paycheck class that can perform calculations/deductions. 之后的想法是在Paycheck类之外创建可以执行计算/扣除的方法。 With the way laws and payroll processes work, how deductions are performed and what deductions are performed are constantly changing, so I want to keep extensibility easy. 通过法律和薪资流程的工作方式,演绎方式以及演绎方式都在不断变化,因此,我想保持可扩展性容易。

My dilemma is that I want to keep the Paycheck property setters private so that just anyone can change those values. 我的困境是我想将Paycheck属性设置器设为私有,以便任何人都可以更改这些值。 Keeping that data constant after a paycheck is processed is critical. 在处理薪水后保持该数据恒定至关重要。

I considered the use of delegates, but I'm not so sure that's my answer either, as I still wouldn't be able to develop the desired methods outside of the Paycheck class. 我考虑过使用委托,但是我也不确定那是我的答案,因为我仍然无法在Paycheck类之外开发所需的方法。

Is there something I'm not thinking of or is this a "you can't have your cake and eat it too" kind of situation? 有什么我不介意的东西吗?还是“你不能吃蛋糕也不能吃”这种情况?

Have the interfaces that calculate these values return the values that they are calculating. 让计算这些值的接口返回它们正在计算的值。 Pass in instances of these interfaces to Paycheck , which can the perform the calculations (without needing to know anything about the implementation of the interface) and then have it set the values of the appropriate properties. 将这些接口的实例传递给Paycheck ,后者可以执行计算(无需了解任何有关接口实现的信息),然后让其设置适当属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM