[design mode] JavaScript bridge mode

Source: Internet
Author: User

Bridging mode description

Definition: The separation of abstraction and realization, so that it can be free and independent change;

Description: Because of the requirements of the software environment, so that the type abstraction has a variety of implementations to define their own changes, which makes us to separate the abstract implementation and implementation, so that the abstraction and implementation of decoupling, so that it can be separated independent changes, so that they can be free to add their own process implementation.

Bridging mode can solve the above problem, the role of bridging mode:

1. Abstract role, this abstract class defines a reference to the implementation of the interface definition;

2. Modify the abstract role, which expands and modifies the implementation of the abstract interface definition, which is used to refer to the implementation (underlying) operation of the implementing interface;

3. Implement the role, define the implementation of the interface definition;

4. Specific implementation, for the realization of the implementation of the interface definition, it is an underlying implementation;

As can be seen from the above, the bridge mode has two parts: the abstract part and the realization part; The implementation is partially used for the specific underlying implementation, and the abstraction is used to encapsulate the specific underlying reality, and the interface definition can be different;

5. Inheritance

What is inheritance? Inheritance is a relationship between a class and a class, or a relationship between objects of the same type, and inheriting classes can have all non-private members or methods of the inherited class, and inheritance is the most important feature of object-oriented and an important way to reuse. The advantage of inheritance is that it can realize code reuse, reduce the amount of code development, and shorten development time. Inheritance of the shortcomings of the packaging, the base class may be "white box" exposed the details of the method, object encapsulation, is the software component can become a good modular foundation, object encapsulation generally includes attribute state and method behavior, is the whole abstraction of the object world, object encapsulation, which is internal use, What is called for external use can be perfectly defined. Encapsulation guarantees that objects are less dependent on external components and reduce the impact on other objects due to internal changes. Good encapsulation of objects can guarantee "high cohesion, low coupling" between objects; for inheritance reuse, class compilation is shared statically, and the parent public methods and members can be used directly. Because of the dependency of the two, the modification of the parent class may affect the implementation of the subclass, sometimes it is necessary to modify some of the subclass's operations, etc. in turn, subclasses can override the implementation of the extended parent class, and the parent-defined constraint is easily changed by the easy quilt class. Since the two have a fixed relationship at execution time, when a new type is present, it is bound to redefine the subclass and not be reused at another level, that is, when the type is present, when the internal implementation is not satisfied with the existing subclass and the parent class, the subclass must be redefined, or even a new class is defined ( This may show the functional composition or similar, but the new implementation is not the same);

6. Combination and aggregation;

1>. Combination, which is a strong association, consists of a part of the whole, is a relationship of ownership, with the same life cycle, partial detachment of the whole will not have meaning; like a wolf, a lion this dynamic, the body and limbs of the eyes, is the whole and part of the combination of the relationship; 2>. Aggregation, this is a weak association, the two are only a containment relationship, the part can be separated from the existence of the whole independent, is the above wolves or lions, both are social animals, individual individuals are independent of the existence, in case of detachment from the group, or can do their own activities. The 2nd object-oriented principle is to use the combination/aggregation method instead of inheritance reuse as much as possible. Combination/aggregation, as little as possible to use inheritance, you can make the object as far as possible only responsible for a work, in the face of interface programming ideas, using the combination/aggregation multiplexing method, because the aggregation object is implemented by the interface, the interface can be abstract instantiation or using a method of dependency injection point to the implementation of the object, when the need to change Just make the interface specify the desired reality. (But inheritance is basically about rebuilding subclass inheritance.)

7. Bridging mode is achieved by abstraction, multi-implementation, and the Association of Abstract to the implementation role, which is the aggregation relationship;

8. Comparison of various graphs:

1>. Inheritance Relationship:

When the parent class is is-a with the child class, it is inherited;

2>. Combinatorial relationships

When the subclass is an integral part of the parent class, the has-a relationship is used in the combination relationship;

3>. The polymerization relationship is Hollow ling;

4>. bridging relationship Diagram;

The expression of abstract class and implementation, is the aggregation of the relationship, implementation is only an abstraction of an implementation, because the abstract based on interface interface programming, so when there is another implementation, just let interface point to the desired implementation;

Bridging mode is divided into two parts, part is the abstraction of things, such as the function of a message, there may be mail and text messages to send a few, some can be said that the behavior of things to achieve abstraction, such as the nature of the message content, such as ordinary, urgent, etc. We can also consider the implementation part as a generic component abstraction;

The idea of bridging mode is to let us independent of various abstract implementations, such as the above message and SMS, both based on the same interface interface implementation, when the client has the demand, can be between the two through the interface convenient switching;

Scenario Example:

For example, our teaching, teaching is sometimes carried out in the ordinary classroom, some in the multimedia classroom, some in the laboratory, teaching content of English, Chinese, physics, mathematics, etc.; Of course, the ordinary classroom is what can be taught, multimedia classrooms are also what courses can be used, and use up, the effect is better, students prefer;

Here, where to teach, is a way, and the curriculum is a kind of content, the way can be the realization of the content;

If only inherit the way, then the implementation of each content must be implemented by themselves, which will produce a lot of duplicate code;

At this point we can use the bridge mode, the way and content to carry out their own implementation, the way and the implementation of the use of aggregation method associated;

Source instance

1. Define various courses.

Abstract implementation of the tutorial content;

function couse () {    = {    function() {        console.log (' Teach Chinese Fruit ');    },     function() {        console.log (' Teach English class ');    },    function () {        console.log (' teaching Physics Course ');}    }

The next step is to abstract the teaching method:

1>. General classrooms:

functionOrdinary (couse) { This. couse =couse;  This. where = ' ordinary classroom ';} Ordinary.prototype={Chinese:function() {Console.log (' In ' + This. where + ');  This. Couse.chinese (); }, 中文版:function() {Console.log (' In ' + This. where + ');  This. Couse.english (); }, Physics:function() {Console.log (' In ' + This. where + ');  This. Couse.physics (); }}

2>. Multimedia classrooms:

functionMultimedia (couse) { This. couse =couse;  This. where = ' ordinary classroom ';} Multimedia.prototype={Chinese:function() {Console.log (' In ' + This. where + ');  This. Couse.chinese (); //define new content;Console.log (' Next will be the relevant course film ')}, 中文版:function() {Console.log (' In ' + This. where + ');  This. Couse.english (); //define new content;Console.log (' Please wear your headphones and listen carefully; '))}, Physics:function() {Console.log (' In ' + This. where + ');  This. Couse.physics (); }}

How to use:

// General Classrooms var New couse (); var classroom = Ordinary (couse); Classroom.chinese (); // Multimedia Classrooms New Multimeida (couse); Classroom.chinese ();

Other Notes

Bridging mode, more difficult to understand, mainly to understand the principles of some design patterns, as well as the idea of bridging mode, the principle is not rotten with inheritance because the inheritance is compiled and fixed he can not change the implementation of the father; object-oriented design uses combination/aggregation multiplexing instead of inheritance reuse, and interface-oriented programming benefits Understanding the benefits of composite aggregation multiplexing to object class encapsulation;

Bridging mode requires object abstraction and implementation abstraction to be handled separately, so that it is easy to extend and avoid internal modification, which embodies the principle of object-oriented to extended opening and modification closure. This pattern is very useful, although it is not often met.

[design mode] JavaScript bridge mode

Related Article

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.