桥接模式是否将抽象与实现解耦?[英] Does the Bridge Pattern decouples an abstraction from implementation?

本文是小编为大家收集整理的关于桥接模式是否将抽象与实现解耦?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我从不同文章中学到了桥梁模式,并根据我的理解实施了这一点.让我感到困惑的一件事是桥梁模式说

bridgepattern将其实现的抽象分离出来,以便两者可以独立变化

此陈述的含义是什么?实现是否存在于单独的jar?

不同独立陈述的含义是什么?

考虑提供的 journaldev 文章,详细说明答案.

任何帮助都非常感谢.

推荐答案

bridgepattern 将其实现的抽象脱离.

抽象和实现可以独立变化,因为混凝土类未直接实现抽象(接口)

来自Wikipedia的UML图

关键注意: 两个正交类层次结构(抽象层次 y和实现层次结构)组成(而不是继承).此组成有助于两个层次结构独立变化.

实施永远不要引用抽象.抽象包含实现接口作为成员(通过组成).

回到有关示例代码的问题, journal degres/a>文章:

形状是抽象

三角形是 redewinedabstraction

颜色是实施者

redcolor是 concreteimplementor

混凝土 shape 对象: triangle 扩展了 shape ,但不能实现 color 接口.

public class Triangle extends Shape{
}

redcolor 和 greencolor 实际实现了 color 接口.

混凝土形状对象( triangle )独立于实现抽象(即 color 接口).

Shape tri = new Triangle(new RedColor());

在这里三角形包含一个具体的颜色对象(组成).如果颜色抽象发生变化(界面), redcolor 和 greencolor 负责实施颜色的抽象接口.

形状喜欢 三角形不受与 color 接口的合同变化的影响.因此,颜色接口可以独立变化.这是可能的,因为 shape 持有使用组成而不是实施的合同.

.

总结,

  1. 桥是一种结构模式
  2. 抽象和实施在编译时不绑定
  3. 抽象和实施 - 两者都可以在不影响客户的情况下变化

使用桥梁模式时:

  1. 您想要实现的运行时绑定,
  2. 您从耦合接口和众多实现的类别中泛滥,
  3. 您想在多个对象之间共享实现,
  4. 您需要映射正交类层次结构.

有用的链接:

TutorialSpoint ArticeSpoint Artice

dzone 文章

oodesign 文章

sourcemaking 文章

相关文章:

您什么时候使用桥梁图案?它与适配器模式有何不同?

其他推荐答案

此语句仅意味着您可以切换实施者,而抽象指向运行时,一切都应该起作用(例如在策略模式中;但是在策略模式中,只有策略是抽象的). 它也可以理解为分开两个类别,因此他们不必彼此了解不仅仅是界面.

其他推荐答案

对我来说,桥梁并不是GOF圣经中最重要的DP,因为它主要是策略的导数.由于其他一些未陈年的模式(工厂方法?),这意味着与其他模式相比,抽象类行为的继承更多,因此通常不适用.

这主要是从事大型工作的策略,但是战略的主要问题是该策略通常需要有关其背景的知识.

在某些语言中,这导致策略被宣布为上下文的朋友,或者定义为Java内部类别的策略.

这意味着上下文通常以对各种具体策略的存在的了解.您可以通过使用setStrategy()函数来避免这种情况,但是由于效率原因,从具体策略到上下文的反向依赖性通常可以生存(您想直接操纵上下文的数据结构).

>

这个问题是由桥解决的,因为策略的背景现在是抽象的,但仍然是A类先验,因为它至少具有策略代码.它通常应该定义足以与孔(即抽象方法)一起使用的具体策略的访问API.您将AbstractContext的出现在Appractstragey上的操作的签名中,您很好.

因此,在我看来,桥梁通过使上下文具体的具体以使策略运行的策略来完成策略,但仍然足够抽象,以至于可以正交精炼W.R.T.具体策略(在实施具体策略实际使用的上下文的抽象API时具有反馈效应).

