公共职能之间的耦合(Coupling between public functions)

编程入门 行业动态 更新时间:2024-10-07 09:26:22
公共职能之间的耦合(Coupling between public functions)

可以说,我有一个叫做do3()的函数为了使这个函数起作用,我需要执行函数do1()和do2()。

但是,do1()和do2()也可能需要其他的东西(可能为do4())

所有这些功能都是公开的(并且必须是公开的)。

问题,我应该如何实现代码?

选项1 :

function do3() { do2() do whatever is needed for do3 } function do2() { do1() do whatever is needed for do2 } function do1() { do whatever is needed for do1 }

所以如果我打电话给do3(),我相信一切都会完成,尽管耦合会出现

选项2

function do3() { do whatever is needed for do3 } function do2() { do whatever is needed for do2 } function do2() { do whatever is needed for do1 }

所以当我想打电话给do3()我必须

do1() do2() do3()

我觉得第二种选择更好,因为它没有耦合,但是我不能真正解释为什么,它更像是一种感觉。 我认为,如果我使用选项一,一天我更改do2()我可能有问题。

但是,使用选项2时,我必须确保每次使用do3时都要调用do1和do2。

如果有人有一个更好的主意(选项3)会很好。

谢谢

Lets say I have a function called do3() In order for that function to work I need the functions do1() and do2() be executed.

However, do1() and do2() could also be needed for other stuff (maybe for do4())

All these functions are public (and must be public).

Question, how should i implement the code?

Option 1 :

function do3() { do2() do whatever is needed for do3 } function do2() { do1() do whatever is needed for do2 } function do1() { do whatever is needed for do1 }

So if i call do3(), i am sure that everything will be done, although coupling will appear

Option 2

function do3() { do whatever is needed for do3 } function do2() { do whatever is needed for do2 } function do2() { do whatever is needed for do1 }

So when i want to call do3() i have to

do1() do2() do3()

I feel that the second option is better as it has less coupling, however I cannot really explain why, it is more like a feeling. I think that if I use option one and one day I change do2() I may have problems.

With option 2 however I must be sure to call do1 and do2 every time i want to use do3.

If someone has a better idea (option 3?) would be great to.

Thanks

最满意答案

“可以说我有一个名为do3()的函数为了使这个函数正常工作,我需要执行函数do1()和do2()。”

胡安:根据你的描述,do3()依赖于do1()和do2()。 依赖关系图是

- ->do2() do3() - ->do1()

在这种情况下,你应该采取第二种方法。

如果您的依赖关系图是:

do3()- ->do2() - -> do1()

do3取决于do2

do2取决于do1

在这种情况下,你应该采取第一种方法。

--> : shows the dependency.

"Lets say I have a function called do3() In order for that function to work I need the functions do1() and do2() be executed. "

Juan: As per your description do3() is dependent on do1() and do2(). The dependency Graph is

- ->do2() do3() - ->do1()

In this case you should go for the second approach.

If your dependency graph is:

do3()- ->do2() - -> do1()

ie

do3 depends on do2

do2 depends on do1

In this case you should go for first approach.

--> : shows the dependency.

更多推荐

本文发布于:2023-07-09 10:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:职能   Coupling   functions   public

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!