Pergunta

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

Foi útil?

Solução

This should help

The Bridge Separates an object’s interface from its implementation

The Factory Method - Creates an instance of several derived classes

Outras dicas

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.

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.

Licenciado em: CC-BY-SA com atribuição
scroll top