Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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 pattern use case:

  1. When your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.

Checklist:

  1. Decide if "platform independence" and creation services are the current source of pain.
  2. Map out a matrix of "platforms" versus "products".
  3. Define a factory interface that consists of a factory method per product.
  4. Define a factory derived class for each platform that encapsulates all references to the new operator.
  5. The client should retire all references to new, and use the factory methods to create the product objects.

bridge pattern:

Use it 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.

Related SE questions:

Does the Bridge Pattern decouples an abstraction from implementation?

What is the basic difference between the Factory and Abstract Factory Patterns?

Have a look at this journaldev article and sourcemaking article for in-depth understanding of various design patterns.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top