模式学习(1):Decorator装饰者模式

 

学习(主要参考 刘艺 delphi模式编程)

概念:Decorator以对客户端透明的方式动态的为对象提供修饰等附加的功能。

使用时机:1、在不影响其他对象的情况下,动态透明的增加责任或功能到某一对象。

              2、这些功能可以随时添加或取消;

              3、为了增加大量的功能,而导致派生类激增,继承关系复杂而不现实时;累的定义被隐藏或定义不方便派生类的。

模式结构:1、抽象的部件:定义一个接口(类),可以动态附加在其他对象上。抽象虚拟类。(可理解为要实现的功能,当然是类中定义的方法)

                     TMethod:=class(TObject);

                            procedure XXX();virtual;Abstract;

                     end;

              2、具体的需要被修饰的部件:被修饰的(类)对象。

                     TSomething=class(TMethod);

                             procedure XXX();override;

              3、装饰者:定义与抽象部件(1)一致的接口(函数),维护一个到1的引用,以便修饰抽象部件对象的接口。

                    TDecorateSomething=class(TMethod); 

                               procedure XXX();override;                         

              4、具体装饰者:附加到2上,实现不同的功能效果。

                    TSomething臭美1=class(TDecorateSomething);procedure XXX();override;

                    TSomething臭美2=class(TDecorateSomething);procedure XXX();override;

 

类图:

此类图根据刘艺的例子使用modalmaker11制作。

View Code
  1 unit PicDecorator;
2
3 interface
4
5 uses Windows, Controls, Classes, SysUtils, Forms, Graphics, Dialogs, Jpeg,
6 Extctrls, ComCtrls, Messages;
7
8 type
9 TPicShow = class(TObject)
10 public
11 procedure Display(AWoner: TForm; ImgFile: string); virtual; abstract;
12 end;
13
14 TPic = class(TPicShow)
15 public
16 procedure Display(AWoner: TForm; ImgFile: string); override;
17 end;
18
19 TDecoratedPic = class(TPicShow)
20 private
21 FComponent: TPicShow;
22 public
23 constructor Create(PicShow: TPicShow);
24 procedure Display(AWoner: TForm; ImgFile: string); overload; override;
25 // property Component: TPicShow read FComponent;
26 end;
27
28 TPicWithMusic = class(TDecoratedPic)
29 public
30 destructor Destroy; override;
31 procedure AddMusic;
32 procedure Display(AWoner: TForm; ImgFile: string); override;
33 end;
34
35 TPicWithFrame = class(TDecoratedPic)
36 private
37 FAddedFrame: Integer;
38 public
39 destructor Destroy; override;
40 procedure Display(AWoner: TForm; ImgFile: string); override;
41 end;
42
43 implementation
44
45 {
46 ************************************* TPic *************************************
47 }
48 procedure TPic.Display(AWoner: TForm; ImgFile: string);
49 var
50 Img: TImage;
51 begin
52 Img := TImage.Create(AWoner);
53 Img.Picture.LoadFromFile(ImgFile);
54 Img.AutoSize := true;
55 Img.Stretch := true;
56 AWoner.Width := Img.Width + 32;
57 AWoner.Height := Img.Height + 64;
58 AWoner.Caption := ImgFile;
59 Img.Left := 11;
60 Img.Top := 13;
61 Img.Parent := AWoner;
62 end;
63
64 {
65 ******************************** TDecoratedPic *********************************
66 }
67 constructor TDecoratedPic.Create(PicShow: TPicShow);
68 begin
69 self.FComponent := PicShow;
70 end;
71
72 procedure TDecoratedPic.Display(AWoner: TForm; ImgFile: string);
73 begin
74 if FComponent <> nil then
75 begin
76 FComponent.Display(AWoner, ImgFile);
77 end;
78 end;
79
80 {
81 ******************************** TPicWithMusic *********************************
82 }
83 destructor TPicWithMusic.Destroy;
84 begin
85 // 结束播放,sndPlaySOund(nil,snd_noDefault);
86 showmessage('Over Music');
87 if FComponent <> nil then
88 FComponent.Free;
89 end;
90
91 procedure TPicWithMusic.AddMusic;
92 begin
93 // 添加播放的音乐
94 showmessage('add music');
95 end;
96
97 procedure TPicWithMusic.Display(AWoner: TForm; ImgFile: string);
98 begin
99 inherited Display(AWoner, ImgFile);
100 AddMusic;
101 end;
102
103 {
104 ******************************** TPicWithFrame *********************************
105 }
106 destructor TPicWithFrame.Destroy;
107 begin
108 if FComponent <> nil then
109 FComponent.Free;
110 end;
111
112 procedure TPicWithFrame.Display(AWoner: TForm; ImgFile: string);
113 var
114 FrmOut:TBevel;
115 FrmIn:TBevel;
116 begin
117 inherited Display(AWOner,imgfile);
118 FrmIn:=TBevel.Create(AWoner);
119 FrmIn.Parent:=AWoner;
120 frmin.Width:=AWoner.Width-30;
121 frmin.Height:=AWOner.Height-62;
122 frmin.Left:=10;
123 frmin.Top:=12;
124 frmin.Shape:=bsBox;
125 frmin.Style:=bsLowered;
126
127 frmout:=TBevel.Create(AWOner);
128 Frmout.Parent:=AWoner;
129 frmout.Width:=AWoner.Width-18;
130 frmout.Height:=AWOner.Height-48;
131 frmout.Left:=4;
132 frmout.Top:=6;
133 frmout.Shape:=bsBox;
134 frmout.Style:=bsraised;
135 end;
136
137 end.
View Code
 1 unit Unit3;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.FileCtrl;
