Handling multidimensional changes-Bridging Mode (3)

Source: Internet
Author: User
Tags bmp image pngimage
10.3 complete solution

To reduce the number of child classes to be generated, the operating system and image file formats are separated so that they can be changed independently, sunny developers use the bridge mode to reconstruct the cross-platform Image Browsing System. The basic structure is 10-5:

In Figure 10-5, an image acts as an abstract class, and its subclass jpgimage, pngimage, BMP image, and gifimage act as an extended abstract class; imageimp acts as an implementation class interface, and its subclass windowsimp, linuximp, and uniximp act as specific implementation classes. The complete code is as follows:
// Pixel matrix class: auxiliary class. Files of various formats are eventually converted to pixel matrices, different operating systems provide different ways to display the pixel matrix class Matrix {// Code omitted here} // Abstract Image class: abstract class image {protected imageimp IMP; public void setimageimp (imageimp IMP) {This. IMP = imp;} public abstract void parsefile (string filename);} // Abstract Operating System implementation class: Implementation class interface imageimp {public void dopaint (matrix m ); // display pixel matrix m} // implementation class of the Windows operating system: Implementation class windowsimp implements imageimp {public void dopaint (Matrix M) {// call the drawing function of the Windows system to draw the pixel matrix system. out. print ("display images in Windows OS:") ;}// implementation class of Linux OS: Implementation class linuximp implements imageimp {public void dopaint (matrix m) {// call the rendering function of the Linux system to draw the pixel matrix system. out. print ("display images in Linux:") ;}// implementation class of UNIX: Implementation class uniximp implements imageimp {public void dopaint (matrix m) {// call the rendering function of the UNIX system to draw the pixel matrix system. out. print ("display image in UNIX operating system:") ;}// JPG Format Image: Expanded abstract class jpgima GE extends image {public void parsefile (string filename) {// simulate parsing JPG files and obtain a pixel matrix object m; matrix m = new matrix (); imp. dopaint (m); system. out. println (filename + ", in JPG format. ") ;}}// PNG Format Image: extends the abstract class pngimage extends image {public void parsefile (string filename) {// simulates parsing PNG files and obtains a pixel matrix object m; matrix M = new matrix (); imp. dopaint (m); system. out. println (filename + ", format: PNG. ") ;}}// BMP Format Image: extends the abstract class BMP image extends image {public void parsefile (string filename) {// simulates parsing the BMP file and obtains a pixel matrix object m; matrix M = new matrix (); imp. dopaint (m); system. out. println (filename + ", in the format of BMP. ") ;}}// GIF format image: extends the abstract class gifimage extends image {public void parsefile (string filename) {// simulates parsing the GIF file and obtains a pixel matrix object m; matrix M = new matrix (); imp. dopaint (m); system. out. println (filename + ", in GIF format. ");}}

To make the system more flexible and scalable, we have introduced a configuration file to store the specific expanded abstract classes and implementation class names in the configuration file, and then generate objects through reflection, inject the generated implementation class object into the expanded abstract class object. The configuration file config. the XML Code is as follows:

<?xml version="1.0"?><config><!--RefinedAbstraction--><className>JPGImage</className> <!--ConcreteImplementor--><className>WindowsImp</className></config>

The code for reading the configuration file config. xml and reflecting the generated object's xmlutil class is as follows:

Import javax. XML. parsers. *; import Org. w3C. dom. *; import Org. XML. sax. saxexception; import Java. io. *; public class xmlutil {// This method is used to extract a specific class name from the xml configuration file and return an instance object public static object getbean (string ARGs) {try {// create Document Object documentbuilderfactory dfactory = documentbuilderfactory. newinstance (); documentbuilder builder = dfactory. newdocumentbuilder (); document DOC; Doc = builder. parse (new file ("config. XML "); nodelist NL = NULL; node classnode = NULL; string cname = NULL; NL = Doc. getelementsbytagname ("classname"); If (ARGs. equals ("image") {// obtain the first node that contains the class name, that is, the extended abstract class classnode = NL. item (0 ). getfirstchild ();} else if (ARGs. equals ("OS") {// obtain the second node that contains the class name, that is, the specific implementation class classnode = NL. item (1 ). getfirstchild ();} cname = classnode. getnodevalue (); // generate an Instance Object using the class name and return Class C = Class. forname (cname); object OBJ = C. newinstance (); Return OBJ;} catch (exception e) {e. printstacktrace (); return NULL ;}}}

Write the following client test code:

Class client {public static void main (string ARGs []) {image; imageimp IMP; image = (image) xmlutil. getbean ("image"); imp = (imageimp) xmlutil. getbean ("OS"); image. setimageimp (IMP); image. parsefile ("maid ");}}

Compile and run the program. The output result is as follows:

The image is displayed in JPG format in the Windows operating system.

To change the image file format or OS, you only need to modify the configuration file,You can determine the specific file format by analyzing the suffix of the image file format, and obtain the operating system information when the program is running to determine the operating system type.You do not need to use the configuration file. When a new image file format or operating system is added, the original system does not need to be modified. You only need to add a corresponding extended abstract class or specific implementation class. The system has better scalability, it fully complies with the "open and closed principles ".

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.