- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个类有两个重要功能:
public class Foo {
//plenty of properties here
void DoSomeThing(){/*code to calculate results*/}
void SaveSomething(){/* code to save the results in DB*/}
}
SaveSomething()
使用在 DoSomeThing()
中计算的结果。
问题是我们不能在 DoSomeThing()
之前调用 SaveSomething()
,否则如果发生这种情况,结果将不是真实结果。我的意思是调用顺序很重要,这是维护代码的一个问题。(当新的添加到团队时)。
有什么办法可以解决这个问题吗?
我想到了以下3种方法
DoSomeThing()
之前调用,则在 SaveSomething()
中抛出异常在 DoSomeThing()
和 SaveSomething()
中设置了一个 bool
代码更改为:
bool resultsAreCalculated = false;
void SaveSomething(){
if (!resultsAreCalculated) {
DoSomeThing();
// the resultsAreCalculated = true; is set in DoSomeThing();
// can we throw some exception?
}
/* code to save the results in DB*/
}
像 Fluent 一样实现它:
Foo x = new Foo();
x.DoSomeThing().SaveSomething();
在这种情况下,重要的是要保证这种情况不会发生:
x.SaveSomething().DoSomeThing();
现在,我使用第二种方法。有没有更好的方法或者这就足够了?
最佳答案
理想情况下,需要遵循特定顺序执行的方法表示或暗示需要实现某种工作流。
有一些设计模式支持强制执行类似工作流的线性执行顺序,例如 Template Method Pattern , 或 Strategy .
参加Template Method方法,您的 Foo 类将有一个定义执行 Do()
和 Save()
顺序的抽象基类,类似于:
public abstract class FooBase
{
protected abstract void DoSomeThing();
protected abstract void SaveSomething();
public void DoAndSave()
{
//Enforce Execution order
DoSomeThing();
SaveSomething();
}
}
public class Foo : FooBase
{
protected override void DoSomeThing()
{
/*code to calculate results*/
}
protected override void SaveSomething()
{
/* code to save the results in DB*/
}
}
这样,您类消费者将只能访问 DoAndSave()
,并且他们不会违反您预期的执行顺序。
还有另一种模式可以处理工作流/状态转换类型的情况。可以引用Chain of Command , 和 State图案。
回应您的评论:这遵循相同的模板思想,您在模板中添加另一个步骤,假设您想在保存前验证结果,您可以将模板扩展为:
public abstract class FooBase
{
protected abstract void DoSomeThing();
protected abstract void SaveSomething();
protected abstract bool AreValidResults();
public void DoAndSave()
{
//Enforce Execution order
DoSomeThing();
if (AreValidResults())
SaveSomething();
}
}
当然,对于更详细的工作流程,我在原始答案末尾提到了状态模式,您可以更精细地控制从一种状态到另一种状态的转换条件。
关于c# - 类里面的通话顺序很重要时的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8229293/
在我的应用程序中,我使用 scrape(string url) 方法从网页中抓取链接。可以说它每次都返回我 10 个 url。 我想从每个抓取的 url 中抓取 10 个链接。 长话短说: (第 1
正如标题所说,我需要将 php 放入由 php 回显的 javascript 中 例如: if (something) then (some php)') ?> 这不是真正的代码,只是可能有助于理解
我有一个 ng-repeat 使用 ng-switch 来分隔 ng-repeat 内部的部分,我想在 ng-switch 组上应用一个 orderby 过滤器,但我似乎无法让它工作:
我是 shell 的新手。我不太了解以下功能。这个函数基本上把小时加1。 我想知道为什么开发人员在 $g_current_hour+1 前面放了“10#”。根据我的理解,shell 中的剂量 # 是指
为了在 MongoDB 中对元素进行分组和计数,我已经筋疲力尽了。有很多帖子,但没有一个是我需要的。 这是一个基于以下内容的示例: styvane answer db.VIEW_HISTORICO.a
int main() { int count = 0; string prev = " "; string current; while (cin>>current)
$0表示传递给swift闭包的第一个参数,是swift语言的语法糖。swift会自动为内联闭包提供速记参数名称,可使用$0,$1,$2等名称来引用闭包参数的值。 代码
我想把这个 If 放在宏中,但它总是给我一个错误。我不知道“或”用得是否正确。 Dim SMAT As String SMAT = "blahblahblah" (...) If Ce
这是 JSFiddle 链接:https://jsfiddle.net/fmdsu9Lo/ 我想添加一个悬停功能,以便将鼠标悬停在菜单上将在该菜单选项卡上应用 .current 样式,同时将其从当前打
是否可以在 SQL 中执行类似的操作? SELECT SUM(jobRequirements.number) as total, SUM(jobRequirements.number WHER
我有 3 个 SQL 表。 游戏 wo_tenis_partidos 选择 wo_tenis_pronosticos 配额 wo_tenis_cuotas_ha2 我需要获取满足两个条件的所有行: 条
我有这张乘客表日期: 这意味着在 Reservation 000004 中,有两个行程,分别是 Itinerary 0000010 (AAC Hangar - Anvaya Cove) 和 00000
里面
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我在模态框内显示一个弹出窗口,我可以在关闭时隐藏它,但在点击模态框外时无法隐藏它。我试着在关闭时隐藏它,这很好。代码在这里 $('.close, .close-button').click(funct
我所在的团队正在使用 MVC 架构开发 C# 程序。 当我完成 MVC 教程时,我有一些疑问。请帮我澄清一下这个疑惑。 1) Controller 可以对 View 进行哪些修改? 2)我认为所有 U
我正坐在一些通过#defines 生成大量代码的遗留代码上。现在我知道在 #define 中不可能有 #ifdef,但是 #if 可能吗?我想为特定类型添加一些特化。 (无需进行重大更改,例如使用模板
这是我试图用来创建触发器的代码: -- Dumping structure for trigger kavanga_lead.click_links_insert DROP TRIGGER IF EX
我想写这样的东西: #define COV_ON(x) \ #ifdef COVERAGE_TOOL \ _Pragma (CO
我在这样的定义的帮助下初始化一个结构数组: #define FLAGCODE(name) { #name, MNT_ ## name } struct { const char
我尝试将 css 和 jquery 应用于 div 元素,但它不接受 $(this) 参数。我想知道为什么我不能使用 $(this) 作为 div 元素的引用。 blabla.css({ 'top':
我是一名优秀的程序员,十分优秀!