Bridge Mode of grinding design mode-4

Source: Internet
Author: User
ArticleDirectory
    • 3.4 generalized bridging-nowhere in Java
    • 3.5 Advantages and Disadvantages of Bridge Mode
    • 3.6 think about the Bridge Mode
    • 3.7 related models
3.4 generalized bridging-nowhere in Java

Write in JavaProgramAn important principle is "interface-oriented programming". The accurate point should be "abstract-oriented programming". In Java Development, more interfaces are used instead of abstract classes, therefore, it is generally called "interface-oriented programming.
The interface separates the specific implementation from the customer program using the interface, so that the specific implementation and customer program using the interface can be expanded separately without affecting each other. The program structure 12 using the interface is shown below:


Figure 12 program structure using interfaces

Some may think, how does it sound like a bridge function? Yes. If you want to simplify the abstract Part of the Bridge Mode and do not refined1_action part for the moment, it will be similar to the above structure. The simplified Bridge Mode Structure after removing the refinedabstraction is shown in Figure 13:


Figure 13 Simplified Bridge Mode Structure

is it similar? Some may think that there is a big difference. The main difference is that the client program of the preceding interface directly uses the interface object, unlike the abstract part of the bridge mode, is an interface that holds the specific implementation part, which leads to the drawn structure. One is dependency, and the other is aggregation Association.
think about their essential functions . The abstract Part of the Bridge Mode holds the interface of the specific implementation part. What is the ultimate goal, it is not necessary to call the methods in the specific implementation part of the interface to complete certain functions. This is no different from directly using the interface, but the form is a bit different. Besides, the customer program using the interface can also hold the corresponding interface object, which is the same in form.
that is to say, from a certain point of view, the bridge mode is just an extension of the design principle "abstract programming" . It is through the specific implementation interface that separates the abstract part from the specific implementation. The abstract part is equivalent to the client program that implements some interfaces, so that the abstract part and the implementation part are loosely coupled, this allows for independent changes.
in this way, almost all abstract-oriented programs can be regarded as the embodiment of the Bridge Mode, at least a simplified Bridge Mode, even in a broad sense. Java programming emphasizes "abstract-oriented programming". Therefore, broad bridging is everywhere in Java.
here is another example that you are most familiar. In Java application development, hierarchical implementation is the most basic design method. For example, the presentation layer, logic layer, and data layer are the most familiar three-tier architecture, some friends may have different names for them, but they are all the same thing.
the basic relationship between the three layers is that the presentation layer calls the logic layer, and the logic layer calls the data layer. What is the basis for calling? Of course they are interfaces. Their basic structure is 14:


Figure 14 basic three-tier architecture

Calling through interfaces separates the presentation layer from the logic layer. That is to say, changes in the presentation layer do not affect the logic layer. Likewise, changes in the logic layer do not affect the presentation layer. This is also why the same logic layer and data layer support different performance layers at the same time, such as the swing or Web mode.
APIs are also called between the logic layer and the data layer, which also separates the logic layer from the data layer so that they can be expanded independently. Especially at the data layer, there may be many implementation methods, such as database implementation and file implementation. Even if it is a database implementation, it can also be implemented for different databases, such as Oracle and DB2.
In short, through abstract programming, layers of the three-tier architecture can be independently expanded and changed without affecting other layers. This is exactly the function of the bridge mode, which achieves the separation of abstraction and implementation so that they can be changed independently. Of course, the three-tier architecture uses the bridge mode not only in one place, but at least two places, one between the presentation layer and the logic layer, one is between the logic layer and the data layer.
Next, let's take a look at the program structure of the two places where the bridge mode is used, and then look at the overall program structure. First look at the program structure between the logic layer and the data layer, as shown in Figure 15:


Figure 15 program structure at the logical layer and data layer

Let's look at the structure between the presentation layer and the logic layer, as shown in 16:


Figure 16 structure of presentation layer and logic layer

Then combine them to see the combined program structure, as shown in figure 17:


Figure 17 three-layer Combination Structure

