gpt4 book ai didi

c# - 使用哪些设计模式来实现该业务逻辑?

转载 作者:行者123 更新时间:2023-12-02 13:16:31 25 4
gpt4 key购买 nike

我正在尝试确定用于业务 key 验证 Web 服务的最佳设计模式。基本逻辑流程编码如下。该程序将采用一个参数并使用一个字段来帮助确定在搜索可以找到此业务 key 的多个系统时要采用的路径。首先搜索System1,如果没有找到,再搜索System2和System3。 System1 搜索逻辑取决于传递到原始验证方法的参数中的字段。

我不太确定要使用哪种设计模式。看起来命令、责任链、模板方法都可以用在这里。

通过下面的实现,我发现以下问题:

  1. 每个 SearchSystemX 方法都需要知道在未找到业务键时返回 null,以便“控制”方法继续搜索其他系统。

  2. 每个 SearchSystemX 必须知道如何填充业务对象,目前仅通过简单的原始字符串实现,但这仅作为示例。

请告诉我你的想法。

public string Validate (string parms) {
string returnValue = null;

returnValue = SearchSystem1(parms);

if (returnValue == null) {
returnValue = SearchSystem2(parms);

if (returnValue != null) {
returnValue = SearchSystem3(parms);
}
else if (returnValue == null) {
if (parms == "Criteria1") {
returnValue = SearchSystem4(parms);

if (returnValue == null) {
throw new ApplicationException("ID Invalid");
}
}
else if (parms == "Criteria2") {
throw new ApplicationException("ID Invalid");
}
}
}
return returnValue;

private string SearchSystem1 (string parms) {
string returnValue = null;

if (parms == "Criteria1") {
returnValue = SearchSystem1UsingColumn1(parms);
}
else if (parms == "Criteria2") {
returnValue = SearchSystem1UsingColumn2(parms);

if (returnValue == null) {
returnValue = SearchSystem1UsingColumn4(parms);
}
}

if (returnValue != null) {
returnValue = FetchXYCoordinate(parms);
}

return returnValue;
}

谢谢!

最佳答案

Chain of responsability

Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those that it cannot to the next processing object in the chain

因此,您定义并抽象 SearchSystem (或 SystemSearch )并添加子类,如下所示:

class SearchSystem
{
//link to the next in the chain
SearchSystem next;

// Basic search, If cannot handle forward to the next.
public Result search( Criteria c )
{
Result r = doSearch( c );

if( r != null )
{
return r;
}

return next.search( c );
}
// subclass specific system search
abstract Result doSearch( Criteria c );
}

class SearchSystemOne: SearchSystem
{
Result doSearch( Criteria c )
{
// do some system 1 speficif stuff
// and return either and instance or null
}
}
class SearchSystemTwo: SearchSystem
{
Result doSearch( Criteria c )
{
// do some system 2 speficif stuff
// and return either and instance or null
}
}
.... etc etc.
// End of the chain
class SearchSystemOver: SearchSystem
{
public Result search( Criteria c )
{
throw new ApplicationException("Criteria not foud", c );
}
Result doSearch( Criteria c )
{
// didn't we throw exception already?
}
}

实例化

SearchSystem one = new SearchSystemOne();
SearchSystem two = new SearchSystemTwo();
SearchSystem three = new SearchSystemThree();
SearchSystem four = new SearchSystemFour();
SearchSystem over = new SearchSystemOver();

并建立链条

one.next = two;
two.next = three;
three.next = four;
four.next = over;

最后搜索它。

SearchSystem searcher = one;

searcher.search( Criteria.addName("Oscar").addLastName("Reyes"));

关于c# - 使用哪些设计模式来实现该业务逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com