design mode (vi) bridge mode bridges (structural type)

Source: Internet
Author: User
Tags vars

design mode (vi) bridge mode bridges (structural type)

1. Overview

In a software system, some types, because of their own logic, have two or more dimensions that change, so how do you deal with this "multidimensional change"? How can you use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity?

Example 1: Imagine that if you want to draw a rectangle, circle, ellipse, square, we need at least 4 shape classes, but if the drawing needs to have a different color, such as red, green, blue, etc., at this time at least the following two design scenarios:

• The first design is to provide a set of various color versions for each of the shapes.

• The second design is to combine shapes and colors according to actual needs.

Scenario 1:

Scenario 2:


For a system with two changing dimensions (that is, the reason for two changes), the number of classes in the design system is less, and the system expansion is more convenient. Design Scheme Two is the application of bridge mode. Bridging mode transforms inheritance relationships into associations, reducing the coupling between classes and classes, and reducing the amount of code written. Example 2: An ordinary switch-controlled electric light, electric fan, etc., are examples of bridging. The purpose of the switch is to turn the device on or off. The actual switch can be a simple double-knife zipper switch, or it can be a dimmer switch.

2. Questions

How to deal with this "multidimensional change"? How can you use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity?

3. Solution

Bridge mode : separates the abstract and the implementation parts so that they can be changed independently.        It is a structural pattern, also known as the handle body (Handle and body) mode or interface (Interface) mode. When an abstraction may have multiple implementations, inheritance is often used to reconcile them. An abstract class that defines an interface to that abstraction. The specific subclasses are implemented in different ways, but this approach is sometimes not flexible enough. The inheritance mechanism fixed the abstract part with his sight, making it difficult to modify, augment and recharge the abstract and implementation parts independently.

Understanding bridging patterns requires an understanding of how to decouple abstraction (abstraction) from implementation (implementation) so that they can vary independently. • Abstraction: Abstraction is the omission of information to treat different entities as the same entity. In object-oriented, the process of extracting the common nature of objects to form a class is the process of abstraction. Implementation: For the specific implementation of the abstraction, is the realization, abstraction and realization is a pair of reciprocal concepts, the realization of the object is more specific than the abstract, is a further embodiment of the abstract things of the product. Decoupling: Decoupling is to free up the coupling between abstraction and realization, or to change the strong association between them to a weak association, and to change the inheritance relationship between the two roles into the association relation. The so-called decoupling in bridging mode refers to the use of association relationships (combinations or aggregation relationships) rather than inheritance relationships between abstractions and implementations of a software system, so that they can vary relatively independently, which is the intention of bridging mode.

4. Applicability

1). You do not want to have a fixed bond between the abstract and his implementation part, such as in the run-time implementation part of the program should be selectable or switched.

2). The abstraction of a class and his vision can be augmented by the method of generating subclasses. Bridge mode allows you to have a different abstract interface

and implementation parts, and expand them.

3). Modifications to an abstract implementation section should have no effect on the customer, i.e. the customer's code does not need to be recompiled.

4). You want to completely hide the abstract implementation part from the customer.

5). You want to share the implementation across multiple implementations, but at the same time ask the customer not to know that.

5. Structure


6. Composition of the build pattern

Abstract class (abstraction): Defines an interface for an abstract class and maintains a pointer to an Implementor type Object

Augment abstract class (Refinedabstraction): Expands the interface defined by abstraction

Implementation class Interface (Implementor): Defines the interface of the implementation class, which is not necessarily identical to the interface of the abstraction, in fact the two interfaces can be completely different. Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations.

Implementation Class (Concreteimplementor): Implements the Implementor interface and defines its specific implementation.

7. Effects
Bridge mode has some of the following advantages:
1) Separation interface and its implementation part of an implementation is not necessarily bound to an interface. The implementation of an abstract class can be configured at runtime, and an object can even change its implementation at run time. Separating abstraction from Implementor helps reduce the dependency on implementing partial compile time, and when you change an implementation class, you do not need to recompile the abstraction class and its client programs. In order to ensure binary compatibility between different versions of a class library, this must be the property. In addition, the separation of interfaces and implementations helps in layering, resulting in a better structured system in which the high-level parts of the system need only know abstraction and implementor.
2) Increased scalability You can expand the abstraction and implementor hierarchies independently.

3) Implementation details transparent to the customer you can hide implementation details from the customer, such as shared implementor objects and the corresponding reference counting mechanism, if any.

Disadvantages of Bridging Mode • The introduction of bridging mode increases the difficulty of system understanding and design, because the aggregation correlation relationship is based on the abstraction layer, which requires the developer to design and program the abstraction. • Bridging mode requires the correct identification of the two independently changing dimensions of the system, so its scope of use has some limitations.

8. Implement

Imitation brush:

Now need to provide 3 types of large and medium-sized brushes, to draw 5 different colors, if using crayons, we need to prepare 3*5=15 crayon, that is, we must prepare 15 specific crayon class. And if the use of a brush, only need 3 types of brush, plus 5 color box, with 3+5=8 class can achieve 15 crayons function.

In fact, the key difference between a crayon and a brush is whether the pen and color can be separated. The abstraction (abstraction) is decoupled from the implementation (implementation), allowing the two to change independently. The key is whether to decouple. The color of crayons and crayons is inseparable, so it is necessary to use 15 colors, different sizes of crayons to draw pictures. and brushes and pigments can be very good decoupling, independent changes, it simplifies the operation. Here, the concept of abstraction is: "Brush paint with paint", and in the realization, the brush has a large medium and small third, the pigment has red, green and blue black and white and other 5 kinds, so it can appear 3x5 kind of combination. Each participant (brush and paint) can switch freely on his or her own degree of freedom.

