Decorator Pattern Code Review
Use the Decorator Pattern
Code Review
- Decorator Pattern - Part 1: Define the Decorated Stream Object
- Decorator Pattern - Part 2: Use the Decorator Pattern
Code Walkthrough
Create an instance of the Stream class (object) and pass it as a parameter to the StreamDecorator class (object).
Stream fStrm = null;
StreamDecorator dStrm = null;
Byte[] bytes;
Int32 bytesToRead;
try { this.textBox1.Text = String.Empty; this.textBox1.Refresh();
fStrm = new FileStream("streamDecorator.txt", FileMode.Open, FileAccess.Read); dStrm = new StreamDecorator(fStrm);
bytes = new Byte[dStrm.Length]; bytesToRead = dStrm.NumBytesToRead; dStrm.MaxBytesToRead = 50;
progressBar1.Minimum = 0; progressBar1.Maximum = dStrm.NumBytesToRead;
while (bytesToRead > 0) { bytesToRead = dStrm.Read(bytes); progressBar1.Value = dStrm.TotalBytesRead; this.label2.Text = dStrm.TotalBytesRead.ToString(); this.label2.Refresh(); } this.textBox1.Text = dStrm.ReadText; this.textBox1.Refresh(); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { dStrm.Close(); fStrm.Close(); }
try { this.textBox1.Text = String.Empty; this.textBox1.Refresh();
fStrm = new FileStream("streamDecorator.txt", FileMode.Open, FileAccess.Read); dStrm = new StreamDecorator(fStrm);
bytes = new Byte[dStrm.Length]; bytesToRead = dStrm.NumBytesToRead; dStrm.MaxBytesToRead = 50;
progressBar1.Minimum = 0; progressBar1.Maximum = dStrm.NumBytesToRead;
while (bytesToRead > 0) { bytesToRead = dStrm.Read(bytes); progressBar1.Value = dStrm.TotalBytesRead; this.label2.Text = dStrm.TotalBytesRead.ToString(); this.label2.Refresh(); } this.textBox1.Text = dStrm.ReadText; this.textBox1.Refresh(); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { dStrm.Close(); fStrm.Close(); }
Invoke the standard Stream methods, as well as, the new custom operations in the StreamDecorator class (object).
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!