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

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

问题描述

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



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



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



什么意思是独立声明变化?



考虑到提供的



键注: 两个正交的类层次结构使用组合(而不是继承)链接抽象层次结构 y和实现层次结构)。此组合可帮助层次结构独立变化



实现永远不会引用抽象。抽象包含实现接口作为成员(通过组合)。



回到您关于 journaldev 文章:



形状是抽象



三角形是重新定义抽象



颜色是实施者



RedColor是 ConcreteImplementor



具体形状对象:三角形扩展形状,但不实现 / em>界面。

  public class Triangle extends Shape {
}

RedColor GreenColor 实际上实现了颜色界面。



Concrete Shape对象( Triangle )独立于实现抽象(即颜色界面)。 >

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

这里三角形包含具体的 strong> object( Composition )。如果颜色抽象(界面)中有变化,则 RedColor GreenColor 负责实现 Color 界面



形状三角形不受合约更改为颜色界面。因此,颜色界面可以独立变化。这是可能的,因为 Shape 持有使用组合而不是实施的合约。



总结


  1. 桥是一种结构模式

  2. 抽象并且执行在编译时不受约束

  3. 抽象和实现 - 两者都可以在客户端不同的影响下变化

中使用Bridge模式


  1. 想要运行时实现的绑定

  2. 你从一个耦合的接口和许多实现中增加了类,

  3. 你想在多个对象之间共享一个实现

  4. 您需要映射正交类层次结构。

有用的链接:



tutorialspoint artice



dzone 文章



oodesign 文章



来源制作文章



参考这个 Bridge pattern文档链接为真实世界的例子。


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

Refer to this Bridge pattern Documentation link for real world example.

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

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