桥接模式和策略模式有什么区别? [英] What is the difference between the bridge pattern and the strategy pattern?

查看:44
本文介绍了桥接模式和策略模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读关于 dofactory、维基百科和许多网站的许多文章.我不知道桥接模式和策略模式之间的区别.

I tried to read many articles on dofactory, wikipedia and many sites. I have no idea on differences between bridge pattern and the strategy pattern.

我知道它们都将抽象与其实现分离,并且可以在运行时更改实现.

I know both of them decouple an abstraction from its implementation and can change implementation at run time.

但我还是不知道在什么情况下应该使用策略,在什么情况下应该使用桥牌.

But I still don't know in which situation I should use strategy or in which situation I should use bridge.

推荐答案

语义.来自维基百科:

策略的 UML 类图模式与图表相同桥模式.然而,这两设计模式在他们的意图.虽然战略模式是为了行为,桥接模式是为了结构.

The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

上下文和策略比抽象和之间的耦合在 Bridge 中的实现模式.

The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

据我所知,当您抽象可以从外部源提供的行为(例如,配置可以指定加载一些插件程序集)时,您正在使用策略模式,并且您正在使用桥接模式当您使用相同的结构使您的代码更整洁时.实际代码看起来非常相似 - 您只是出于稍微不同的原因应用这些模式.

As I understand it, you're using the strategy pattern when you're abstracting behavior that could be provided from an external source (eg. config could specify to load some plugin assembly), and you're using the bridge pattern when you use the same constructs to make your code a bit neater. The actual code will look very similar - you're just applying the patterns for slightly different reasons.

这篇关于桥接模式和策略模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