我试图使用Windows Forms实现装饰器模式,并在此处提出一些问题。当我调用之前装饰实例的事件时,它部分不起作用,我无法弄清楚原因。不起作用的部分包括我实现的Windows.Forms.Form的操作,但Console.WriteLine完美地工作。您可以在下面测试我的代码,但您需要桌面上的11.txt文件(或更改路径)以使其正常工作;
using System.Windows.Forms;
using System.IO;
using System;
class Decorator
{
class UpgradedStream : Form
{
public TrackBar trbRead;
public Button btnRead;
public TextBox txtBox;
public UpgradedStream()
{
this.Width = 600;
this.Height = 600;
this.Text = "Decorating Stream class";
trbRead = new TrackBar();
trbRead.Width = 300;
trbRead.Value = 2;
btnRead = new Button();
btnRead.Click += new EventHandler(ReadFile);
btnRead.Text = "&Slide bar abowe!";
btnRead.Top = 40;
btnRead.Width = 150;
txtBox = new TextBox(); ;
txtBox.Multiline = true;
txtBox.Top = 75;
txtBox.Width = 550;
txtBox.Height = 450;
txtBox.ScrollBars = ScrollBars.Vertical;
this.Controls.Add(btnRead);
this.Controls.Add(trbRead);
this.Controls.Add(txtBox);
}
public virtual void ReadFile(Object source, EventArgs e)
{
Text = "123"; //doesn't work in decorator chain
Console.WriteLine("works"); //works
}
}
class DecoratedStream : UpgradedStream
{
public UpgradedStream previous;
public FileStream fin = null;
public int i, count = 0;
public DecoratedStream(UpgradedStream us)
{
previous = us;
fin = new FileStream("11.txt", FileMode.Open);
}
public override void ReadFile(Object source, EventArgs e)
{
previous.ReadFile(source, e);
try
{
fin = new FileStream("C:\\Users\\Vitaliy\\Desktop\\11.txt", FileMode.Open);
do
{
count++;
i = fin.ReadByte();
if (i != -1) txtBox.Text += (char)i;
} while (i != -1 && count < fin.Length * (trbRead.Value * 0.1));
}
catch (IOException exc)
{
txtBox.Text += ("Error Input-Output:\n" + exc.Message);
}
finally
{
txtBox.Text += "\r\n";
txtBox.Text += String.Format("WRITED: {0} SYMBOLS\r\n", Convert.ToInt32(fin.Length * (trbRead.Value * 0.1)));
txtBox.Text += "\r\n";
if (fin != null) fin.Close();
count = 0;
}
}
}
class PasswordForm : Form
{
private Label lblPassForm;
private Button btnPassForm;
private TextBox tbxPassForm;
public PasswordForm()
{
this.Text = "Enter password";
lblPassForm = new Label();
lblPassForm.Text = "password - 123";
tbxPassForm = new TextBox();
tbxPassForm.Top = 30;
btnPassForm = new Button();
btnPassForm.Text = "&OK";
btnPassForm.Top = 50;
btnPassForm.Click += new EventHandler(ClosePassForm);
btnPassForm.DialogResult = DialogResult.OK;
this.Controls.Add(lblPassForm);
this.Controls.Add(tbxPassForm);
this.Controls.Add(btnPassForm);
}
public void ClosePassForm(Object source, EventArgs e)
{
password = tbxPassForm.Text;
this.Close();
}
public String password { get; set; }
}
class AskPassword : UpgradedStream
{
public PasswordForm PassForm;
private string password;
protected internal UpgradedStream previous;
public AskPassword(UpgradedStream us)
{
previous = us;
}
public override void ReadFile(Object source, EventArgs e)
{
previous.ReadFile(source, e);
using (PassForm = new PasswordForm())
{
if (PassForm.ShowDialog() == DialogResult.OK)
{
if (PassForm.password == "123")
previous.ReadFile(source, e);
}
}
}
}
static void Main()
{
Application.EnableVisualStyles();
UpgradedStream up = new UpgradedStream();
DecoratedStream dk = new DecoratedStream(up);
AskPassword ask = new AskPassword(dk);
Application.Run(ask);
}
}
例如
public virtual void ReadFile(Object source, EventArgs e)
{
Text = "123"; //doesn't work in decorator chain
Console.WriteLine("works"); //works
}
答案 0 :(得分:0)
这是因为txtBox
是一个实例字段,并且不会在装饰器链中的类之间共享&#39;。如果您更改声明,以便它像以下一样静止:
public static TextBox txtBox;
然后,您将观察到可能更接近您所期望的行为。我并不是说这是首选解决方案,只是说明了问题。
通过分离演示和行为可能会更好。这将创造一个可以说是更清洁的解决方案。