Java implements 23 design patterns: bridging pattern

Classification of 23 design patterns 1, Overview The Bridge pattern is defined as follows: separating abstractions fr...

Classification of 23 design patterns

1, Overview

The Bridge pattern is defined as follows: separating abstractions from implementations so that they can change independently. It is realized by using combination relation instead of inheritance relation, which reduces the coupling degree of abstraction and realization. The main purpose of Bridge mode is to realize the independent change of the two dimensions of abstract part and implementation part, and then to connect the two parts with the combination relationship; for example, some classes have two or more dimensional changes, such as graphics can be divided by shape or color. How to design software like Photoshop that can draw different shapes and colors? If we use inheritance method, there are m × n kinds of M shapes and n colors, which not only correspond to many subclasses, but also are difficult to expand.

advantage

1. Separation of abstraction and implementation.
2. Excellent extension ability.
3. Make the details transparent to customers.

shortcoming

The introduction of bridge mode will increase the difficulty of system understanding and design. Because the aggregation relationship is established in the abstract layer, developers are required to design and program for the abstract.

scene

1. If a system needs to increase more flexibility between the abstract and concrete roles of components, and avoid the establishment of static inheritance relationship between the two levels, they can establish an association relationship in the abstract level through the bridging mode.
2. For those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance, the bridging mode is particularly suitable.
3. A class has two independent changing dimensions, and both of them need to be extended.

2, Implementation

1. Structure diagram

The Bridge mode consists of the following main roles.

  • Abstraction role: defines an abstract class and contains a reference to an implementation object.
  • Extended abstraction role: it is a subclass of the abstract role, which implements the business methods in the parent class, and realizes the business methods in the role through the composite relation call.
  • Implementor role: defines the interface of the implementer role, which can be called by the extended abstract role.
  • Concrete Implementor role: the concrete implementation of the role interface is given.

PS: the UML structure diagram can be referred to. The example implementation is not completed according to the UML diagram, and it can be implemented flexibly;

2. Implementation
  • Gifts belong to abstract category
package cn.missbe.model.bridge; /** * Copyright (c) 2020. * Email: [email protected] * * @author lyg 2020/4/29 8:46 PM * description: **/ /** * Abstract gift */ public abstract class Gift { protected GiftImpl giftImpl; void printGiftName() { System.out.println("Gift is:" + giftImpl.giftName); } } /** * Nice gift */ class LovelyGift extends Gift { public LovelyGift(GiftImpl giftImpl) { this.giftImpl = giftImpl; } } /** * Practical gift */ class UtilityGift extends Gift { public UtilityGift(GiftImpl giftImpl) { this.giftImpl = giftImpl; } }
  • Gifts belong to the category of concrete realization
package cn.missbe.model.bridge; /** * Copyright (c) 2020. * Email: [email protected] * * @author lyg 2020/4/29 8:49 PM * description: **/ public abstract class GiftImpl { protected String giftName; String getGiftName() { return giftName; } } /** * flower */ class Flower extends GiftImpl { public Flower(String giftName) { this.giftName = giftName; } } /** * book */ class Book extends GiftImpl { public Book(String giftName) { this.giftName = giftName; } }
  • Main test
package cn.missbe.model.bridge; /** * Copyright (c) 2020. * Email: [email protected] * @author lyg 2020/4/29 4:48 PM * description: * Bridge mode: just understand * Separate abstract and concrete, and connect abstract and concrete with aggregation (bridge) * The bridge pattern is to separate the abstract part from its implementation part, so that they can all change independently and transform the inheritance relationship into the association relationship **/ public class Main { public static void main(String[] args) { /* * Now suppose the scene boy needs to give a gift to the girl * Gifts (abstractions) can give good-looking or practical gifts. Both good-looking and practical gifts belong to abstract concepts, * Specific gift flowers and rings belong to the beautiful category * Specific gift books and umbrellas are all practical * Abstract concepts can change independently. For example, warm gifts and cutting-edge gifts can be found at the back * Specific concepts can also be changed independently, such as car, pen, etc., and the number of classes may explode if inheritance is adopted */ Gift giftLovely = new LovelyGift(new Flower("LovelyFlower")); giftLovely.printGiftName(); ///Flowers belong to the category of good-looking, good-looking belongs to abstract, flowers are specific gifts, two dimensional changes Gift giftUtility = new UtilityGift(new Flower("UtilityBook")); giftUtility.printGiftName(); ///Book belongs to the category of utility, utility belongs to abstraction, book is a concrete gift, two dimensions change Gift giftBook = new UtilityGift(new Book("Book")); giftBook.printGiftName(); } }

5 June 2020, 00:05 | Views: 4256

Add new comment

For adding a comment, please log in
or create account

0 comments