8
9 type
10 TForm3 = class(TForm)
11 DirectoryListBox1: TDirectoryListBox;
12 DriveComboBox1: TDriveComboBox;
13 Edit1: TEdit;
14 Panel1: TPanel;
15 FileListBox1: TFileListBox;
16 FilterComboBox1: TFilterComboBox;
17 Button1: TButton;
18 Button2: TButton;
19 RadioGroup1: TRadioGroup;
20 Bevel1: TBevel;
21 Image1: TImage;
22 procedure Button1Click(Sender: TObject);
23 procedure Button2Click(Sender: TObject);
24 procedure Edit1KeyPress(Sender: TObject; var Key: Char);
25 procedure FileListBox1Click(Sender: TObject);
26 private
27 { Private declarations }
28 imgfilename:string;
29 public
30 { Public declarations }
31 end;
32
33 var
34 Form3: TForm3;
35
36 implementation
37 uses PicDecorator;
38 {$R *.dfm}
39
40 procedure TForm3.Button1Click(Sender: TObject);
41 var
42 decopic:TPicShow;
43 currfrm:TForm;
44 begin
45 currfrm:=TForm.Create(nil);
46 case self.RadioGroup1.ItemIndex of
47 0: decopic:=TPicwithframe.Create(Tpic.Create);
48 1: decopic:=TPicwithMusic.Create(Tpic.Create);
49 2: ;
50 end;
51 try
52 decopic.Display(currfrm,imgfilename);
53 currfrm.ShowModal;
54
55 finally
56 decopic.Free;
57 currfrm.Free;
58 end;
59 end;
60
61 procedure TForm3.Button2Click(Sender: TObject);
62 begin
63 close;
64 end;
65
66 procedure TForm3.Edit1KeyPress(Sender: TObject; var Key: Char);
67 begin
68 if key=#13 then
69 begin
70 filelistbox1.ApplyFilePath(edit1.Text);
71 key:=#0;
72 end;
73 end;
74
75 procedure TForm3.FileListBox1Click(Sender: TObject);
76 var
77 fileExt:string[4];
78 begin
79 fileExt:=ansiUppercase(ExtractfileExt(filelistbox1.FileName));
80 self.Button1.Enabled:=false;
81 if (fileExt='.JPG') then
82 begin
83 self.Image1.Picture.LoadFromFile(filelistbox1.FileName);
84 imgfilename:=filelistbox1.FileName;
85 button1.Enabled:=true;
86 end;
87
88 end;
89
90 end.

代码为实现的例子。

posted @ 2012-02-17 01:12  步履蹒跚  阅读(254)  评论(0编辑  收藏  举报