想要使用SOLID“O”重构C#代码。原则 [英] Want to refactor C# code using SOLID "O" principles
本文介绍了想要使用SOLID“O”重构C#代码。原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
want to write this code in simplified way using SOLID principles. Any help would be highly appreciated.Can i implement inheritance to do this.
void CalculateTaxAndRewardPoints()
{
if (state == TEXAS)
{
rate = TX_RATE;
amt = baseRate * TX_RATE;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
else if ((state == OHIO) || (state == MAINE))
{
if (state == OHIO)
rate = OH_RATE;
else
rate = MN_RATE;
amt = baseRate * rate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
if (state == OHIO)
points = 2;
}
else
{
rate = 1;
amt = baseRate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
}
我的尝试:
What I have tried:
void CalculateTaxAndRewardPoints()
{
while (state == TEXAS)
{
rate = TX_RATE;
amt = baseRate * TX_RATE;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
do while ((state == OHIO) || (state == MAINE))
{
while (state == OHIO)
rate = OH_RATE;
else
rate = MN_RATE;
amt = baseRate * rate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
do while (state == OHIO)
points = 2;
}
Do while
{
rate = 1;
amt = baseRate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
}
推荐答案
阅读本CP文章(附代码示例),了解如何实施开放/封闭原则:使用简单的C#示例的SOLID架构原则 [ ^ ]
Have a read of this CP article (with code examples) on how to implement the Open/Closed principle: SOLID architecture principles using simple C# examples[^]
这篇关于想要使用SOLID“O”重构C#代码。原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文