zoukankan      html  css  js  c++  java
  • CSharp设计模式读书笔记(8):桥接模式(学习难度:★★★☆☆,使用频率:★★★☆☆)

    桥接模式(Bridge Pattern):

    将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

    模式角色与结构:

     示例代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CSharp.DesignPattern.BridgePattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                Image image = new JPGImage();
                ImageImp imp = new WindowsImp();
    
                image.SetImageImp(imp);
                image.ParseFile("c:\txt");
    
                Console.ReadLine();
            }
    
            //像素矩阵类:辅助类,各种格式的文件最终都被转化为像素矩阵,不同的操作系统提供不同的方式显示像素矩阵  
            class Matrix
            {
                //此处代码省略  
            }
    
            //抽象图像类:抽象类 
            abstract class Image
            {
                protected ImageImp imp;
    
                public void SetImageImp(ImageImp imp)
                {
                    this.imp = imp;
                }
    
                public abstract void ParseFile(String fileName);
            }
    
            //JPG格式图像:扩充抽象类  
            class JPGImage : Image {
                public override void ParseFile(String fileName)
                {  
                    //模拟解析JPG文件并获得一个像素矩阵对象m;  
                    Matrix m = new Matrix();   
                    imp.DoPaint(m);  
                    Console.WriteLine(fileName + ",格式为JPG。");  
                }  
            }
    
            //PNG格式图像:扩充抽象类  
            class PNGImage : Image
            {
                public override void ParseFile(String fileName)
                {
                    //模拟解析PNG文件并获得一个像素矩阵对象m;  
                    Matrix m = new Matrix();
                    imp.DoPaint(m);
                    Console.WriteLine(fileName + ",格式为PNG。");
                }
            }
    
            //BMP格式图像:扩充抽象类   
            class BMPImage : Image
            {
                public override void ParseFile(String fileName)
                {
                    //模拟解析BMP文件并获得一个像素矩阵对象m;  
                    Matrix m = new Matrix();
                    imp.DoPaint(m);
                    Console.WriteLine(fileName + ",格式为BMP。");
                }
            }
    
            //GIF格式图像:扩充抽象类
            class GIFImage : Image
            {
                public override void ParseFile(String fileName)
                {
                    //模拟解析GIF文件并获得一个像素矩阵对象m;  
                    Matrix m = new Matrix();
                    imp.DoPaint(m);
                    Console.WriteLine(fileName + ",格式为GIF。");
                }
            }
    
            //抽象操作系统实现类:实现类接口 
            interface ImageImp
            {
                void DoPaint(Matrix m);
            }
    
            //Windows操作系统实现类:具体实现类 
            class WindowsImp : ImageImp
            {
                public void DoPaint(Matrix m)
                {
                    //调用Windows系统的绘制函数绘制像素矩阵  
                    Console.WriteLine("在Windows操作系统中显示图像:");
                }
            }
    
            //Linux操作系统实现类:具体实现类 
            class LinuxImp : ImageImp
            {
                public void DoPaint(Matrix m)
                {
                    //调用Linux系统的绘制函数绘制像素矩阵  
                    Console.WriteLine("在Linux操作系统中显示图像:");
                }
            }
    
            //Unix操作系统实现类:具体实现类 
            class UnixImp : ImageImp
            {
                public void DoPaint(Matrix m)
                {
                    //调用Unix系统的绘制函数绘制像素矩阵  
                    Console.WriteLine("在Unix操作系统中显示图像:");
                }
            }
        }
    }
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993345.html
Copyright © 2011-2022 走看看