当前位置: 首页 > article >正文

8.Bridge 桥接模式(结构型模式)

【1】抽象A==>实现细节b

【2】抽象A==>抽象B<==实现细节b

【3】【抽象B】==相对稳定,也可能变化   【实现细节b】==频繁变化

【4】抽象B 不稳定?

思考问题:一个变化是平台(抽象B)的变化,另一个变化是型号(实现细节b)的变化。

【抽象部分B】

public abstract class Tank
{
    protected TankPlatformImplementation tankImple;
    public Tank(TankPlatformImplementation tankImple){this.tankImple=tankImple;}

    public abstract void Shot();
    public abstract void Run(); 
    public abstract void Trun();
}
public abstract class TankPlatformImplementation
{
    public abstract void MoveTankTO(Point to);//移动到某个点
    public abstract void DrawTank();          //画一个Tank
    public abstract void DoShot();            //播放声音,画出轨迹,特效等
}

 

 【实现细节b】各种实际实现通过继承来实现

public class T50:Tank { }
public class T75:Tank { }
public class T90:Tank { }

各种平台不同的实现: 

//PC                                   //Mobile
public class PCT50:T50 { }             public class MobileT50:T50 { }
public class PCT75:T75 { }             public class MobileT75:T75 { }
public class PCT90:T90 { }             public class MobileT90:T90 { }

软件加上时间轴,随着时间的变化,动态的创作软件系统。

using System;

namespace ConsoleApp2
{
    internal class Tank_Factory
    {
        public static void Main() 
        {
            TankPlatformImplementation tankImpl = new MobileTankImplementation();
            T50 tank = new T50(tankImpl);
            tank.Show();
            
              
            tank.TankImpl = new MobileTankImplementation();
            tank.Show();


            Console.WriteLine(tank.ToString());

            Console.WriteLine("Tank_Factory01");
            Console.ReadLine();
        }

    }


    public class Point
    {
    }
    //--Tank------------
    public abstract class Tank
    {
        protected TankPlatformImplementation tankImpl;
        protected Tank(TankPlatformImplementation tankImpl)
        {
            this.tankImpl = tankImpl;
        }

        public TankPlatformImplementation TankImpl
        {
            get { return this.tankImpl; }
            set { this.tankImpl = value; }
         }
        public abstract void Show();
        public abstract void Run();
        public abstract void Stop();

    }
    //--TankPlatformImplementation------------
    public abstract class TankPlatformImplementation
    {
        public abstract void MoveTank(Point to);
        public abstract void DrawTank();
        public abstract void DoShot();
    }
    //--PCTankImplementation------------------
    public class PCTankImplementation : TankPlatformImplementation
    {
        public override void DoShot()
        {
            Console.WriteLine("PCTankImplementation");
        }

        public override void DrawTank()
        {
            
        }

        public override void MoveTank(Point to)
        {
            
        }
    }
    //--MobileTankImplementation--------------
    public class MobileTankImplementation : TankPlatformImplementation
    {
        public override void DoShot()
        {
            Console.WriteLine("MobileTankImplementation");
        }

        public override void DrawTank()
        {

        }

        public override void MoveTank(Point to)
        {

        }
    }



    //--T50-------------
    public class T50 : Tank
    {
        public T50(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
           tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }
    //--T75-------------
    public class T75 : Tank
    {
        public T75(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }
    //--T90--------------
    public class T90 : Tank
    {
        public T90(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }

    //--PCT50--------------
    public class PCT50 : T50
    {
        public PCT50(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }
    //--PCT75--------------
    public class PCT75 : T75
    {
        public PCT75(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }
    //--PCT90--------------
    public class PCT90 : T90
    {
        public PCT90(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }


    //--MobileT50-------------
    public class MobileT50 : T50
    {
        public MobileT50(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
            
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }

    //--MobileT75-------------
    public class MobileT75 : T75
    {
        public MobileT75(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }

    //--MobileT90-------------
    public class MobileT90 : T90
    {
        public MobileT90(TankPlatformImplementation tankImpl) : base(tankImpl)
        {
        }

        public override void Run()
        {
            tankImpl.MoveTank(new Point());
        }

