java设计模式工厂模式_Java中的桥梁设计模式

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 - implementor
	protected Color color;
	
	//constructor with implementor as input argument
	public 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);
	}

	@Override
	public 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);
	}

	@Override
	public 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设计模式工厂模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值