gpt4 book ai didi

c# - 桥梁模式与策略模式

转载 作者:可可西里 更新时间:2023-11-01 09:12:46 28 4
gpt4 key购买 nike

我知道,这个问题被问过很多次了,但我做了一些研究,但还是没弄明白,也许你能帮我解决这个问题:正如多次声明的那样,UML 几乎是相同的。此外,实现和想法或多或少是相同的:您定义一个接口(interface),而不是子类型,它封装了一些逻辑并让它传递给抽象。所以,即使是 Microsoft 博客专家

https://blogs.msdn.microsoft.com/gyanjadal/2015/01/05/difference-between-strategy-and-bridge-patterns/说:

The simple answer is “They are similar but different”. The implementations are similar but the intentions are different. To give an analogy, a city bus and school bus are both similar vehicles, but they are used for different purposes. One is used to transport people between various parts of the city as a commuter service. The other is used for transporting kids to schools.

“如果它听起来像一只鸭子,看起来像一只鸭子,但它打算成为一只天鹅,那么它可以是它们中的任何一个”,这是我在这里读到的。

因为还是没看懂,所以就深挖一下:

https://social.msdn.microsoft.com/Forums/en-US/08775d39-2de0-4598-8872-df21f681b7b3/strategy-vs-bridge-patterns?forum=architecturegeneral

此线程也没有添加任何新内容,除了:

They both look the same on the surface to me as well. The main difference I see is the fact that in the Bridge pattern, the abstraction is PART OF the object, but in the Strategy pattern the abstraction is performed BY the object.

但是,如果我们阅读策略的定义:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

没有定义策略是如何应用的。它也可以很容易地成为抽象的接口(interface),与 LINQ-Orderby 等常见的策略实现完全相同。

关于该主题的另一个兴趣点在这里:

http://game-engineering.blogspot.ch/2008/07/bridge-pattern-vs-strategy-pattern.html

本次类(class)的主要部分:

You say "Strategy" when you want to vary behavior, and you do so not by writing different objects but by introducing a class heirarchy. You say "Bridge" when you expect that you will vary both the interface and the implementation. In both cases you're providing flexibility for a changing implementation; in a Bridge, you're also expecting the interface to change.

这可能是主要区别吗?由于实现者和抽象是如此松耦合,我可以更改实现者的接口(interface)而抽象不必关心?这听起来很合理,但是因为它们以某种方式连接,所以抽象也不会改变吗?这不会破坏信息隐藏和 DRY 等所有其他原则吗?

我还查看了很多示例,为了位置的原因我没有在此处添加这些示例,而且我找不到其中任何一种模式的示例,我无法更改以适应另一种模式。无论是通过接口(interface)属性还是仅通过参数。

我在这里错过了什么吗?可能有人有“我想使用 Strategy,但 Bridge 确实更适合”的真实示例,或者反之亦然,例如?

编辑:为什么我要为这个主题(再次)证明一个自己的线程?首先,上述线程的公认答案如下

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.

我在前面的解释中已经提供了,从外部源抽象行为正是 Strategy- and Bridge-Pattern 的定义。

还有

and you're using the bridge pattern when you use the same constructs to make your code a bit neater.

策略模式还使代码更整洁,因为它抽象了整个构建 block ,从而大大加强了代码。

我认为任何阅读了整个主题的人都会看到,除了这两句话之外,这个主题还有更多内容。

最佳答案

桥接模式的维基百科 UML 图:

Wikipedia UML Diagram for Bridge查看我在链接问题中的回答以了解基本差异:

What is the difference between the bridge pattern and the strategy pattern?

主要区别:抽象和实现可以独立更改

关于您的其他查询:

Is this probably the main-difference? Since the Implementor and the Abstraction are so loose coupled, I can change the Interface of the Implementor and the Abstraction doesn't have to care? That sounds reasonable, but wouldn't then have the Abstraction to change as well, since they are kindahow connected?

看看下面的代码示例@

When do you use the Bridge Pattern? How is it different from Adapter pattern?

尽管该示例是用 java 编写的,但对于 c# 开发人员来说也很容易理解。

在链接示例中:

Vehicle            : Abstraction
Car : Re-defined Abstraction
Truck : Re-defined Abstraction
Implementor : GearShifter
ConcreteImplementor: ManualGearShifter
ConcreteImplementor: AutoGearShifter

主题演讲:

  1. 现在 VehicleGearShifter 可以独立改变。

  2. 如果 Vehicle 发生变化,则只需更改 CarTruck

  3. 如果 GearShifter 改变,只有 ManualGearShifterAutoGearShifter 需要改变。

  4. 由于 Vehicle(抽象)通过组合包含 GearShifter(实现),因此 GearShifter 的更改不会影响 Vehicle

  5. 由于GearShifter(实现者)不包含或引用Vehicle(抽象),抽象的变化不会影响实现。

编辑:

桥接模式呈现两个正交的类层次结构 - 一个用于抽象,一个用于实现,它们可以独立更改而不依赖于其他。

关于c# - 桥梁模式与策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814624/

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