Java design pattern-difference between the bridging pattern and the policy pattern

Source: Internet
Author: User

[Reproduced from: http://www.blogjava.net/wangle/archive/2007/04/25/113545.html]

The bridge mode is a type of structural mode, while the strategy mode is a behavior mode. The following is their UML structure diagram.

In the bridge mode, the aggregate action references implementor by means of aggregation.

 

In policy mode, context also uses aggregation to reference the startegy abstract interface.

 

According to their structure, in both modes, there is a situation where an object uses aggregation to reference the abstract interface of another object, the abstract interface can be implemented in multiple ways and can be replaced. It can be said that the two represent the decoupling between the caller and the called, as well as the separation of abstract interfaces and implementations.

So what is the difference between the two?

1. first, there is a difference between the two in form. By comparing the two structure charts, we can find that in the bridge mode, not only implementor is changed (concreateimplementior ), in addition, the aggregate action can also be changed (refined1_action), and the changes are completely independent. The refined1_action and concreateimplementior are loosely coupled. They are only linked by the relationship between the aggregate action and implementor. In the policy mode, context changes are not considered, but the algorithm is irreplaceable.

2. In terms of semantics, the Bridge Mode emphasizes that the implementor interface only provides basic operations, while the merge Action defines high-level operations based on these basic operations. The policy mode emphasizes that the Strategy abstract interface provides an algorithm, which is generally stateless and has no data, while the context simply calls these algorithms to complete their operations.

3. in the bridge mode, the implementor interface is defined as well as the intent action interface. The intent action interface exists not only to communicate with the implementor, but also reflects the features of the structural mode: classes and objects are combined by inheritance and aggregation to form a larger structure. In policy mode, startegy and context interfaces are both cooperative interfaces and do not involve other functional interfaces. Therefore, they are a type of behavior mode. The main feature of the behavior mode is to process the communication mode between objects. It is usually to introduce the intermediary object to decouple the communication parties. In this case, the context is actually decoupled from the actual algorithm provider.

Therefore, compared with the policy mode, the bridge mode requires more content and more complex structures. The main significance of the expression of the bridge mode is actually the principle of interface isolation, that is, to distinguish the two systems that are essentially not cohesive, so that they can be loosely combined, policy decoupling is only a layer of a certain algorithm, not at the system level. From the structure diagram, we can see that the policy structure is inclusive in the bridge structure, and there must be a policy pattern in the bridge. The rule action and implementor can be considered as a policy pattern, however, in the bridge mode, implementor generally provides a series of system-based operations, and implementor is a static structure with state and data. The bridging action can also be changed independently.

 

Reference: Lu Zhenyu's design patterns-the story of crayons and brushes

The difference between bridge strategy and state will be discussed later.

 

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.