4

i am learning software design pattern and want to know the difference between bridge and decorator pattern

 interface Iconcept
    {
        void action();
    }
    class concept : Iconcept
    {
        public void action()
        {
            Console.WriteLine("walking ");
        }
    }
    class decoratorA : Iconcept
    {
        Iconcept concept;
        public decoratorA(Iconcept concept)
        {
            this.concept = concept;
        }
        public void action()
        {
            concept.action();
            Console.WriteLine("with his dog");
        }
    }
    class decoratorB : Iconcept
    {
        Iconcept concept;
        public decoratorB(Iconcept concept)
        {
            this.concept = concept;
        }
        public void action()
        {
            concept.action();
            Console.WriteLine("in the rain");
        }
    }
    class client
    {
        static void Main()
        {
            Iconcept concept = new concept();
            concept.action();

            new decoratorA(concept).action();
            new decoratorB(concept).action();


            Console.ReadLine();
        }
    }

decorator pattern

definition:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

nowif we take this code for the decorator pattern, i have an implementation by the name concept and i am extending/decorating the implementation with new implementation class name as decoratorA and decoratorB

bridge pattern

definition: Decouple an abstraction from its implementation so that the two can vary independently.

i have a old implementation by the name concept and without doing any changes in the old implementation i am implementing a new implementation.

so what differentiates the two pattern

Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • Possible duplicate of [Simplified difference between Bridge and Decorator pattern](https://stackoverflow.com/questions/48355285/simplified-difference-between-bridge-and-decorator-pattern) – Gonzalo Jan 20 '18 at 11:57
  • similar: https://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adapter-and-bridge-patterns-differ – Julio Marins Apr 22 '18 at 01:39

4 Answers4

1

Bridge you decouple abstraction from implementation by wrapping the object implemented with a class that doesn't necessarily share the same interface of the object.

Decorator modify existing behaviour by wrapping the object implemented with a class that inherits the same interface as the object implemented, so it necessarily has the same behaviours.

Julio Marins
  • 10,039
  • 8
  • 48
  • 54
1

Bridge will have two different type hierarchies. One for the super class abstraction and the other for the implementation. The super class abstraction has a reference to the implementation.

On the other hand, decorator structure will have one common type at the top. The decorated object and decorator object will share the same super type. The decorator will have a reference to the decorated object.

In both cases client will pass the Actual object(Implementation object for Bridge and the to be decorated object) to the bridge and the decorator objects.

RCT
  • 1,044
  • 1
  • 5
  • 12
0

The Decorator and Bridge pattern both use Composition. IMHO the difference can be found in the way the client code uses these patterns.

The Bridge pattern injects the implementation into the abstraction. Then it invokes methods from the Abstraction.

$implementation = new MyImplementationA;
$abstraction = new AbstractionParent($implementation);
$abstraction->doSomething();

$implementation = new MyImplementationB;
$abstraction = new AbstractionChild($implementation);
$abstraction->doSomething();

The Decorator patterns stacks Objects like this:

$helloWorldBase = new HelloWorldBase();
$helloWorldDecoEN = new HelloWorlDecoratorEN($helloWorldBase);
$helloWorldDecoNL = new HelloWorlDecoratorNL($helloWorldDecoEN);
$helloWorldDecoPL = new HelloWorlDecoratorPL($helloWorldDecoNL);
$decorator = new HelloWorldBaseDecorator($helloWorldDecoPL);
echo $decorator->sayHello();
Julian
  • 4,396
  • 5
  • 39
  • 51
-1

you should read this pages Bridge and Decorator, there are great content about design patterns, I don't like some code examples they have but intent, problems and structure are well explained.

Both patterns try to simplified complex inherit class structures, so they can look similar in intent, but the problem they try to solve and the way they do is different.

Decorator tries to abstract some optional behaviour from an object, so you don't have to extend the core class so many times, a typical example are UI components. You can have a TextView, a TextView with border, a TextView with border and shadow, etc. In that post there is a similar example with Windows. So the solution is to make the abstract Decorator extend from the same interface of the core element and it will have a reference to the core element. Code to create a textview could be like this:

UIElement textview = new BorderTextView(new ShadowTextView(new TextView()));

Decorator pattern from: sourcemaking.com

Bridge tries to decouple an abstraction from its implementation, and make both independet to vary when needed.

Bridge is a synonym for the "handle/body" idiom. This is a design mechanism that encapsulates an implementation class inside of an interface class. The former is the body, and the latter is the handle. The handle is viewed by the user as the actual class, but the work is done in the body. "The handle/body class idiom may be used to decompose a complex abstraction into smaller, more manageable classes. The idiom may reflect the sharing of a single resource by multiple classes that control access to it (e.g. reference counting)."

In this case the "handle" has a reference to the "body" but "body" doesn't know about his "handle", not like Decorator where the abstraction of behaviour has a reference to the parent abstraction, but the concrete implementation (our textview core in the example) doesn't know about decorators.

Bridge pattern from: sourcemaking.com

Gonzalo
  • 1,781
  • 15
  • 29