《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:可移植的用户界面

本文主要是介绍《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:可移植的用户界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


示例:可移植的用户界面

说明:

 

代码:

 

unit uWindow;

 

interface

 

uses

Windows,SysUtils,Classes,Graphics;

 

type

    TWindow = class;

    TWindowImp = class;

 

    {窗口视图}

    TView = class

    public

        //---

        procedure Draw(const AWindow: TWindow);

    end;

 

    {窗口抽象类}

    TWindow = class

    private

        FCanvas: TCanvas;

        FContents: TView;

        FImp: TWindowIMP;

        function GetView(): TView;

        function GetWindowIMP: TWindowIMP;

        procedure SetOrigin(const Value: TPoint);

        procedure SetExtent(const Value: TPoint);

    protected

        procedure DrawLine(const p1,p2: TPoint);

        procedure DrawRect(const p1,p2: TPoint);

        procedure DrawPolygon(const Points: array of TPoint);

        procedure DrawText(const text: string; position: TPoint);

        //---

        property WindowIMP: TWindowIMP read GetWindowIMP;

        property View: TView read GetView;

    public

        constructor Create(ACanvas: TCanvas; contents: TView);

        //---

        procedure Open; virtual;

        procedure Close(); virtual;

        procedure Iconify(); virtual;

        procedure Deiconfy(); virtual;

        //---

        procedure Raised(); virtual;

        procedure Lower(); virtual;

        //---

        procedure DrawContents; virtual; abstract;

        //---

        property Origin: TPoint write SetOrigin;

        property Extent: TPoint write SetExtent;

    end;

 

    TApplicationWindow = class(TWindow)

    public

        procedure DrawContents(); override;

        procedure DrawCloseBox;

    end;

 

    TIconWindow = class(Twindow)

    private

        FBitmapName: string;

    public

        procedure DrawContents; override;

        procedure DrawBorder;

        //---

        property BitmapName: string write FBitmapName;

    end;

 

    {窗口实现类}

    TWindowImp = class

    protected

        procedure SetOrigin(const Value: TPoint); virtual;

        procedure SetExtent(const Value: TPoint); virtual;

    public

        constructor Create;

        //---

        procedure DeviceDrawLine(const p1,p2: TPoint); virtual; abstract;

        procedure DeviceDrawPolygon(const Points: array of TPoint); virtual; abstract;

        procedure DeviceDrawRect(const x1,y1,x2,y2: integer); virtual; abstract;

        procedure DeviceDrawText(const Text: string; x1,y1: integer); virtual; abstract;

        procedure DeviceDrawBitmap(const ABitmapName: string; x1,y1: integer); virtual; abstract;

        //---

        property Origin: TPoint write SetOrigin;

        property Extent: TPoint write SetExtent;

    end;

 

    TXWindowImp = class(TWindowImp)

    private

        FCanvas: TCanvas;

    public

        constructor Create(ACanvas: TCanvas);

        //---

        procedure DeviceDrawLine(const p1,p2: TPoint); override;

        procedure DeviceDrawRect(const x1,y1,x2,y2: integer); override;

        procedure DeviceDrawText(const Text: string; x1,y1: integer); override;

        procedure DeviceDrawBitmap(const ABitmapName: string; x1,y1: integer); override;

        procedure DeviceDrawPolygon(const Points: array of TPoint); override;

    end;

 

    TPMWindowImp = class(TWindowImp)

    private

        FCanvas: TCanvas;

    public

        constructor Create(ACanvas: TCanvas);

        //---

        procedure DeviceDrawLine(const p1,p2: TPoint); override;

        procedure DeviceDrawRect(const x1,y1,x2,y2: integer); override;

        procedure DeviceDrawText(const Text: string; x1,y1: integer); override;

        procedure DeviceDrawBitmap(const ABitmapName: string; x1,y1: integer); override;

        procedure DeviceDrawPolygon(const Points: array of TPoint); override;

    end;

 

    TWindowSystemFactory = class

    public

        constructor Create;

        destructor Destroy; override;

        //---

        class function Instance(): TWindowSystemFactory;

        function MakeWindowImp(ACanvas: TCanvas): TWindowImp;

    end;

 

implementation

 

var

    FFactory: TWindowSystemFactory;

 

