设计问题:“通用应用程序/流程”对于多个客户端,结果会充满“但是”。
我即将为我的客户创建一个应用程序。 他(我的客户)的多个客户将登录该应用程序并执行相同的操作。 因此,从这个角度来看,我们有一个“通用应用程序”:编写一次,适合每个客户的需求。
但是,在编写第一行代码之前,我们当然有第一个例外:当客户端 A 执行操作 A 时,您必须执行与客户端 B、C 和 D 相同的操作,而且......
当然,我们可以等待对第一个想到的“所有人的一个流程”进行更多此类修改。
没问题,无法预见一切,而且源代码很灵活。
但是,我预见到我的源代码包含各种 if 语句:
if (client == "a")
{
SetEndDate(+1);
}
else if (client == "b")
{
SetEndDate(+10);
}
else //no enddate modification needed for other clients
if (client == "d" || client == "E" )
{
DoExtraCheck1();
}
else if (client = "b")
{
if ( DoExtraCheck1())
{
DoExtraCheck2();
}
}
else //no checking needed for other clients
我对此感到害怕!
什么是一个优雅的解决方案来简化这个过程?
接下来要解决的当然是客户端之间的 UI 是否也发生变化,但这是针对 V2 的问题:)
i'm about to create an app for my client.
Multiple clients of him (of my client) will login to that app and do the same thing.
So in that view, we have a 'generic app': written once, suits every clients needs.
But, before the first line of code is written, we have offcourse the first exception: when client A does action A, you have to do the same as for clients B, C and D, but also.....
And of course, we can wait for more of that kind of modifications of the first thought out 'one flow for all'.
No problem, can't foresee all, and source code is flexible.
But, i foresee my source code with all kinds of if statements:
if (client == "a")
{
SetEndDate(+1);
}
else if (client == "b")
{
SetEndDate(+10);
}
else //no enddate modification needed for other clients
if (client == "d" || client == "E" )
{
DoExtraCheck1();
}
else if (client = "b")
{
if ( DoExtraCheck1())
{
DoExtraCheck2();
}
}
else //no checking needed for other clients
and i'm getting scared of that!
What could be a nice elegant solution to streamline this?
Next thing to solve is of course if the UI also changes between clients, but that is for the V2 question :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理这些事情的一个好方法是策略模式。您可以为所有客户端实施默认策略,并为具有特殊行为的客户端将其子类化。源代码中的
if
将通过多态性进行处理。不改变接口的额外处理(例如检查不变量或类似的东西)可以使用装饰器模式< /a>.
两者之间的区别在于装饰器模式旨在扩展功能,而使用策略模式您实际上可以更改实现。
这两种解决方案都假设您可以为所有客户端定义一个接口,因此可以利用多态性。
A good way to handle these things is the strategy pattern. You can implement a default strategy for all clients, and subclass it for clients that have special behavior. The
if
s in your source code will be handled by polymorphism.Extra processing (e.g. checking invariants or stuff like that) that does not change the interface can be implemented using the decorator pattern.
The difference between the two is that the decorator pattern is intended to extend functionality, where with the strategy pattern you can actually change the implementation.
Both of these solution assume that you can define one interface for all clients, and thus can exploit polymorphism.