Talk about the design mode ~ Bridge)

Source: Internet
Author: User

Returned directory

In software systems, some types of software systems have two or more dimensional changes due to their own logic. How can we deal with such "multi-dimensional changes "? How can we use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This requires the bridge mode.

Intention

[Gof95] when proposing the bridge model, it is pointed out that the purpose of the bridge model is to "decouple abstract action and implementation so that the two can change independently ". This sentence has three keywords: abstraction, implementation, and decoupling.

Member of Bridge Mode

Abstraction

The conceptual relationships that exist in multiple entities are abstract. As a process, abstraction is to ignore some information and treat different entities as the same entities.

Implementation

The specific implementation provided by abstraction is implementation. The implementation here is not a specific implementation, but an interface or abstract class, which is an extension of abstraction.

Decoupling

Coupling is a strong association between two entities. Removing their strong associations is the relief of coupling, or decoupling. Here, decoupling means to free up coupling between abstraction and reality, or change strong associations between them into weak associations. Change the inheritance relationship between two roles to an aggregation relationship, that is, change the strong association between them to a weak Association. Therefore, the so-called decoupling in the bridge mode refers to the use of a combination/aggregation relationship between the abstraction and implementation of a software system, rather than an inheritance relationship, so that the two can change relatively independently. This is the purpose of the bridge model. When can it be used?

Some types have two or more dimensions due to their own logic. In this case, the bridge mode is used.

Structure of Bridge Mode

Implementation of Bridge Mode

Valid action: the abstract, which has references to the real-time user.

Refinedabstraction: updates the abstract and extends the abstract. It can be used to add or modify some of the abstract's functions.

Implementor: The implementer. It is an interface or function class that abstracts the implementation.

Concreteimplementora: The implementer is a method for implementing implementor.

C # Implementation of Bridge Mode
# Region Bridge pattern # region abstractions // "your action" class extends action {// fields protected implementor; // properties public implementor {set {implementor = value ;}} // Methods virtual public void operation () {implementor. operation () ;}// "refinedaskaction" class refinedcontactaction: Role action {// Methods override public void operation () {implementor. operation () ;}# endregion # region implementer // "implementor" abstract class implementor {// Methods Abstract Public void operation ();} // "concreteimplementora" class concreteimplementora: implementor {// Methods override public void operation () {console. writeline ("concreteimplementora operation") ;}// "concreteimplementorb" class concreteimplementorb: implementor {// Methods override public void operation () {console. writeline ("concreteimplementorb operation") ;}# endregion # endregion

Call Code

            Abstraction abstraction = new RefinedAbstraction();            // Set implementation and call            abstraction.Implementor = new ConcreteImplementorA();            abstraction.Operation();            // Change implemention and call            abstraction.Implementor = new ConcreteImplementorB();            abstraction.Operation();

Result

Returned directory

Talk about the design mode ~ Bridge)

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.