procedure TView.Draw(const AWindow: TWindow);

begin

    AWindow.DrawText('abc',Point(12,12));

end;

 

constructor TWindow.Create(ACanvas: TCanvas; contents: TView);

begin

    FCanvas := ACanvas;

    FContents := contents;

end;

 

procedure TWindow.Open;

begin

end;

 

procedure TWindow.Close();

begin

end;

 

procedure TWindow.Iconify();

begin

end;

 

procedure TWindow.Deiconfy();

begin

end;

 

procedure TWindow.SetExtent(const Value: TPoint);

begin

    self.WindowIMP.Extent := Value;

end;

 

procedure TWindow.Raised();

begin

end;

 

procedure TWindow.Lower();

begin

end;

 

procedure TWindow.DrawLine(const p1,p2: TPoint);

begin

    self.WindowIMP.DeviceDrawLine(p1,p2);

end;

 

procedure TWindow.DrawRect(const p1,p2: TPoint);

begin

    self.WindowIMP.DeviceDrawRect(p1.x,p1.y,p2.x,p2.y);

end;

 

procedure TWindow.DrawPolygon(const Points: array of TPoint);

begin

    self.WindowIMP.DeviceDrawPolygon(Points);

end;

 

procedure TWindow.DrawText(const text: string; position: TPoint);

begin

    self.WindowIMP.DeviceDrawText(text,position.X,position.Y);

end;

 

function TWindow.GetWindowIMP: TWindowIMP;

begin

    if FImp = nil then

        FImp := TWindowSystemFactory.Instance.MakeWindowImp(FCanvas);

    //---

    Result := FImp;

end;

 

function TWindow.GetView(): TView;

begin

    Result := FContents;

end;

 

procedure TWindow.SetOrigin(const Value: TPoint);

begin

    self.WindowIMP.Origin := Value;

end;

 

constructor TWindowSystemFactory.Create;

begin

    if FFactory = nil then

        FFactory := Self

    else

        abort;

end;

 

destructor TWindowSystemFactory.Destroy;

begin

    FFactory := nil;

    //---

    inherited;

end;

 

class function TWindowSystemFactory.Instance(): TWindowSystemFactory;

begin

    if FFactory = nil then

        FFactory := TWindowSystemFactory.Create;

    //---

    Result := FFactory;

end;

 

function TWindowSystemFactory.MakeWindowImp(ACanvas: TCanvas): TWindowImp;

    //---

    function GetImpNameByIni: string;

    begin

        Result := 'PM';

    end;

var

    AImpName: string;

begin

    AImpName := GetImpNameByIni;

    //---

    if AImpName = 'PM' then

        Result := TPMWindowImp.Create(ACanvas)

    else if AImpName = 'X' then

        Result := TXWindowImp.Create(ACanvas)

    else

        Result := TPMWindowImp.Create(ACanvas);

end;

 

constructor TWindowImp.Create;

begin

end;

 

procedure TWindowImp.SetExtent(const Value: TPoint);

begin

end;

 

procedure TWindowImp.SetOrigin(const Value: TPoint);

begin

end;

 

procedure TApplicationWindow.DrawCloseBox;

begin

    self.DrawRect(Point(10,10),Point(50,50));

end;

 

procedure TApplicationWindow.DrawContents();

begin

    self.View.Draw(self);

end;

 

procedure TIconWindow.DrawBorder;

begin

    self.DrawRect(Point(10,10),Point(50,50));

    self.DrawText('123',Point(12,12));

end;

 

procedure TIconWindow.DrawContents();

begin

    if FBitmapName <> '' then

        self.WindowIMP.DeviceDrawBitmap(FBitmapName,0,0);

    //----

    self.WindowIMP.DeviceDrawLine(Point(10,10),Point(50,50));

end;

 

constructor TXWindowImp.Create(ACanvas: TCanvas);

begin

    inherited Create;

    //---

    FCanvas := ACanvas;

end;

 

procedure TXWindowImp.DeviceDrawBitmap(const ABitmapName: string; x1,y1:

    integer);

    //---

    procedure _XDrawBitmap;

    var

        AGraphic: TBitmap;

    begin

        AGraphic := TBitmap.Create;

        try

            AGraphic.LoadFromFile(ABitmapName);

            FCanvas.Draw(0,0,AGraphic);

        finally

            AGraphic.Free;

        end;

    end;

