Design Mode: bridge mode; design mode: Bridge

Source: Internet
Author: User
Tags bmp image pngimage

Design Mode: bridge mode; design mode: Bridge

1. Class Diagram

Instance class diagram

2. Create a project

........................

3. Create a Matrix: pixel Matrix class, auxiliary class, and image files of various formats are eventually converted into pixel matrices. different operating systems provide different methods of real pixel matrices.

Namespace BridgeSample

{

Class Matrix

{

// Code omitted

}

} 

4. Create ImageImp: Abstract The operating system implementation class and act as the implementation class interface.

Namespace BridgeSample

{

Interface ImageImp

{

Void DoPaint (Matrix m); // displays the pixel Matrix.

}

}

5. Create WindowsImp: The Implementation class of the Windows operating system and act as the specific implementation class. 

Using System;

Namespace BridgeSample

{

Class WindowsImp: ImageImp

{

Public void DoPaint (Matrix m)

{

// Call the rendering function of Windows to draw the pixel matrix.

Console. Write ("display images in Windows :");

}

}

}

6. New LinuxImp: Linux implementation class, acting as a specific implementation class

// LinuxImp. cs

Using System;

Namespace BridgeSample

{

Class LinuxImp: ImageImp

{

Public void DoPaint (Matrix m)

{

// Call the rendering function of the Linux system to draw the pixel matrix.

Console. Write ("display images in Linux :");

}

}

}

7. Create a UnixImp: UNIX operating system implementation class, acting as a specific implementation class 

Using System;

Namespace BridgeSample

{

Class UnixImp: ImageImp

{

Public void DoPaint (Matrix m)

{

// Call the rendering function of the Unix system to draw the pixel matrix.

Console. Write ("display images in Unix operating systems :");

}

}

}

8. Image: Abstract Image class, acting as an abstract class

Namespace BridgeSample

{

Abstract class Image

{

Protected ImageImp imp;

// Inject implementation class interface object

Public void SetImageImp (ImageImp imp)

{

This. imp = imp;

}

/// <Summary>

/// Convert a file to a pixel matrix

/// </Summary>

/// <Param name = "fileName"> </param>

Public abstract void ParseFile (string fileName );

}

}

9. JPGImage: JPG Format Image class, acting as an extended abstract class 

Using System;

Namespace BridgeSample

{

Class JPGImage: Image

{

Public override void ParseFile (string fileName)

{

// Parse the JPG file and obtain a pixel matrix object m;

Matrix m = new Matrix ();

Imp. DoPaint (m); // call the pixel conversion method of the Operating System

 

Console. WriteLine ("{0}, in JPG format. ", FileName );

}

}

}

10. Create a PNGImage: PNG Image class to act as an extended abstract class.

Using System;

Namespace BridgeSample

{

Class PNGImage: Image

{

Public override void ParseFile (string fileName)

{

// Parse the PNG file and obtain a pixel matrix object m;

Matrix m = new Matrix ();

Imp. DoPaint (m); // call the pixel conversion method of the Operating System

 

Console. WriteLine ("{0}, in PNG format. ", FileName );

}

}

}

11. Create a New BMP image: BMP Format Image class, acting as an extended abstract class

Using System;

Namespace BridgeSample

{

Class BMP Image: Image

{

Public override void ParseFile (string fileName)

{

// Parse the PNG file and obtain a pixel matrix object m;

Matrix m = new Matrix ();

Imp. DoPaint (m); // call the pixel conversion method of the Operating System

 

Console. WriteLine ("{0}, in the format of BMP. ", FileName );

}

}

}

12. Create a New GIF image class to act as an extended abstract class.

Using System;

Namespace BridgeSample

{

Class GIFImage: Image

{

Public override void ParseFile (string fileName)

{

// Parse the PNG file and obtain a pixel matrix object m;

Matrix m = new Matrix ();

Imp. DoPaint (m); // call the pixel conversion method of the Operating System

Console. WriteLine ("{0}, in GIF format. ", FileName );

}

}

}

13. Create a configuration file App. config

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<Deleetask>

<! -- Refinedaskaction -->

<Add key = "image" value = "BridgeSample. PNGImage"/>

<! -- ConcreteImplementor -->

<Add key = "OS" value = "BridgeSample. WindowsImp"/>

</AppSettings>

</Configuration>

14. Edit Program: client test class

Using System;

Using System. Configuration;

Using System. Reflection;

Namespace BridgeSample

{

Class Program

{

Static void Main (string [] args)

{

Image image;

ImageImp imp;

// Read the configuration file

String imageType = ConfigurationManager. receivettings ["image"];

String osType = ConfigurationManager. receivettings ["OS"];

// Reflect the generated object

Image = (Image) Assembly. Load ("BridgeSample"). CreateInstance (imageType );

Imp = (ImageImp) Assembly. Load ("BridgeSample"). CreateInstance (osType );

Image. SetImageImp (imp );

Image. ParseFile ("map of China ");

Console. Read ();

}

}

}

15. run mode code:

 

16. To change the image file format or OS, you only need to modify the configuration file.

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<Deleetask>

<! -- Refinedaskaction -->

<Add key = "image" value = "BridgeSample. BMP image"/>

<! -- ConcreteImplementor -->

<Add key = "OS" value = "BridgeSample. LinuxImp"/>

</AppSettings>

</Configuration>

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.