设计模式之桥接模式

1、类图

实例类图

2、创建项目

……………………

3、 新建Matrix:像素矩阵类,辅助类,各种格式的图像文件最终都会被转化为像素矩阵,不同的操作系统提供不同的方式现实像素矩阵。

namespace BridgeSample

{

    class Matrix

    {

        //代码省略

    }

} 

4、 新建ImageImp:抽象操作系统实现类,充当实现类接口

namespace BridgeSample

{

    interface ImageImp

    {

        void DoPaint(Matrix m);  //显示像素矩阵

    }

}

5、 新建WindowsImp:Windows操作系统实现类,充当具体实现类 

using System;

namespace BridgeSample

{

    class WindowsImp : ImageImp

    {

        public void DoPaint(Matrix m)

        {

         //调用Windows系统的绘制函数绘制像素矩阵

         Console.Write("在Windows操作系统中显示图像:");

        }

    }

6、 新建LinuxImp:Linux操作系统实现类,充当具体实现类

//LinuxImp.cs

using System;

namespace BridgeSample

{

    class LinuxImp : ImageImp

    {

        public void DoPaint(Matrix m)

        {

            //调用Linux系统的绘制函数绘制像素矩阵

            Console.Write("在Linux操作系统中显示图像:");

        }

    }

}

7、 新建UnixImp:UNIX操作系统实现类,充当具体实现类 

using System; 

namespace BridgeSample

{

    class UnixImp : ImageImp

    {

        public void DoPaint(Matrix m)

        {

            //调用Unix系统的绘制函数绘制像素矩阵

            Console.Write("在Unix操作系统中显示图像:");

        }

    }

}

8、 Image:抽象图像类,充当抽象类

namespace BridgeSample

{

    abstract class Image

    {

        protected ImageImp imp;

        //注入实现类接口对象

        public void SetImageImp(ImageImp imp)

        {

            this.imp = imp;

        }

        /// <summary>

        /// 文件转换到像素矩阵

        /// </summary>

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

        public abstract void ParseFile(string fileName);

    }

9、 JPGImage:JPG格式图像类,充当扩充抽象类 

using System;

namespace BridgeSample

{

    class JPGImage : Image

    {

        public override void ParseFile(string fileName)

        {

            //模拟解析JPG文件并获得一个像素矩阵对象m;

            Matrix m = new Matrix();

            imp.DoPaint(m); //调用操作系统的像素转换方法

            Console.WriteLine("{0},格式为JPG。",fileName);

        }

    }

}

10、 新建PNGImage:PNG格式图像类,充当扩充抽象类

using System;

namespace BridgeSample

{

    class PNGImage : Image

    {

        public override void ParseFile(string fileName)

        {

            //模拟解析PNG文件并获得一个像素矩阵对象m;

            Matrix m = new Matrix();

            imp.DoPaint(m);//调用操作系统的像素转换方法

            Console.WriteLine("{0},格式为PNG。", fileName);

        }

    }

11、 新建BMPImage:BMP格式图像类,充当扩充抽象类

using System;

namespace BridgeSample

{

    class BMPImage : Image

    {

        public override void ParseFile(string fileName)

        {

            //模拟解析PNG文件并获得一个像素矩阵对象m;

            Matrix m = new Matrix();

            imp.DoPaint(m);//调用操作系统的像素转换方法

            Console.WriteLine("{0},格式为BMP。", fileName);

        }

    }

}

12、 新建GIFImage:GIF格式图像类,充当扩充抽象类 

using System; 

namespace BridgeSample

{

    class GIFImage : Image

    {

        public override void ParseFile(string fileName)

        {

            //模拟解析PNG文件并获得一个像素矩阵对象m;

            Matrix m = new Matrix();

            imp.DoPaint(m);//调用操作系统的像素转换方法

            

            Console.WriteLine("{0},格式为GIF。", fileName);

        }

    }

13、 新建 配置文件App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <!--RefinedAbstraction-->

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

    <!--ConcreteImplementor-->

    <add key="os" value="BridgeSample.WindowsImp"/>

  </appSettings>

</configuration>

14、 编辑Program:客户端测试类

using System;

using System.Configuration;

using System.Reflection;

namespace BridgeSample

{

    class Program

    {

        static void Main(string[] args)

        {

            Image image;

            ImageImp imp;

            //读取配置文件

            string imageType = ConfigurationManager.AppSettings["image"];

            string osType = ConfigurationManager.AppSettings["os"];          

            //反射生成对象

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

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

            image.SetImageImp(imp);

            image.ParseFile("中国地图");

            Console.Read();

        }

    }

15、 运行模式代码:

 

16、如果需要更换图像文件格式或者更换操作系统,只需修改配置文件即可。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <!--RefinedAbstraction-->

    <add key="image" value="BridgeSample.BMPImage"/>

    <!--ConcreteImplementor-->

    <add key="os" value="BridgeSample.LinuxImp"/>

  </appSettings>

</configuration> 

原文地址:https://www.cnblogs.com/cqxhl/p/6097399.html