Bridge Mode (post) 2

Source: Internet
Author: User

Iv. Modem Problems

I feel that the example of Bridge pattern in Agile Software Development-principles, models and practices is good. (The encapsulation of changes in chapter 33 of Java and patterns is also well written and recommended.
Everyone reads it. It elaborates on design to interfaces in design patterns explained.
2) favor composition over inheritance. 3) Find What varies and
Encapsulate it .).

There are a large number of modem customer programs using the modem interface. The modem interface is composed of several Derived classes: hayesmodem, usroboticsmodem, and
Earniesmodem implementation. It complies well with OCP, LSP, and dip. When a new type of modem is added, the client program of the modem will not be affected.

It is assumed that this situation lasted for several years, and many modem client programs are using the modem interface. A non-dial modem is now available, called a dedicated modem. Their locations
At both ends of a dedicated connection. There are several new applications that use these dedicated modem, which do not require dialing. We call these users deduser. However, the customer wants all the current Modem
All client programs can use these dedicated modems. They do not want to change many modem customer applications, so they can make some fake calls for these modem customer programs.
(Dummy) phone number.

If possible, we will change the system design as shown in.

We separate the dialing and communication functions into two different interfaces. The original modem implements these two interfaces, and the modem client uses these two interfaces. Deduser only
And dedicatemodem only implements the modem interface. But doing so will require us to change all modem customer programs-this is not allowed by the customer.

One possible solution is to let dedicatedmodem derive from modem and implement the dial and hangup methods as null, as shown below:

A few months later, there were already a large number of dedusers, and the customer made a new change. To enable international phone numbers, credit card phone numbers, pin phone numbers, and so on, you must use char [10] storage numbers in the existing dial to change to phone numbers of any length.

Obviously, all modem client programs must be changed. The customer agreed to change the modem client program because they had no choice. The bad thing is, we have to tell deduser writers that they have to change their code! You can imagine how happy they are to hear this. They didn't need to call dial.

This is the harmful and chaotic dependency that many projects will have. A kludge in a part of the system creates a harmful dependency, which eventually leads to problems in completely unrelated parts of the system.

If you use the adapter mode to solve the initial problem, you can avoid this serious problem.

Note that the pooled object still exists. The adapter still needs to simulate the connection status. However, all dependencies are initiated from the adapter. The collection body is isolated from the system and hidden from almost unknown adapters.

Bridge Mode

There is another way to look at this problem. Now there is another way to split the modem hierarchy. For example:

This is not an ideal structure. Each time a new hardware is added, two new classes must be created-one for the dedicated scenario and the other for the dial-up scenario. Each time a new connection type is added, three new classes must be created, corresponding to three different hardware. If these two degrees of freedom are unstable at all, then a large number of derived classes will appear soon.

The bridge mode is usually useful when the type hierarchy has multiple degrees of freedom. We can separate these hierarchies and combine them through bridges, rather than merging them.

We divide the modem hierarchy into two layers. One represents the connection method, and the other represents the hardware.

This structure is complex but interesting. Its creation does not affect the user of the modem and completely isolates the connection policy and hardware implementation.
Each derived class of modemconnectcontroller represents a new connection policy. You can use sendlmp, cancelmp,
Diallmp and hanglmp. The addition of new imp methods does not affect users. You can use ISP to add new interfaces to the connection control class. In this way, you can create a migration path and call
The client program of the modem can follow this path to slowly obtain an API that is higher than the dial and hangup levels.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.