《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

相关文章

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

Redis延迟队列的实现示例

《Redis延迟队列的实现示例》Redis延迟队列是一种使用Redis实现的消息队列,本文主要介绍了Redis延迟队列的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、什么是 Redis 延迟队列二、实现原理三、Java 代码示例四、注意事项五、使用 Redi

在Pandas中进行数据重命名的方法示例

《在Pandas中进行数据重命名的方法示例》Pandas作为Python中最流行的数据处理库,提供了强大的数据操作功能,其中数据重命名是常见且基础的操作之一,本文将通过简洁明了的讲解和丰富的代码示例,... 目录一、引言二、Pandas rename方法简介三、列名重命名3.1 使用字典进行列名重命名3.编

Python使用Colorama库美化终端输出的操作示例

《Python使用Colorama库美化终端输出的操作示例》在开发命令行工具或调试程序时,我们可能会希望通过颜色来区分重要信息,比如警告、错误、提示等,而Colorama是一个简单易用的Python库... 目录python Colorama 库详解:终端输出美化的神器1. Colorama 是什么?2.

Go Gorm 示例详解

《GoGorm示例详解》Gorm是一款高性能的GolangORM库,便于开发人员提高效率,本文介绍了Gorm的基本概念、数据库连接、基本操作(创建表、新增记录、查询记录、修改记录、删除记录)等,本... 目录1. 概念2. 数据库连接2.1 安装依赖2.2 连接数据库3. 数据库基本操作3.1 创建表(表关

Python视频剪辑合并操作的实现示例

《Python视频剪辑合并操作的实现示例》很多人在创作视频时都需要进行剪辑,本文主要介绍了Python视频剪辑合并操作的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录介绍安装FFmpegWindowsMACOS安装MoviePy剪切视频合并视频转换视频结论介绍

python多进程实现数据共享的示例代码

《python多进程实现数据共享的示例代码》本文介绍了Python中多进程实现数据共享的方法,包括使用multiprocessing模块和manager模块这两种方法,具有一定的参考价值,感兴趣的可以... 目录背景进程、进程创建进程间通信 进程间共享数据共享list实践背景 安卓ui自动化框架,使用的是

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.