看到桥梁的一种更简单的方法是说AppractStrategy操作应始终将抽象作为参数,而不是真正了解其上下文.

更精确地回答OP问题:

此陈述的含义是什么?实现是否存在于单独的jar?

是的,实际上,通常您可以在软件包" base"中定义抽象和实施者(TEY可能是接口).混凝土实施者每个人都可以驻留在软件包" Implacx"中.混凝土上下文可以驻留在单独的软件包" contxx"中.依赖关系图中没有循环,每个人都取决于基础,可以独立定义新的" contxx"和" inmlypxx"(它们之间根本没有依赖关系),因此在OP中进行了粗体语句.

不同独立语句的含义是什么?

想想Eclipse中的编辑插件;它必须处理按钮和点击(例如策略)上的操作,但是该策略需要采取的实际操作是对编辑状态本身(例如"突出显示文本").您可以以抽象的方式定义编辑器所拥有的内容,包括它具有用于文书和关键的处理程序以及突出显示和导航功能的事实,即使这些功能也可以被混凝土编辑器(Flash而不是突出显示)覆盖.那是一座桥,您可以独立定义新的编辑和新处理程序.

有了一些依赖注入(例如Google Guice)或一些手动出厂代码或组件取向,可以从外部进行清洁Setstrategy,您可以在应用程序的各个部分获得非常低的耦合.

考虑提供的JournalDev文章,详细说明了答案.

老实说,我认为这不是DP的最佳应用,因为颜色实现似乎并不在乎他们的背景.您应该在这里使用装饰器,因为颜色是形状的独立问题.

可以看一下这些幻灯片,用于使用装饰器的解决方案(部分是法语,对不起). https ://www-licence.ufr-info-p6.jussieu.fr/lmd/licence/2015/ue/3i002-2016fev/cours/cours/cours-9.pdf (基于示例16-18)这里 : https ://www-licence.ufr-info-p6.jussieu.fr/lmd/licence/2015/ue/3i002-2016fev/cours/cours/cours-4.pdf 幻灯片10至15.

在该示例上,如果" UpdateInertie"是Forme的成员,我们将需要桥梁,这听起来并不荒谬.

,再次出现了桥梁的组合.

本文地址:https://www.itbaoku.cn/post/627689.html

问题描述

I learned Bridge pattern from different articles and I have implemented that as per my understanding . One thing that is confusing me is bridge pattern says

BridgePattern decouples an abstraction from its implementation so that the two can vary independently

what is meaning of this statement? Is implementation resides at in separate jar ?

what is meaning of vary independently statement ?

considering the provided journaldev article, elaborate the answer.

Any help is greatly appreciated.

推荐答案

BridgePattern decouples an abstraction from its implementation.

Abstraction and Implementation can vary independently since the concrete class does not directly implement Abstraction ( interface)

UML Diagram from Wikipedia

Key note: Two orthogonal class hierarchies (The Abstraction hierarchy and Implementation hierarchy) are linked using composition (and not inheritance).This composition helps both hierarchies to vary independently.

Implementation never refers Abstraction. Abstraction contains Implementation interface as a member (through composition).

Coming back to your question regarding the example code in journaldev article :

Shape is Abstraction

Triangle is RedefinedAbstraction

Color is Implementor

RedColor is ConcreteImplementor

A concrete Shape object : Triangle extends Shape but does not implement the Color interface.

public class Triangle extends Shape{
}

RedColor and GreenColor actually implement the Color interface.

The Concrete Shape object (Triangle) is independent of implementing abstraction (i.e. Color interface).

Shape tri = new Triangle(new RedColor());

Here Triangle contains a concrete Color object ( Composition). If there is a change in the Color abstraction (interface), RedColor and GreenColor are responsible for implementing the abstraction of Color interface.

Shapes like Triangle is not affected by changes in contract to the Color interface. So the Color interface can vary independently. This is possible because Shape holds the contract that uses Composition rather than implementation.

