《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

相关文章

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

SpringBoot 中 CommandLineRunner的作用示例详解

《SpringBoot中CommandLineRunner的作用示例详解》SpringBoot提供的一种简单的实现方案就是添加一个model并实现CommandLineRunner接口,实现功能的... 目录1、CommandLineRunnerSpringBoot中CommandLineRunner的作用

Java死锁问题解决方案及示例详解

《Java死锁问题解决方案及示例详解》死锁是指两个或多个线程因争夺资源而相互等待,导致所有线程都无法继续执行的一种状态,本文给大家详细介绍了Java死锁问题解决方案详解及实践样例,需要的朋友可以参考下... 目录1、简述死锁的四个必要条件:2、死锁示例代码3、如何检测死锁?3.1 使用 jstack3.2

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult