Bridge accounting

To some extent, I understand the bridge pattern. I understand the interface decoupling and implementation. He uses a class >, such as a plugin that contains the actual implementation logic of a derived class.

But can someone explain how it allows the interface and output it yourself? If I want to add a new method to the interface, it must be implemented in a derived class that will change it.

Secondly, the client code must be changed in order to install a new developer when a new object is needed.

+7
source share
3 answers

Yes, the goal of the Patter bridge is to separate the abstraction (i.e. the interface) from the implementation so that they change independently. In practice, the idea is to use two separate hierarchies instead of a classic single hierarchy.

Make an example. Suppose you have an abstraction of Window, and you need to create a subclass of IconWindow that specializes Window for each supported platform.

With a single hierarchy, you get:

Window |--------------... IconWindow | -----------------------------------------------... | | | XIconWindow MSIconWindow OSXIconWindow 

This structure is very inconvenient because:

  • if you want to add a new subclass that specializes in Window (e.g. BitmapWindow), you must create a subclass for each supported platform (i.e. three subclasses in this example).

  • If you want to add a supported platform, you must add a new subclass for each existing specialization.

Therefore, it is better to separate the two hierarchies with:

  imp Window--------------------------> WindowImp | | -----------.... --------------------------------- | | | | IconWindow XWindowImp MSWindowImp OSXWindowImp 

Window and WindowImp are interfaces. IconWindow uses the methods provided by Window. The window, in turn, calls the associated method on imp.

The relationship between Window and WindowImp is called Bridge .

Example: IconWindow :: DrawBorder () uses Window :: DrawRect (), which calls imp-> DevDrawLine (), which is declared in WindowImp and defined in specific subclasses (for example, in the XWindowImp class).

My suggestion is to read the book: Design Patterns - Elements of Reusable Object-Oriented Software "( http://en.wikipedia.org/wiki/Design_Patterns ), which contains the example above.

+19
source

I think the Java JDBC API is the best example illustrating the user of a bridge design pattern. The client uses the abstraction (JDBC API) without worrying about the implementation (MYSQL, Oracle, etc.) that various SQL providers provide.

To illustrate further, you have an EmployeeDAOImpl that implements the EmployeeDAO interface using save, update, and delete methods. Now this EmployeeDAOImpl will use the JDBC API (Abstraction) to perform CRUD operations without worrying about the database used. The only thing EmployeeDAOImpl needs is a URL to download the driver.

Now the bottom line is that the hierarchy of EmployeeDAOImpl and other DAOs can vary independently. They don’t need to worry about implementing abstractions of the JDBI APIs.

+2
source

Bridge:

  • Discard the abstraction from its implementation so that they can vary independently of each other;

  • Structural patterns;

  • A bridge pattern is a design pattern used in software development that is designed to "separate the abstraction from its implementation so that they can be independent of each other." A bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes;

  • When a class changes frequently, the functions of object-oriented programming become very useful, since changes to the program code can be easily done with minimal prior knowledge of the program. A bridge pattern is useful when both the class and what it does often change. The class itself can be seen as an implementation and what the class can do as an abstraction. The bridge pattern can also be considered as two layers of abstraction:

  • The bridge pattern is often mixed with the adapter pattern. In fact, the bridge pattern is often implemented using the class adapter pattern;

  • Option: an implementation can be further separated by deferring the presence of the implementation to the point at which abstraction is used.

0
source

All Articles