教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 小学教育 >

软件设计模式复习题(4)

来源:网络收集 时间:2026-08-31
导读: 而剩下的不能共享的属性,则可以由客户端维护,并在需要的时候传入享元,传入的不能共享的属性不能影响内部的不可变属性。 12、 使用解析器模式实现对学生成绩表的查询输入语句进行解析并执行查询,成绩表 Stack存

而剩下的不能共享的属性,则可以由客户端维护,并在需要的时候传入享元,传入的不能共享的属性不能影响内部的不可变属性。 12、 使用解析器模式实现对学生成绩表的查询输入语句进行解析并执行查询,成绩表

Stack存储解析后信息类: namespace Interpreter{ public class Stack{ ArrayList stk; public Stack(){ stk = new ArrayList ();} public void push(Object obj) { stk.Add (obj); } public ParseObject pop() { int i = stk.Count -1; ParseObject obj = (ParseObject) stk[i]; stk.RemoveAt (i); return obj;} public ParseObject top() { int i = stk.Count -1; return (ParseObject)stk[i];} public bool hasMoreElements() { return stk.Count > 0;} public ParseObject nextTop() {

int i = stk.Count -2; if (i >= 0) return (ParseObject)stk[i]; else return null;} public void pop2Push(ParseObject p){ int i = stk.Count ; if (i >= 2) { pop(); pop(); push(p);} else push(p);}}} ParseObject解析对象类: public class ParseObject{ public const int VERB=1000; public const int VAR=1010; public const int MULTVAR=1020; protected int value, type; public ParseObject(int val, int typ) { value = val; type = typ; } public int getValue() { return value;} public int getType() { return type;}} Parser解析类: public class Parser{

public Parser(string line, StudentData kd, ListBox pt) { stk = new Stack (); actionList = new ArrayList (); setData(kd, pt); buildStack(line); buildChain();}

public void setData(StudentData kd, ListBox pt) { dat = new Data(kd.getData ()); ptable = pt;} public void Execute() { while(stk.hasMoreElements () ) { chn.sendToChain (stk);} for(int i=0; i< actionList.Count ; i++ ) { Verb v = (Verb)actionList[i]; v.setData (dat, ptable); v.Execute ();}}}

Form1窗口:

namespace Interpreter{ public class InterpForm : System.Windows.Forms.Form{ private System.Windows.Forms.TextBox txCommand; private System.Windows.Forms.Button btCompute; private System.Windows.Forms.ListBox lsResults; private StudentData kdata; public InterpForm(){ InitializeComponent(); init();} private void init() { kdata = new StudentData ("data.txt");}

[STAThread] static void Main() { Application.Run(new InterpForm());}

private void btCompute_Click(object sender, System.EventArgs e) {

Parser par = new Parser (txCommand.Text ,kdata, lsResults); par.Execute ();}}} 13、 结合命令模式和备忘录模式实现程序的undo和redo功能。 答:Form1:

public Form1() { InitializeComponent(); init();}

private void init() {

btBlue.setCommand (blueC=new BlueCommand (pBox)); btRed.setCommand (redC=new RedCommand (pBox)); btUndo.setCommand (undoC = new UndoComd ()); btRedo.setCommand(redoC = new RedoComd()); EventHandler evh = new EventHandler (commandClick); btBlue.Click += evh; btRed.Click += evh; btUndo.Click += evh; btRedo.Click += evh; pBox.Paint += new PaintEventHandler (paintHandler); redoundo();}

public void redoundo(){

RedoComd.redolist = new ArrayList(); if (RedoComd.redolist.Count < 1){ btRedo.Enabled = false;}

UndoComd.undolist = new ArrayList(); if (UndoComd.undolist.Count < 1){ btUndo.Enabled = false;}} private void commandClick(object sender, EventArgs e) { Command comd = ((CommandHolder)sender).getCommand ();

undoC.add (comd); comd.Execute (); redoC.add(comd); comd.Execute();}

public void paintHandler(object sender, PaintEventArgs e) {

Graphics g = e.Graphics ; blueC.draw(g); redC.draw (g); }

[STAThread]

static void Main(){

Application.Run(new Form1());} Command:

public interface Command { void Execute(); void Undo(); void Redo(); bool isUndo(); bool isRedo();}

public class UndoComd:Command { public static ArrayList undolist; public UndoComd(){

undolist = new ArrayList();} public void add(Command comd) { if(! comd.isUndo ()) {

undolist.Add(comd);}} public bool isUndo() { return true;} public void Undo() { } public void Execute() { int unIndex =undolist.Count - 1; if (unIndex >= 0){

Command cmd = (Command)undolist[unIndex]; cmd.Undo();

RedoComd.redolist = new ArrayList(); RedoComd.redolist.Add(cmd); undolist.Remove(unIndex);}}} public class RedoComd:Command { public static ArrayList redolist; public RedoComd(){

redolist = new ArrayList();} public void add(Command comd) { if(! comd.isRedo ()) {

redolist.Add(comd);}} public bool isRedo() {

return true; } public void Redo() { } public void Execute() { int reIndex =redolist.Count - 1; if (reIndex >= 0){

Command cmd = (Command)redolist[reIndex]; cmd.Redo();

UndoComd.redolist = new ArrayList(); UndoComd.redolist.Add(cmd); redolist.Remove(reIndex);}}} 14、 试比较和分析中介者模式和观察者模式之间的相似性和差异性?两者是否能够相

互转化?

答:中介者模式和观察者模式的区别是,前者应用于多对多杂乱交互行为的统筹处理,后者应用于一(多)对多关系的灵活定制。对于本例来说,集中处理后还需要分散处理,那么后半阶段的处理过程可以应用观察者模式。对于前一节的例子来说,如果有多个主体角色和多个观察者进行多对多通讯的话,也可以应用中介者模式来统筹这个多对多的过程(大家可以自己尝试修改前一节的实例来应用中介者模式)。 15、 使用访问者模式对12题中的学生信息进行报到。 答:public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable. /// </summary>

private ponentModel.Container components = null; private Student[] empls; private Student empl ;

private System.Windows.Forms.Button btCompute; private System.Windows.Forms.ListBox lsVac;…… 此处隐藏:4437字,全部文档内容请下载后查看。喜欢就下载吧 ……

软件设计模式复习题(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/42737.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)