Crayon because the pen and color can not be separated, resulting in pen and color two degrees of freedom cannot be changed individually, so that only 15 objects can be created to complete the task.

Bridge mode transforms the inheritance relationship into a composite relationship, which reduces the coupling between systems and reduces the amount of code written.

Uml

Code implementation:

[PHP]View Plain Copy print?
  1. <?php
  2. /******************************abstraction **************************/
  3. /**
  4. *
  5. * Abstraction Abstract class Interface
  6. * @author Guisu
  7. *
  8. */
  9. Abstract class Brushpenabstraction {
  10. protected $_implementorcolor = null;
  11. /** 
  12. *
  13. * Enter description here ...
  14. * @param Color $color
  15. */
  16. Public function Setimplementorcolor (implementorcolor $color) {
  17. $this->_implementorcolor = $color;
  18. }
  19. /** 
  20. *
  21. * Enter description here ...
  22. */
  23. Public abstract function Operationdraw ();
  24. }
  25. /******************************refinedabstraction **************************/
  26. /**
  27. *
  28. * expanded by abstraction; Big Brush
  29. * @author Guisu
  30. *
  31. */
  32. Class Bigbrushpenrefinedabstraction extends Brushpenabstraction {
  33. Public function Operationdraw () {
  34. echo ' Big and ', $this->_implementorcolor->bepaint (), ' drawing ';
  35. }
  36. }
  37. /**
  38. *
  39. * Expanded by abstraction; Chinese brush
  40. * @author Guisu
  41. *
  42. */
  43. Class Middlebrushpenrefinedabstraction extends Brushpenabstraction {
  44. Public function Operationdraw () {
  45. echo ' Middle and ', $this->_implementorcolor->bepaint (), ' drawing ';
  46. }
  47. }
  48. /**
  49. *
  50. * expanded by abstraction; small brush
  51. * @author Guisu
  52. *
  53. */
  54. Class Smallbrushpenrefinedabstraction extends Brushpenabstraction {
  55. Public function Operationdraw () {
  56. echo ' Small and ', $this->_implementorcolor->bepaint (), ' drawing ';
  57. }
  58. }
  59. /******************************implementor **************************/
  60. /**
  61. * Implementation class Interface (Implementor)
  62. *
  63. * @author mo-87
  64. *
  65. */
  66. Class Implementorcolor {
  67. protected $value;
  68. /** 
  69. * Coloring
  70. *
  71. */
  72. Public function Bepaint () {
  73. Echo $this->value;
  74. }
  75. }
  76. /******************************oncrete implementor **************************/
  77. Class Oncreteimplementorred extends Implementorcolor {
  78. Public function __construct () {
  79. $this->value = "Red";
  80. }
  81. /** 
  82. * Can cover
  83. */
  84. Public function Bepaint () {
  85. Echo $this->value;
  86. }
  87. }
  88. Class Oncreteimplementorblue extends Implementorcolor {
  89. Public function __construct () {
  90. $this->value = "Blue";
  91. }
  92. }
  93. Class Oncreteimplementorgreen extends Implementorcolor {
  94. Public function __construct () {
  95. $this->value = "green";
  96. }
  97. }
  98. Class Oncreteimplementorwhite extends Implementorcolor {
  99. Public function __construct () {
  100. $this->value = "White";
  101. }
  102. }
  103. Class Oncreteimplementorblack extends Implementorcolor {
  104. Public function __construct () {
  105. $this->value = "BLACK";
  106. }
  107. }
  108. /**
  109. *
  110. * Client program
  111. * @author Guisu
  112. *
  113. */
  114. Class Client {
  115. public static function Main () {
  116. //small strokes red
  117. $objRAbstraction = new Smallbrushpenrefinedabstraction ();
  118. $objRAbstraction->setimplementorcolor (new oncreteimplementorred ());
  119. $objRAbstraction->operationdraw ();
  120. }
  121. }
  122. Client::main ();

Cross-platform video player: Two dimension changes, platform and video files in different formats:

9. Bridging mode and other related modes

1) Abstract Factory mode can be used to create and configure a specific bridge mode.

2) The adapter mode is used to help unrelated classes work together, and it is usually not used until the system is designed. However, bridge mode is used at the beginning of the system, allowing the abstract interface and implementation parts to be changed independently.

3) The difference between Bridge mode and decoration:

Decoration Mode:

These two patterns are to some extent to reduce the number of subclasses and avoid complex inheritance relationships. However, the methods they solve are different, the decorative mode of the class in the more than the base class in a separate class, to adapt to the need for new functions, when we describe the new function of the class encapsulated in the base class object, we get the necessary subclass object, These classes, which describe new features, can be combined to achieve many combinations of functionality.

Bridging mode:

Bridging mode abstracts the implementation details of the original base class, constructs it into an implementation structure, and then changes the original base class into an abstract hierarchical structure, so that the system can change independently in many dimensions.

10. Summary

Bridge mode is a very useful pattern and very complex, it is well suited to the open-closed principle and priority to use the object, rather than inherit these two object-oriented principles.

design mode (vi) bridge mode bridges (structural type)

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.