《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

相关文章

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++