In Summary,

  1. Bridge is a structural pattern
  2. Abstraction and implementation are not bound at compile time
  3. Abstraction and implementation - both can vary without impact in client

Use the Bridge pattern when:

  1. You want run-time binding of the implementation,
  2. You have a proliferation of classes from a coupled interface and numerous implementations,
  3. You want to share an implementation among multiple objects,
  4. You need to map orthogonal class hierarchies.

Useful links:

tutorialspoint artice

dzone article

oodesign article

sourcemaking article

Related post:

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

其他推荐答案

This statement simply means, that you can switch implementor, to which abstraction points to, in run-time and everything should work (like in Strategy Pattern; but in Strategy Pattern, only strategies are abstract). It can be also understood as separating two classes, so that they don't have to know about each other more than just their interfaces.

其他推荐答案

For me Bridge is not really the most foremost DP in the GOF bible, since it is mostly a derivative of Strategy. As some other patterns that have not aged so well (factory method?) it implies more inheritance with abstract classes holding behavior than other patterns, hence is less generally applicable.

It's mostly Strategy doing the big work, but a major issue with Strategy is that the strategy often needs knowledge about its context.

In some languages this leads to strategies being declared friend of the context, or strategies defined as internal classes in Java.

This means that the context often ends up with knowledge of existence of the various concrete strategies. You can avoid this by using a setStrategy() function, but the reverse dependency from concrete strategy to context usually survives, due to efficiency reasons (you want to manipulate the context's data structures directly).

This issue is kind of solved by Bridge, as the context of Strategy is now abstract, but still a class a priori, since it has at least the code for Strategy. It should usually define an access API sufficient for the concrete Strategies to work with, possibly with holes i.e. abstract methods. You put an occurrence of AbstractContext in the signature of the operations on AbstractStragey and you're good.

So in my point of view, Bridge completes Strategy by making the Context concrete enough for the strategies to work, but still abstract enough that it can be orthogonally refined w.r.t. concrete strategies (with feedback effects when implementing abstract API of the context that the concrete strategies actually use).

A simpler way of seeing bridge is to say that the AbstractStrategy operations should always take abstractions as parameters rather than really knowing intimately their context.

To answer the OP question more precisely :

what is meaning of this statement? Is implementation resides at in separate jar ?

Yes, indeed, typically you could define the Abstraction and Implementor in a package "base" (tey could be interfaces). The concrete Implementors can each reside in a package "implXX". The concrete context can reside in separate packages "contXX". There are no cycles in the dependency graph, everybody depends on base, new "contXX" and "implXX" can be defined independently (no dependencies at all between them) thus the bold statement in the OP.

what is meaning of vary independently statement ?

Think of an editor plugin in eclipse; it must handle the actions on buttons and clicks (like a strategy), but the actual action the strategy needs to do is act on the editor state itself (e.g. "highlight text"). You define what an editor possesses in an abstract way, including the fact that it has Handler for clics and keypresses as well as highlighting and navigation features, even these can be overriden by concrete editors (flash instead of highlight). That's a bridge, you can define new editors and new handler independently.

With some dependency injection (e.g. Google guice) or some manual factory code or component orientation to cleanly setStrategy from outside you get very low coupling of the various parts of the application.

considering the provided journaldev article, elaborate the answer.

honestly I think this is a not the best application of the DP since the Color implementations don't seem to care much about their context. You should use a Decorator here as Color is an independent concern from Shape.

Have a look a at these slides for a solution with a Decorator (partly in French, sorry). https://www-licence.ufr-info-p6.jussieu.fr/lmd/licence/2015/ue/3I002-2016fev/cours/cours-9.pdf (slides 16-18) based on example introduced here : https://www-licence.ufr-info-p6.jussieu.fr/lmd/licence/2015/ue/3I002-2016fev/cours/cours-4.pdf slides 10 to 15.

On that example, we would need Bridge if "updateInertie" was member of Forme, which does not sound preposterous. Again Bridge emerges more as a combination of other patterns.