begin

    _XDrawBitmap;

end;

 

procedure TXWindowImp.DeviceDrawLine(const p1,p2: TPoint);

    //---

    procedure _XDrawLine;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clRed;

                Width := 1;

                Style := psSolid;

            end;

            //---

            MoveTo(p1.X,p1.X);

            LineTo(p2.X,p2.X);

        end;

    end;

begin

    _XDrawLine;

end;

 

procedure TXWindowImp.DeviceDrawPolygon(const Points: array of TPoint);

    //---

    procedure _XDrawPolygon;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clRed;

                Width := 1;

                Style := psSolid;

            end;

            Brush.Color := clWhite;

            //---

            Polygon(Points);

        end;

    end;

begin

    _XDrawPolygon;

end;

 

procedure TXWindowImp.DeviceDrawRect(const x1,y1,x2,y2: integer);

    //---

    procedure _XDrawRect;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clRed;

                Width := 1;

                Style := psSolid;

            end;

            Brush.Color := clWhite;

            //---

            Rectangle(x1,y1,x2,y2);

        end;

    end;

begin

    _XDrawRect;

end;

 

procedure TXWindowImp.DeviceDrawText(const Text: string; x1,y1: integer);

    //---

    procedure _XDrawString;

    begin

        with FCanvas do

        begin

            with Font do

            begin

                Name := '宋体';

                Size := 11;

                Color := clRed;

            end;

            TextOut(x1,y1,Text);

        end;

    end;

begin

    _XDrawString;

end;

 

constructor TPMWindowImp.Create(ACanvas: TCanvas);

begin

    inherited Create;

    //---

    FCanvas := ACanvas;

end;

 

procedure TPMWindowImp.DeviceDrawBitmap(const ABitmapName: string; x1,y1:

    integer);

    //---

    procedure _PMDrawBitmap;

    var

        AGraphic: TBitmap;

    begin

        AGraphic := TBitmap.Create;

        try

            AGraphic.LoadFromFile(ABitmapName);

            FCanvas.StretchDraw(Rect(0,0,AGraphic.Width div 2,AGraphic.Height div 2),AGraphic);

        finally

            AGraphic.Free;

        end;

    end;

begin

    _PMDrawBitmap;

end;

 

procedure TPMWindowImp.DeviceDrawLine(const p1,p2: TPoint);

    //---

    procedure _PMDrawLine;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clBlue;

                Width := 1;

                Style := psDot;

            end;

            //---

            MoveTo(p1.X,p1.X);

            LineTo(p2.X,p2.X);

        end;

    end;

begin

    _PMDrawLine;

end;

 

procedure TPMWindowImp.DeviceDrawPolygon(const Points: array of TPoint);

    //---

    procedure _PMDrawPolygon;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clBlue;

                Width := 1;

                Style := psDot;

            end;

            Brush.Color := clWhite;

            //---

            Polygon(Points);

        end;

    end;

begin

    _PMDrawPolygon;

end;

 

procedure TPMWindowImp.DeviceDrawRect(const x1,y1,x2,y2: integer);

    //---

    procedure _PMDrawRect;

    begin

        with FCanvas do

        begin

            with Pen do

            begin

                Color := clBlue;

                Width := 1;

                Style := psDot;

            end;

            Brush.Color := clYellow;

            //---

            Rectangle(x1,y1,x2,y2);

        end;

    end;

begin

    _PMDrawRect;

end;

 

procedure TPMWindowImp.DeviceDrawText(const Text: string; x1,y1: integer);

    //---

    procedure _PMDrawString;

    begin

        with FCanvas do

        begin

            with Font do

            begin

                Name := '宋体';

                Size := 11;

                Color := clBlue;

            end;

            TextOut(x1,y1,Text);

        end;

    end;

begin

    _PMDrawString;

end;

 

end.

procedure TForm1.Button1Click(Sender: TObject);

var

    AWindow: TWindow;

    AView: TView;

begin

    AView := TView.Create;

    AWindow := TApplicationWindow.Create(self.Canvas,AView);

    try

        AWindow.DrawContents;

    finally

        AWindow.Free;

        AView.Free;

    end;

end;

这篇关于《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:可移植的用户界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142334

相关文章

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C