《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:备忘录接口

示例:备忘录接口
说明:
(1)、定义
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

(2)、结构
 clip_image002
Memento(备忘录):备忘录存储原发器对象的内部状态。原发器根据需要决定备忘录存储原发器的哪些内部状态。防止原发器以外的其他对象访问备忘录。备忘录实际上有两个接口,管理者(caretaker)只能看到备忘录的窄接口-它只能将备忘录传递给其他对象。相反,原发器能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。理想的情况是只允许生成本备忘录的那个原发器访问本备忘录的内部状态。
Originator(原发器):原发器创建一个备忘录,用以记录当前时刻它的内部状态。使用备忘录恢复内部状态.。备忘录是被动的。只有创建备忘录的原发器会对它的状态进行赋值和检索。
Caretaker(负责人):负责保存好备忘录。不能对备忘录的内容进行操作或检查。

界面:
 clip_image002[5]
object Form2: TForm2
  Left = 192
  Top = 120
  Width = 244
  Height = 107
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 40
    Top = 32
    Width = 75
    Height = 25
    Caption = '操作'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 136
    Top = 32
    Width = 75
    Height = 25
    Caption = '恢复'
    TabOrder = 1
    OnClick = Button2Click
  end
end

代码:
unit uMemento;

interface

{注:Delphi没有friend指令;但在同一单元内所有对象皆可以存取
    其它对象中宣告私有的部分;亦即皆为朋友的关系。}
uses Dialogs;

type
    {状态}
    TState = class
    private
        FValue: string;
    public
        procedure Assign(const AState: TState);
        //---
        property Value: string read FValue write FValue;
    end;

    {备忘录}
    TMemento = class
    private
        FState: TState;
        function GetState: TState;
        procedure SetState(const Value: TState);
    protected
        property State: TState read GetState write SetState;
    public
        constructor Create(AState: TState);
        destructor Destroy; override;
    end;

    {原发器}
    TOrigintor = class
    private
        FState: TState;
    public
        constructor Create;
        destructor Destroy; override;
        //---
        function CreateMemento: TMemento;
        procedure SetMemento(AMemento: TMemento);
        //----
        procedure Operate(const Value:string);
        procedure Show;
    end;

    {负责人}
    TCaretaker = class
    private
        FMemento: TMemento;
        procedure SetMemento(const Value: TMemento);
    public
        constructor Create;
        destructor Destroy; override;
        //---
        property Memento: TMemento read FMemento write SetMemento;
    end;

implementation

function TOrigintor.CreateMemento: TMemento;
begin
    Result := TMemento.Create(FState);
end;

procedure TOrigintor.SetMemento(AMemento: TMemento);
begin
    FState.Assign(AMemento.State);
end;

procedure TOrigintor.Show;
begin
    ShowMessage(FState.Value);
end;

procedure TOrigintor.Operate(const Value:string);
begin
    FState.Value := Value;
end;

constructor TMemento.Create(AState: TState);
begin
    FState := TState.Create;
    self.SetState(AState);
end;

destructor TMemento.Destroy;
begin
    FState.Free;
    //---
    inherited;
end;

function TMemento.GetState: TState;
begin
    Result := FState;
end;

procedure TMemento.SetState(const Value: TState);
begin
    FState.Assign(Value);
end;

constructor TCaretaker.Create;
begin
    FMemento := nil;
end;

destructor TCaretaker.Destroy;
begin
    self.Memento := nil;
    //---
    inherited;
end;

procedure TCaretaker.SetMemento(const Value: TMemento);
begin
    if FMemento <> nil then
        FMemento.Free;
    FMemento := Value;
end;

{ TState }

procedure TState.Assign(const AState: TState);
begin
    self.FValue := AState.FValue;
end;



constructor TOrigintor.Create;
begin
    FState := TState.Create;
end;

destructor TOrigintor.Destroy;
begin
    FState.Free;
    //---
    inherited;
end;

end.

unit Unit2;

interface

uses
    Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
    Dialogs,StdCtrls,uMemento;

type
    TForm2 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
    private
        FOrigintor: TOrigintor;
        FCaretaker: TCaretaker;
    public
    { Public declarations }
    end;

var
    Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
    with FOrigintor do
    begin
        FCaretaker.Memento := CreateMemento;
        //---
        Operate(DateTimeToStr(Now));
        Show;
    end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
    with FOrigintor do
    begin
        SetMemento(FCaretaker.Memento);
        Show;
    end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
    FOrigintor := TOrigintor.Create;
    FCaretaker := TCaretaker.Create;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
    FOrigintor.Free;
    FCaretaker.Free;
end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值