From the broad Bridge Mode perspective, the layer-3 architecture that we are familiar with on weekdays is actually using the Bridge Mode in combination. As shown in the figure,The bridging mode can be used in a continuous combination. The implementation part of a bridging mode can be used as an abstract Part of the next bridging mode.. Similarly, the bridging mode can be used for combination from a three-tier architecture to a four-tier or five-tier architecture to a n-tier architecture.
From a more fundamental point of view, as long as it is an abstract-oriented Java program, it can be regarded as a bridge-mode application, which separates abstraction and implementation, so that they can change independently. However, as long as the purpose of separation is achieved, it doesn't matter if it is not called the bridge mode.

3.5 Advantages and Disadvantages of Bridge Mode
    • Separating abstraction and implementation
      The Bridge Mode separates the abstract and implementation parts, greatly improving the flexibility of the system. Let the abstract Part and implementation part be separated and define interfaces separately, which helps to layer the system and generate a better structured system. For the high-level part of the system, you only need to know the abstract part and the interface of the implementation part.
    • Better scalability
      Since the Bridge Mode separates the abstract and implementation parts and defines interfaces separately, the abstract and implementation parts can be separately extended without affecting each other, this greatly improves the scalability of the system.
    • Dynamic switchover implementation
      Since the bridging mode separates abstraction and implementation, dynamic selection and specific implementation can be achieved when bridging is implemented, that is to say, an implementation is no longer bound to an abstract interface, and can implement dynamic switching during runtime.
    • Reduces the number of child classes.
      According to the previous article, if there are two changing latitudes, if the inheritance implementation method is adopted, the product of the changeable quantities on the two latitudes is required; to use the bridge mode, the variable quantity and sub-classes at two latitudes are required. This can significantly reduce the number of child classes.
3.6 think about the Bridge Mode

1: essence of the bridge mode
essence of the Bridge Mode: separation of abstraction and implementation .
the most important task of the bridge mode is to separate the abstract part and the implementation part, which is the key to solving the problem. Only when abstraction and implementation are separated can they be changed independently. Only when abstraction and implementation can be changed independently can the system have better scalability and maintainability.
for other benefits, such as the ability to implement dynamic switching and reduce the number of child classes. After the abstract part and the implementation part are separated, if the abstract part and the implementation part are not separated, everything is free of discussion. Therefore, the essence of the bridge mode is to separate abstraction and implementation ".

2: embodiment of design principles
(1) the Bridge Mode achieves the principle of opening and closing.
generally, the abstract part and the implementation part of the bridge mode are changeable, that is, the application has two changing latitudes. The Bridge Mode finds these two changes, and encapsulate them separately to reasonably implement OCP.
when the bridge mode is used, the upper-layer merge action and implementor are usually unchanged, while the implementation of the specific implementor is variable, because role actions are implemented through interfaces, the implementation of specific implementor can be expanded, and multiple specific implementations can be implemented as needed.
Similarly, refinedabstraction is variable. It inherits and extends the aggregate action. Generally, in the implementation of refinedabstraction, the method in the aggregate action is called to complete more functions through combination, these functions are often related to specific services.
The Bridge Mode also demonstrates the combination of multiple objects and less inheritance of objects.
in the previous example, if you use Object Inheritance to extend the function, not only do you have strong coupling between objects, in addition, many sub-classes are required to complete the corresponding functions. As mentioned above, the product sub-classes of the changeable quantity at two latitudes are required.
the combination of objects not only simplifies and maintains the coupling between objects, but also greatly reduces the number of child classes, it requires a variable number and so many subclasses at two latitudes.

3: When to use the Bridge Mode
We recommend that you use the bridging mode in the following cases:

    • If you do not want to use a fixed binding relationship in the abstract and implementation parts, you can use the bridge mode to separate the abstract and implementation parts, then, you can dynamically set the specific implementation required for the abstract part during the running of the program. You can also dynamically switch the specific implementation.
    • If both the abstract part and the implementation part can be expanded, the bridge mode can be used to allow the abstract part and the implementation part to be changed independently, so that the individual extension can be flexibly implemented, instead of mixing them together, the scaling side will affect the other side.
    • If you want to implement partial modifications without affecting the customer, you can use the bridge mode. The customer is running an abstract-oriented interface, and partial modifications can be implemented independently of the abstract part, this will not affect the customer. It can also be said that it is transparent to the customer.
    • If an inherited implementation scheme is used, many sub-classes will be generated. In this case, you can consider using the Bridge Mode to analyze the reasons for functional changes and see if the data can be separated into different latitudes, then they are separated in the bridging mode to reduce the number of child classes.

 

3.7 related models
  • Bridging Mode and Policy Mode
    These two models have great similarities.
    If the abstract part of the bridge mode is simplified, if the callback action is not extended for the moment, the refinedworkflow action is removed. For the simplified structure of the bridge mode, see Figure 13. See Figure 17.1 for the structure of the Policy mode. We will find that their structures are similar to those shown in 18 at this time:


    Figure 18 structure of the bridge mode and Policy Mode
    Through the above structure, we can see that the bridge mode and the policy mode are so similar. You can regard the context of the policy mode as the object using the interface, while strategy is an interface. The specific implementation of the policy is equivalent to the specific implementation of the interface. In this case, in some cases, the bridge mode can be used to simulate the implementation of the Policy mode function.
    Although these two models are similar, they are still different. The main difference is that the purpose of the model is different. The purpose of the policy model is to encapsulate a seriesAlgorithmThese algorithms can be replaced with each other. The purpose of the bridge mode is to separate the abstract and implementation parts so that they can be changed independently.

  • Bridging Mode and status Mode
    In terms of the mode structure, the state mode and the policy mode are the same. The relationship between the two modes is basically similar to that between the bridge mode and the policy mode.
    The purpose of the state mode is to encapsulate the behavior corresponding to the state and change the behavior of the object when the internal state changes.
  • Bridge Mode and template method mode
    These two models have similarities.
    Although the standard template method mode is implemented by inheritance, the template method can also be implemented through the callback interface. If the implementation of the interface is independent, it is similar to calling the specific implementation method through the interface of the template method. This structure is similar to the simplified Bridge Mode.
    You can use the bridge mode to simulate the template method mode. If you define a method in the implementation of an aggregate action object, the method contains a fixed algorithm skeleton, that is, the method is equivalent to the template method. In the template method mode, the uncertain implementation step is delayed to the subclass for implementation. Now in the bridge mode, the uncertain implementation step is delegated to the specific implementation part for completion, some interfaces are implemented through callback to complete some steps in the algorithm skeleton. In this way, you can use the bridge mode to simulate the template method mode function.
    The bridge mode is used to simulate the functions of implementing the template method mode. Another potential benefit is that the template method can be easily expanded and changed. In the standard template method, one problem is that when the template changes, all subclasses will change, which is inconvenient. If you use the bridge mode to implement similar functions, you will not have this problem.
    In addition, the bridge mode can simulate the functions that can be implemented in the template method mode to implement specific business functions. It does not mean that the bridge mode and the template method mode are the same, or the bridge mode can replace the template method mode. Note that their functions, purposes, and essential ideas are different.
  • Bridge Mode and abstract factory Mode
    These two modes can be used in combination.
    In the bridge mode, the abstract part needs to obtain the interface object of the corresponding implementation part. Who will create the specific implementation object of the implementation part? This is where the abstract factory model comes in handy. That is, you can use the abstract factory mode to create and configure a specific implementation object.
    In fact, abstract workers are mainly used to create a series of objects. If few objects are created, or very simple, simple factories can also be used to achieve the same effect, but it is easier than abstract factories.
  • Bridge Mode and adapter Mode
    These two modes can be used in combination.
    The functions of these two modes are completely different. The functions of the adapter mode are mainly used to help irrelevant classes to work collaboratively, focusing on the classes that cannot work together due to interface incompatibility, so that they can work together. The Bridge Mode focuses on separating abstraction and implementation.
    Therefore, the use of the adapter mode is usually considered after the system design is complete, while the bridge mode should be considered at the beginning of the system.
    Although the functions are different, these two modes can still be used in combination. For example, if some interfaces are implemented, but some of them are not suitable for the interface requirements of the new functions, simply discard them, what should I do if some functions are still available? Then use the adapter to adapt, so that the old interface can adapt to the needs of new functions.

 

 

The grinding design mode is over. Thank you!

 

 

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.