博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转载] C#面向对象设计模式纵横谈——21 Memento备忘录模式
阅读量:7295 次
发布时间:2019-06-30

本文共 2555 字,大约阅读时间需要 8 分钟。

主讲:李建忠

来源:

 

 

1:  [Serializable]
2:  public class Rectangle
3:  {
4:      int x;
5:      int y;
6:      int weight;
7:      int height;
8:
9:      pulic void SetValue(Rectangle r)
10:      {
11:          this.x=r.x;
12:          this.y=r.y;
13:          this.weight=r.weight;
14:          this.height=r.height;
15:      }
16:   
17:      public Rectangle(int x,int y,int weight,int height)
18:      {
19:          this.x=x;
20:          this.y=y;
21:          this.weight=weight;
22:          this.height=height;
23:      }
24:   
25:      public void MoveTo(Point p)
26:      {
27:   
28:      }
29:   
30:      public void ChangeWidth(int width)
31:      {
32:   
33:      }
34:   
35:      public void ChangeHeight(int height)
36:      {
37:   
38:      }
39:   
40:      public void Draw(Graphic graphic)
41:      {
42:   
43:      }
44:   
45:      public GeneralMemento CreateMemento()
46:      {
47:          GeneralMemento gm=new GeneralMemento();
48:          gm.SetState(this);
49:          return gm;
50:      }
51:   
52:      public void SetMemento(GeneralMemento gm)
53:      {
54:          Recgletangle r=(Retangle)gm.GetState();
55:   
56:          this.x=r.x;
57:          this.y=r.y;
58:          this.weight=r.weight;
59:          this.height=r.height;
60:      }
61:  }
62:   
63:  class GeneralMemento
64:  {
65:      Stream rSaved;
66:      public GeneralMemento(Factory factory)
67:      {
68:          rSaved=factory.CreateStream();
69:      }
70:   
71:      public void SetState(object r)
72:      {
73:          BinaryFormat bf=new BinaryFormat();
74:          bf.Serializa(r,rSaved);
75:      }
76:   
77:      internal object GetState()
78:      {
79:          BinaryFormat bf=new BinaryFormat();
80:          rSaved.Seek(0,seek.Original);
81:          object obj=bf.DeSerialize(rSaved);
82:   
83:          return obj;
84:      }
85:  }
86:   
87:  //处于另外的程序集中
88:  class GraphicSystem
89:  {
90:      //原发器对象
91:      //有必要对自身内部状态进行保存,然后再某个点处又需要恢复内部状态的对象
92:      Rectangle r=new Rectangle(0,0,10,10);
93:   
94:      //备忘录对象——保存原发器对象的状态,但是不提供原发器对象支持的操作
95:      //MemoryStream rSaved=new MemoryStream();
96:   
97:      public static void Main()
98:      {
99:          Process(r);
100:      }
101:   
102:      public static void Process(Rectangle r)
103:      {
104:          gm=r.CreateMemento();
105:   
106:          //...
107:      }
108:  }
109:   
110:  public void Saved_Click(object sender,EventArgs e)
111:  {
112:      gm.SetMemento(r);
113:  }

 

转载于:https://www.cnblogs.com/6DAN_HUST/archive/2012/10/18/2728984.html

你可能感兴趣的文章
hdu1078 记忆化搜索
查看>>
2017 清北济南考前刷题Day 3 afternoon
查看>>
洛谷P2326 AKN’s PPAP
查看>>
WERKZEUG之WSGI阅读笔记
查看>>
一个初学者C#编写帐号密码保存软件的思考过程
查看>>
【软件解决】解决VS环境中出现虚线问题
查看>>
laravel 实现增 与查
查看>>
一种排序
查看>>
Linux实战教学笔记44:NoSQL数据库开篇之应用指南
查看>>
springmvc(2)处理器设配器和映射器
查看>>
PAT 1003
查看>>
switch gnome-terminal tabs
查看>>
怎样理解Functor与Monad
查看>>
DRF教程4-视图集和路由类
查看>>
javascript向上滚动(放上鼠标就停)
查看>>
python的编码问题
查看>>
获取下拉框的值
查看>>
windows server2012 R2 本地策略编辑
查看>>
数据结构与算法(3)图
查看>>
VB Script 如何使用动态数组分配
查看>>