Saturday, November 13, 2010

..........Bridge Pattern[SD]

The Bridge pattern is used to separate the interface of class from its implementation, so that either can be varied separately.

suppose we have an interface

public interface ISwitch
{
public void SwitchOn();

public void SwitchOff();
}

And we have class CFan

public class CFan : ISwitch
{
public void SwitchOn()
{
System.Console.Writeline(“Switch On”);
}

public void SwitchOff()
{
System.Console.Writeline(“Switch Off”);
}
}

Suppose two classes are derived from this interface Bulb and Fan.

In future if we require to add one or more function in Switch for Bulb than we have to add that function in Fan also But we don’t want it, This problem can be solved by converting this interface into abstract class. But this decision should be made earlier to implementation whether the interface should be interface or abstract class.


Difference Between Adoptor and Bridge
The adaptor is a thing that sits between the two interfaces. In a bridge, there isn't a thing that sits between two interfaces. The interfaces connect directly in a Bridge. It's a tight coupling. No adaptors sitting between the two halves.

A bridge is by design. An adaptor is not. An adaptor is a patch. A bridge is put in place on purpose.

An adaptor is just something for working one interface to fit another interface. The two interfaces "mean" the same thing, (or roughly the same thing,) they just do it in a different shape. So you put an adaptor between them.

No comments:

Followers

Link