开发者

AbstractFactory Versus Bridge Pattern

开发者 https://www.devze.com 2023-04-11 22:05 出处:网络
I have just learned the Bridge Pattern and its intent : Decouple an abstraction from its implementation so that the two can vary independently.

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 Impleme开发者_Go百科ntation.

Could you please explain me the real difference between AbstractFactory and Bridge Pattern?


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


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.


There are few differences and we can't compare both of them.

  1. Abstract Factory is creational design pattern, which deals with object creation. Bridge is structural design pattern, which deals with class structure and composition.

  2. 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:

  1. When your system has to create multiple families of products

Bridge pattern: allows abstraction and implementation to change independently

Use it when :

  1. Run-time binding of the implementation allows abstraction and implementation to vary independently

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号