java设计模式工厂模式

Today we will look into Bridge Design Pattern in java. When we have interface hierarchies in both interfaces as well as implementations, then bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs.

今天,我们将研究Java中的Bridge Design Pattern。 当我们在接口和实现中都具有接口层次结构时,则使用网桥设计模式将接口与实现分离,并在客户端程序中隐藏实现细节。

桥梁设计模式 (Bridge Design Pattern)

Just like Adapter pattern, bridge design pattern is one of the Structural design pattern.

就像Adapter模式一样 ,桥梁设计模式是结构设计模式之一

According to GoF bridge design pattern is:

根据GoF桥的设计模式是:

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

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

The implementation of bridge design pattern follows the notion to prefer Composition over inheritance.

桥梁设计模式的实现遵循相对于继承更倾向于“ 组合”的概念。

Java示例中的桥梁设计模式 (Bridge Design Pattern in Java Example)

If we look into bridge design pattern with example, it will be easy to understand. Lets say we have an interface hierarchy in both interfaces and implementations like below image.

如果我们以示例来研究桥梁设计模式,将很容易理解。 可以说我们在接口和实现中都有一个接口层次结构,如下图所示。

Now we will use bridge design pattern to decouple the interfaces from implementation. UML diagram for the classes and interfaces after applying bridge pattern will look like below image.

现在,我们将使用网桥设计模式将接口与实现分离。 应用桥接模式后,类和接口的UML图将如下图所示。

Notice the bridge between Shape and Color interfaces and use of composition in implementing the bridge pattern.

注意ShapeColor接口之间的桥梁,以及在实现桥梁图案时使用合成的方法。

Here is the java code for Shape and Color interfaces.

这是Shape和Color接口的Java代码。

Color.java

Color.java

package com.journaldev.design.bridge;public interface Color {public void applyColor();
}

Shape.java

Shape.java

package com.journaldev.design.bridge;public abstract class Shape {//Composition - implementorprotected Color color;//constructor with implementor as input argumentpublic Shape(Color c){this.color=c;}abstract public void applyColor();
}

We have Triangle and Pentagon implementation classes as below.

我们有以下Triangle和Pentagon实现类。

Triangle.java

Triangle.java

package com.journaldev.design.bridge;public class Triangle extends Shape{public Triangle(Color c) {super(c);}@Overridepublic void applyColor() {System.out.print("Triangle filled with color ");color.applyColor();} }

Pentagon.java

Pentagon.java

package com.journaldev.design.bridge;public class Pentagon extends Shape{public Pentagon(Color c) {super(c);}@Overridepublic void applyColor() {System.out.print("Pentagon filled with color ");color.applyColor();} }

Here are the implementation classes for RedColor and GreenColor.

这是RedColor和GreenColor的实现类。

RedColor.java

RedColor.java

package com.journaldev.design.bridge;public class RedColor implements Color{public void applyColor(){System.out.println("red.");}
}

GreenColor.java

GreenColor.java

package com.journaldev.design.bridge;public class GreenColor implements Color{public void applyColor(){System.out.println("green.");}
}

Lets test our bridge pattern implementation with a test program.

让我们用一个测试程序来测试我们的桥接模式实现。

BridgePatternTest.java

BridgePatternTest.java

package com.journaldev.design.test;import com.journaldev.design.bridge.GreenColor;
import com.journaldev.design.bridge.Pentagon;
import com.journaldev.design.bridge.RedColor;
import com.journaldev.design.bridge.Shape;
import com.journaldev.design.bridge.Triangle;public class BridgePatternTest {public static void main(String[] args) {Shape tri = new Triangle(new RedColor());tri.applyColor();Shape pent = new Pentagon(new GreenColor());pent.applyColor();}}

Output of above bridge pattern example program is:

上面的桥接模式示例程序的输出为:

Triangle filled with color red.
Pentagon filled with color green.

Bridge design pattern can be used when both abstraction and implementation can have different hierarchies independently and we want to hide the implementation from the client application.

当抽象和实现可以分别具有不同的层次结构并且我们希望对客户端应用程序隐藏实现时,可以使用网桥设计模式。

翻译自: https://www.journaldev.com/1491/bridge-design-pattern-java

java设计模式工厂模式

