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

查看:24
本文介绍了桥接模式是否将抽象与实现分离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从不同的文章中学习了桥接模式,并根据我的理解实现了它.让我困惑的一件事是桥接模式说

BridgePattern 将抽象与其实现分离,以便两者可以独立变化

这句话是什么意思?实现是否驻留在单独的 jar 中?

不同的独立声明是什么意思?

考虑提供的

关键说明: 两个正交的类层次结构(抽象层次结构y 和实现层次结构)使用组合(而不是继承).这种组合有助于两个层次结构独立变化.

实现从不引用抽象.抽象包含实现接口作为成员(通过组合).

回到关于 journaldev<中的示例代码的问题/a> 文章:

形状是抽象

三角形是重新定义抽象

颜色是实现者

RedColor 是 ConcreteImplementor

一个具体的Shape 对象:Triangle 扩展了Shape,但没有实现Color 接口.

public class Triangle extends Shape{}

RedColorGreenColor 实际上实现了 Color 接口.

具体形状对象 (Triangle) 独立于实现抽象(即 Color 接口).

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

此处三角形包含一个具体的颜色对象(组合).如果Color抽象(接口)发生变化,RedColorGreenColor负责实现Color的抽象强>界面.

ShapesTriangle 不会受到 Color 界面契约变化的影响.所以 Color 界面可以独立变化.这是可能的,因为Shape 持有使用组合而不是实现的合同.

总而言之,

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

在以下情况下使用桥接模式:

  1. 您想要实现的运行时绑定,
  2. 您有来自耦合接口和众多实现的大量类,
  3. 您想在多个对象之间共享一个实现,
  4. 您需要映射正交的类层次结构.

有用的链接:

tutorialspoint 文章

dzone 文章

oodesign 文章

sourcemaking 文章

相关帖子:

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

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)

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?

这篇关于桥接模式是否将抽象与实现分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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