“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
The bridge model
Bridges are used to decouple abstractions from implementations so that they can vary independently. This type of design pattern is structural in that it decouples abstraction and implementation by providing a bridge between them.
In the case of multiple possible changes, inheritance can result in many classes that are inflexible to extend, so the bridge pattern is used to separate the abstract from the implementation so that they can all change independently. Advantages of bridge mode:
- 1. Separation of abstraction and implementation.
- 2. Excellent ability to expand.
- 3. Implementation details are transparent to customers.
The following is a brief introduction based on cases.
Case analysis
What this time uses is the more classic case that everybody is familiar with, is some year software designer test questions. Details are as follows:
To realize an image browsing system, the system is required to display BMP, JPEG and GIF files in three formats, and can run on Windows and Linux two operating systems. The system first parses files in BMP, JPEG and GIF formats into a pixel matrix, which is then displayed on the screen. The system needs to be scalable to support new file formats and operating systems. In order to meet the above requirements and reduce the number of subclasses to be generated, the Bridge design mode is adopted for design, and the class diagram is shown below.
Through the problem analysis, we can know that the file format to be read may be multiple, and may display images in different operating systems, and need to display the file and system implementation together. After analysis, the file format can be many, many operating systems, so using bridge mode is a good design for this problem. Let’s start with a simple code implementation. The following is the completed file.
Write images to show abstract classes
Abstract class of image display is written to achieve image display of different types of files quickly after new operating systems are added. Decouple image file types from the operating system. Write the code below, only one image output method.
/** * @ClassName ImageHandler * @Description: * @author Public Id: Public abstract class ImageHandler {public abstract void doPaint(PictureElement PE); }Copy the code
PictureElement is the image pixel that is displayed. Since this is a mock presentation, only the file name is displayed for this object.
/** * @ClassName PictureElement * @Description: Public class PictureElement {private String fileName; public String getFileName() { return fileName; } public PictureElement(String fileName) { this.fileName = fileName; } public void setFileName(String fileName) { this.fileName = fileName; }}Copy the code
Linux System Display
/** * @ClassName LinuxImageImpl * @Description: * @author Public Id: Public class LinuxImageImpl extends ImageHandler {@override public void DoPaint (PictureElement PE) {system.out.println ("Linux output image. The file name is: "+ PE. GetFileName ()); }}Copy the code
Windows System Display
/** * @ClassName WinImageImpl * @Description: * @author Public Id: Public class WinImageImpl extends ImageHandler {@override public void DoPaint (PictureElement PE) {system.out.println ("Windows output image. The file name is: "+ PE. GetFileName ()); }}Copy the code
Write image file processing classes
Create an abstract ImageInfo class, which contains an image display class and an image file conversion class.
/** * @ClassName ImageInfo * @Description: Public abstract class ImageInfo {protected ImageHandler ImageHandler; public void setImageHandler(ImageHandler image) { this.imageHandler = image; } public abstract void parseFile(String fileName); }Copy the code
Write different file handling methods
JPEG file processing.
/** * @ClassName JPEG * @Description: * @author Public Id: Public class JPEG extends ImageInfo {@override public void parseFile(String fileName) { System.out.println(" Convert JPEG format file to image pixel matrix object "); PictureElement pe = new PictureElement(fileName); imageHandler.doPaint(pe); }}Copy the code
GIF file processing.
/** * @ClassName GIF * @Description: * @author Public Id: Public class extends ImageInfo {@override public void parseFile(String fileName) { System.out.println(" Convert GIF format file to image pixel matrix object "); PictureElement pe = new PictureElement(fileName); imageHandler.doPaint(pe); }}Copy the code
BMP file processing.
/** * @ClassName BMP * @Description: * @author Public Id: Public class BMP extends ImageInfo {@override public void parseFile(String fileName) { System.out.println(" Convert BMP format files into image pixel matrix objects "); PictureElement pe = new PictureElement(fileName); imageHandler.doPaint(pe); }}Copy the code
run
Run the simple test class in Main and see the results below.
/** * @ClassName MainApplication * @Description: * @author Public Id: * @version V1.0 **/ public class MainApplication {public static void main(String[] args) ImageInfo imageInfoGIF = new GIF(); LinuxImageImpl linuxImage = new LinuxImageImpl(); imageInfoGIF.setImageHandler(linuxImage); imageInfoGIF.parseFile("test.GIF"); ImageInfo imageInfoJPEG = new JPEG(); WinImageImpl winImage = new WinImageImpl(); imageInfoJPEG.setImageHandler(winImage); imageInfoJPEG.parseFile("test.JPEG"); }}Copy the code
Running results:
Convert GIF format files into image pixel matrix objects for Linux to output images. The file name is: test.gif Convert JPEG file into image pixel matrix object Windows output image. The file name is: test.jpegCopy the code
Ok, a simple bridge mode is introduced to complete, learning design mode, as far as possible combined with the familiar case analysis, so that there will be twice the result with half the effort, but also easier to understand. Try using design patterns in your projects.
Thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.
About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.