java设计模式工厂模式_Java中的桥梁设计模式相关推荐

  1. java设计模式工厂模式_Java中的复合设计模式

    java设计模式工厂模式 Composite pattern is one of the Structural design pattern. Composite design pattern is ...

  2. java设计模式工厂模式_Java中的外观设计模式

    java设计模式工厂模式 Facade Design Pattern is one of the Structural design patterns (such as Adapter pattern ...

  3. java设计模式工厂模式_Java中的工厂设计模式

    java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...

  4. java设计模式代理模式_Java中的代理设计模式

    java设计模式代理模式 代理对象或代理对象为另一个对象提供占位符,以控制对该对象的访问. 代理充当原始对象的轻量级版本或简化版本. 它支持与原始对象相同的操作,但可以将那些请求委托给原始对象以实现它 ...

  5. java设计模式 订阅模式_Java中的外观设计模式

    java设计模式 订阅模式 立面是指建筑物的外观. 当穿过街道时,我们所看到的只是建筑物的外观. 该工作面抽象了建筑物的所有复杂实现细节. 同样, 外观设计模式旨在为子系统中的一组接口提供统一的接口. ...

  6. java设计模式 订阅模式_Java中的复合设计模式

    java设计模式 订阅模式 当我们必须使用对象的树状分层结构时,复合设计模式非常有用. 它使我们能够统一对待单个对象和对象组成. 它属于结构设计模式的范畴,因为它将对象组合成树形结构以表示部分整个层次 ...

  7. java设计模式迭代器模式_Java中的迭代器设计模式–示例教程

    java设计模式迭代器模式 迭代器模式是一种行为模式,它用于提供遍历一组对象的标准方式. Iterator模式在Java Collection Framework中得到了广泛使用,其中Iterator ...

  8. java设计模式迭代器模式_Java中的迭代器设计模式

    java设计模式迭代器模式 Iterator design pattern in one of the behavioral pattern. Iterator pattern is used to ...

  9. java简单工厂模式_Java 简单工厂模式

    Java 简单工厂模式 2014-05-28·WeaponX 3717 次浏览 ## 介绍 简单工厂模式又称静态工厂模式. 简单工厂模式由工厂类角色.抽象产品角色和具体产品角色组成. 工厂类角色是本模 ...

最新文章

  1. 微信一次发两个ajax请求,微信浏览器发送ajax请求执行多次解决方法
  2. 缓存初解(四)---Ibatis的缓存配置+Ehcache
  3. down 网卡端口周期性的up_Linux 中如何启用和禁用网卡?
  4. python 正则search 所有_python之路----正则re(search,match,findall……)
  5. [html] 说说你对HTML5中pattern属性的理解
  6. ajax请求url python,ajax请求方式
  7. Ubuntu 13.10 用sogou拼音替换ibus-转
  8. 最小生成树的java实现
  9. 情人节--我们依旧单身(制作属于自己的QQ拼音皮肤)(带全部ps素材)
  10. Fragment、FragmentActivity、Fragment生命周期及Fragment组件穿透
  11. 推送V3 - Vue + Layim + Websocket 实践笔记
  12. vscode 自定义字体样式_vscode md样式自定义
  13. 帮助中国IT企业吃掉更多不会跳舞的大象
  14. Navicat Mysql 破解教程(亲测可用)
  15. Android Kotlin okhttp Retrofit 线程协程那些事
  16. 【ML】英雄联盟对局胜负预测任务
  17. [理财]:如何稳步的实现财务自由?
  18. 【227】基本计算器II--无括号的加减乘除计算器
  19. PySide+PyDesigner出现错误:This application failed to start because no Qt platform plugin could be initia
  20. 曾国藩谕纪鸿(咸丰六年九月二十九夜)- 勤俭自持,习劳习苦

热门文章

  1. Ubuntu 左边栏和顶栏都不见了,ctrl+alt+t 也调用不出terminal
  2. 8.26~8.30-广州软件所-实习工作日记
  3. hdu 1261 字串数
  4. volatile 变量
  5. 判断一个字符串是否在一个数组中
  6. [转载] 算法导论:分治法,python实现合并排序MERGE-SORT
  7. [转载] 如何使用Python 实现秒表功能?
  8. 在consul上注册web服务
  9. 在mysql的操作界面中,如何清屏幕
  10. __construct __destory __call __get __set