        public override void Show()
        {
            tankImpl.DoShot();
        }

        public override void Stop()
        {
            tankImpl.DrawTank();
        }
    }



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ImageImp temp1 = new LinuxImp();
            ImageImp temp = new WindowImp();

            Image imp = new JPGImage();
            imp.SetImageImp(temp);
            imp.Show("zhongguo");

            Image imp1= new PNGImage();

            imp1.SetImageImp(temp1);
            imp1.Show("zhongguo");

            Console.WriteLine("Program02--------------");
            Console.ReadLine();
        }
    }
    //-----------------------------------------------
    public class Matix
    {
        public int x;
        public int y;

        public Matix(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    public abstract class ImageImp { public abstract void DoPaint(Matix m); }

    public class WindowImp : ImageImp
    {
        public override void DoPaint(Matix m)
        {
            Console.WriteLine("在window系统中显示图像。");
        }
    }
    public class LinuxImp : ImageImp
    {
        public override void DoPaint(Matix m)
        {
            Console.WriteLine("在Linux系统中显示图像。");
        }
    }
    public class UnixxImp : ImageImp
    {
        public override void DoPaint(Matix m)
        {
            Console.WriteLine("在Linux系统中显示图像。");
        }
    }

    public abstract class Image 
    {
        protected ImageImp imageImp;
        public void SetImageImp(ImageImp imageImp) { this.imageImp = imageImp; }

        public abstract void Show(string s);

    }

    public class JPGImage : Image
    {

        public override void Show(string s)
        {
            imageImp.DoPaint(new Matix(10, 20));
            Console.WriteLine("JpgImp---");
        }
    }
    public class PNGImage : Image
    {

        public override void Show(string s)
        {
            imageImp.DoPaint(new Matix(10, 30));
            Console.WriteLine("PNGImp---");
        }
    }



    


    //----------------------------------------------

    public abstract class Implemetor//实施者
    {
        public abstract void OperationImpl();//活动,业务
    }

    public class ConcreteImplementor : Implemetor
    {
        public override void OperationImpl()
        {
            throw new NotImplementedException();
        }
    }

    public abstract class Abstraction 
    {
        protected Implemetor impl; //定义实现类接口对象
        public void SetImpl(Implemetor impl) 
        {
            this.impl = impl;
        }

        public abstract void Operation();//声明抽象业务方法
    }

    public class RefinedAbstraction : Abstraction
    {
        public override void Operation()
        {
            impl.OperationImpl(); //调用实现类代码
        }
    }

}

--------------------------------------------------------------------------------------------------------------------------------

桥接模式意图定义:将抽象部分与它他的实现部分解耦,使得两者都能够独立变化。--(GoF)

---------------------------------------------------------------------------------------------------------------------------------


http://www.kler.cn/a/500733.html

相关文章:

  • 【2024年华为OD机试】(A卷,100分)- 单词倒序(Java JS PythonC/C++)
  • C++实现设计模式---单例模式 (Singleton)
  • 修改sshd默认配置,提升安全
  • 工业 4G 路由器赋能远程医疗,守护生命线
  • [SMARTFORMS] 导出SMARTFORMS表单数据
  • Boost.Asio 同步读写及客户端 - 服务器实现详解
  • 前端学习-事件流,事件捕获,事件冒泡以及阻止冒泡以及相应案例(二十八)
  • YOLOv10-1.1部分代码阅读笔记-downloads.py
  • Docker基础篇(一)
  • 如何在C#中使用COM接口
  • Linux web资产收集
  • 计算机视觉算法实战——打电话行为检测
  • Windows下Dll在Unity中使用的一般方式
  • 运维高级课作业一
  • MeCo——给预训练数据增加源信息,就能减少33%的训练量并且提升效果
  • MYSQL-创建数据库 CREATE DATABASE (十一)
  • 蠕虫病毒会给服务器造成哪些危害?
  • vue3后台系统动态路由实现
  • centos 搭建nginx+配置域名+windows访问
  • Vue 开发者的 React 实战指南:性能优化篇
  • 【Ubuntu与Linux操作系统:九、Shell编程】
  • Perl语言的编程范式
  • 简历整理YH
  • Django 社团管理系统的设计与实现