5

What is the difference between Bridge and Factory Pattern? both the pattern seems to instanitae the class depending upon the logic from the client side.

Factory Pattern

interface IFactory {
    void GetProduct();
}
class Germany : IFactory {
    public void GetProduct() {
        Console.WriteLine("germany");
    }
}
class Russia : IFactory {
    public void GetProduct() {
        Console.WriteLine("Russia");
    }
}
class Canada : IFactory {
    public void GetProduct() {
        Console.WriteLine("Canada");
    }
}
class Component {
    IFactory factory;
    public Component(IFactory component) {
        this.factory = component;
    }
    public void BuyProduct() {
        factory.GetProduct();
    }
}
class Client {
    static void Main() {
        Component component = new Component(new Canada());
        component.BuyProduct();

        Console.Read();
    }
}

Bridge Pattern

interface IBridge
{
    void print();
}
class ImplementationA : IBridge
{
    public void print()
    {
        Console.WriteLine("implementation A");
    }
}
class ImplementationB : IBridge
{
    public void print()
    {
        Console.WriteLine("implementation A");
    }
}
class Abstraction
{
    IBridge bridge;
    public Abstraction(IBridge bridge)
    {
        this.bridge = bridge;
    }
    public void caller()
    {
        bridge.print();
    }
}
class Client
{
    static void Main()
    {
        new Abstraction(new ImplementationA()).caller();
    }
}

in both Factory and Bridge pattern we can see that the class is instantiated from the client on some logic. Is not both the code doing the same thing. In Factory pattern, clients logic decide which class should be instantiated which is the same case with Bridge Pattern as well.

so what really is the change,please help me understand it

5
  • 1
    Never seen the bridge pattern. It has nothing to do with factories, though(??). Seems identical to an "adapter" or a "facade" to me.
    – usr
    Nov 15, 2016 at 20:25
  • 2
    stackoverflow.com/questions/1425171/… -- in any case, I don't see how factory/bridge are related?
    – AJ X.
    Nov 15, 2016 at 20:26
  • @user2864740 i have updated the question please have a look Nov 15, 2016 at 20:37
  • 1
    An abstract factory will turn into an explosion of concrete implementation classes. A bridge implementation is going to cut down on this by injecting some of the cross cutting behavior.
    – AJ X.
    Nov 15, 2016 at 20:40
  • hard to find out if you are talking about factory, factory method or abstract factory... Aug 17, 2019 at 21:10

3 Answers 3

4

This should help

The Bridge Separates an object’s interface from its implementation

The Factory Method - Creates an instance of several derived classes

3

An (abstract) factory pattern is a creational pattern. It is responsible for creating instances of other objects.

Bridge is a structural pattern. It solves a problem regarding the structure of your code. Bridge is responsible for decoupling an abstraction from an implementation. Bridge allows you to have multiple implementations of an abstraction without having to inherit multiple classes from the same base class. In fact, with Bridge, you have multiple seperate class hierarchies: a class hierarchy which represents the abstraction, and a class hierarchy which represents its implementation.

Bridge and Factory can work together though: You can use a factory to instantiate the (specific) implementation for your bridge.

1

In provided example for Factory pattern, factory class and method is not correct. It must decide which sub class object to instantiate depends on any condition provided by client class while calling factory method. And client class must have IFactory factory=null; not in Component in your case. You changed the method names only, both looks same.

The code for factory class(Component) must be like below:

class Component {        
        public GetComponent(int flag) {
            IFactory factory=null;
            if(flag==1)
              factory = new Germany();
            else if(flag==2)
              factory=new Russia();
            else
              factory=new Canada();
        }            
    }

class Client {
        static void Main() {
            IFactory factory=null;
            factory= new GetComponent(1);
            factory.GetProduct();

            Console.Read();
        }
    }

Difference:

Bridge Pattern: Separates abstraction from its implementation, so both can be modified independently.

Factory Pattern: It is way of creating object but letting subclasses decide which class to instantiate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.