《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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

Java中Scanner的用法示例小结

《Java中Scanner的用法示例小结》有时候我们在编写代码的时候可能会使用输入和输出,那Java也有自己的输入和输出,今天我们来探究一下,对JavaScanner用法相关知识感兴趣的朋友一起看看吧... 目录前言一 输出二 输入Scanner的使用多组输入三 综合练习:猜数字游戏猜数字前言有时候我们在