抽象工厂与桥接模式
我刚刚了解了Bridge
模式及其意图:将抽象与其实现分离,以便两者可以独立变化。
但为什么 AbstractFactory
不能做同样的事情呢?
我知道 AbstractFactory
可以创建一个特定的桥,但我的问题涉及使用 AbstractFactory
而不是 Bridge
来解耦抽象和实现。
您能解释一下 AbstractFactory
和 Bridge
模式之间的真正区别吗?
I have just learned the Bridge
Pattern and its intent : Decouple an abstraction from its implementation so that the two can vary independently.
But why couldn't just an AbstractFactory
do the same thing ?
I know that an AbstractFactory
can create a particular bridge but my question concerns usage of AbstractFactory
instead Bridge
for decoupling Abstraction and Implementation.
Could you please explain me the real difference between AbstractFactory
and Bridge
Pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,从我读到的内容来看,桥梁模式更多地适用于班级及其所做的事情经常变化的情况。类本身可以被认为是实现,类的行为可以被认为是抽象。
另一方面,抽象工厂提供了一个用于创建相关或依赖对象组的接口,而无需指定它们的具体类;他们的实施问题。
所以我想总结一下,你正在将苹果与橙子进行比较,也许这就是混乱的根源。它们是为了解决不同的问题。
对我来说,操作意味着java中的方法,因此操作是由抽象定义或声明的,但在类本身中实现。所以,是的,抽象只是声明操作可以做什么,但实际的实现是在类中完成的。另外,抽象工厂也是正确的。
我想桥的定义部分是它可以有一组与一个抽象不同的抽象。
设计模式使用抽象一词来指代依赖于一组抽象操作的类,其中该组抽象操作的多种实现是可能的。
有关详细信息,请参阅以下链接:
在 Java 中使用抽象和桥接模式
维基百科:Bridge_Pattern
Java 中的桥接模式
桥接模式设计模式
First off the bridge pattern from what I've read is more for when both the class and what it does varies often. The class itself can be considered as the implementation and the behavior of the class as the abstraction.
The Abstract Factory on the other hand provides an interface for creating groups of related or dependent objects, without specifying their concrete classes; their implementation concerns.
So I guess to sum it up, you are comparing apples to oranges and maybe that is where the confusion is coming from. They are for solving different problems.
To me operations imply methods in java, so the operations are defined or declared by the abstraction, but are implemented in the class itself. So yes the abstraction is just declaring what the operations could do as far as behavior, but the actual implementations are done in the class. Also, the Abstract Factory is correct as well.
I guess the defining part for bridge is that it could have sets of abstractions that vary versus one abstraction.
Design Patterns uses the word abstraction to refer to a class that relies on a set of abstract operations, where several implementations of the set of abstract operations are possible.
See these links for more information:
Using Abstractions and the Bridge Pattern in Java
Wikipedia: Bridge_Pattern
Bridge Pattern in Java
The Bridge Pattern Design Pattern
是的。他们很相似。 AF 用于创建对象族。而 Bridge 更多的是关于行为,并且还允许算法和平台之间的松散耦合。
例如:假设我们开发一个使用Ping和Traceroute进行网络诊断的程序,不同平台上的命令是不同的。抽象工厂可用于获取任何平台的 ping 或 Traceroute 实例。
事情并没有就此结束。 Bridge 允许我们使用 AF 返回的 ping 和跟踪命令开发更高级别的算法。根据平台的不同,可以使用不同的 ping 和 Traceroute 序列的算法可以使用桥接模式从平台特定的实现细节中抽象出来。
Yes. They are similar. AF is used to create families of objects. Where as Bridge is more about behavior and also allows loose coupling between algorithm and platform.
eg: Suppose we develop a program for network diagnostics using Ping and Traceroute, the commands on different platform are different. Abstract factory can be used to get an instance of ping or traceroute for any of the platforms.
It does not end there. Bridge allows us to develop higher level algorithm using those ping and trace commands returned by AF. The algorithm which can use a different sequence of ping and traceroute, depending on the platform,can be abstracted from the platform specific implementation details using bridge pattern.
两者之间几乎没有区别,我们无法比较它们。
抽象工厂是创建设计模式,它处理对象创建。 Bridge 是结构设计模式,它处理类结构和组合。
在Bridge中,抽象和实现将独立变化。但在抽象工厂中,如果你改变抽象(接口),你就必须改变客户端。
抽象工厂用例:
时桥接模式:允许抽象和实现独立更改
在以下情况下使用它:
相关 SE 问题:
桥接模式是否将抽象与实现解耦?
之间的基本区别是什么工厂和抽象工厂设计模式?
There are few differences and we can't compare both of them.
Abstract Factory is creational design pattern, which deals with object creation. Bridge is structural design pattern, which deals with class structure and composition.
In Bridge, abstraction and implementation will vary independently. But in abstract factory, if you change abstraction ( interface), you have to change client.
Abstract Factory Use case:
Bridge pattern: allows abstraction and implementation to change independently
Use it when :
Related SE questions:
Does the Bridge Pattern decouples an abstraction from implementation?
What is the basic difference between the Factory and Abstract Factory Design Patterns?