Blastasia,Inc. a software company, providing solutions globally    Welcome to Blastasia.com Home Contact Us Sitemap FAQ Inquiry Form Blastasia,Inc. a software company, providing solutions globally
Blastasia,a world class I.T company      TRANSFORMING BUSINESS USING I.T.   
 
Blastasia,Inc. a software company, providing solutions globally Contact Us
 
 
 
Featured Solutions
    Decorator Design Pattern gives Power to .Net Development
  And in order to have the power to show off some iconic power, we have the IconCellDecorator
   

public class IconCellDecorator : CellDecoratorBase
{
private Image image;
public IconCellDecorator(Image image, CellBase cell)
:base(cell)
{
this.image = image;
}

public override void Draw(Graphics graphics, int cellWidth, int cellHeight)
{
// Call contained component's draw method
base.Draw(graphics, cellWidth, cellHeight);

// draw icon.
Rectangle r = this.GetRectangle(cellWidth, cellHeight);
r.Inflate(-3, -3);
r.Width -= 50;
r.X = r.X + 50;

graphics.DrawImage(image, r);
}

public override string ToString()
{
string str = cell.ToString();
str += "\nWow! This is great, with IconCellDecorator power,";
str += "\n i can now show off my icon.";
return str;
}
}

 
I have also added a new Decorator just to drive the point that it is easy to add more behavior as needed. The BorderedCellDecorator allows you to draw a highlighted border around the control when it is clicked.
   

public class BorderedCellDecorator : CellDecoratorBase
{
private Color borderColor;
private int borderWidth = 4;
public BorderedCellDecorator(Color borderColor, CellBase cell)
: base(cell)
{
this.borderColor = borderColor;
}

public override void Draw(System.Drawing.Graphics graphics, int cellWidth, int cellHeight)
{
base.Draw(graphics, cellWidth, cellHeight);

Rectangle r = GetRectangle(cellWidth, cellHeight);
r.Inflate(-3, -3);
Brush b = new SolidBrush(Color.Orange);
Pen p = new Pen(b, borderWidth);

graphics.DrawRectangle(p, r);
}

public override string ToString()
{
string str = cell.ToString();
str += "\nAdded BorderedCellDecorator power,";
str += "\n i can now enclosed myself with a shield border.";
return str;
}
}

  Finally, in order to test our control and its amazing powers, we code something like this.
   

public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private int rows;
private int columns;

private void btnDraw_Click(object sender, EventArgs e)
{
rows = int.Parse(this.txtRows.Text);
columns = int.Parse(this.txtColumns.Text);

CreateCells(rows, columns);

this.Refresh();
}

protected override void OnPaint(PaintEventArgs e)
{
if (list == null) return;
if (list.Count == 0) return;

DrawLayout(e.Graphics);
}
private System.Collections.ArrayList list;
private void CreateCells(int rows, int columns)
{
list = new System.Collections.ArrayList();

CellBase cellBase = null;
Image img = Image.FromFile(@"bell.gif");

int counter = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// cellBase = new TableCell();
cellBase = new TableCell();

cellBase.RowNo = i;
cellBase.ColumnNo = j;
cellBase.TableNo = counter;

if (chkBackColor.Checked)
cellBase = new FillableCellDecorator(Color.Purple, cellBase);

if (chkText.Checked)
cellBase = new TextCellDecorator(counter.ToString(),
Color.Yellow, cellBase);

if (chkImage.Checked)
cellBase = new IconCellDecorator(img, cellBase);

list.Add(cellBase);
counter++;
}
}
}

// DrawLayout method using offscreen rendering to avoid flicker.
private void DrawLayout(Graphics g)
{
int cellWidth = 100;
int cellHeight = 50;

Bitmap bmpOff;
Graphics gxOff;

// Create a bitmap the size of the form.
bmpOff = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Pen whitePen = new Pen(Color.White, 3);

// Create a Graphics object that is not of the screen.
gxOff = Graphics.FromImage(bmpOff);
// gxOff.FillRectangle(new SolidBrush(Color.Red), 0, 0, bmpOff.Width, bmpOff.Height);

foreach (CellBase cell in list)
{
cell.Draw(gxOff, cellWidth, cellHeight);
}

// Render the off-screen bitmap onto the screen.
g.DrawImage(bmpOff, 0, 0, ClientRectangle, GraphicsUnit.Pixel);

// Dispose created Graphic object.
gxOff.Dispose();

}

protected override void OnMouseUp(MouseEventArgs e)
{
if (list == null) return;
if (list.Count == 0) return;

foreach (CellBase cell in list)
{
if (cell.Intersects(e.X, e.Y))
{
Graphics g = this.CreateGraphics();

DrawLayout(g);

this.lblCellTypeUnClicked.Text = cell.ToString();

// Wrapped object to have a highlight power.
CellBase clickedCell = new BorderedCellDecorator(Color.Orange, cell);
clickedCell.Draw(g, 100, 50);

this.lblCellTypeClicked.Text = clickedCell.ToString();
g.Dispose();

break;
}
}
}
}

  Here are some images of our application
      Simple Control of Decorators  
 
Click Image to view the full size
 
This image shows the Simple Control of Decorators
   

 

Simple Control of Decorators  
 
Click Image to view the full size
 
This image shows the Simple control of Decorators, with the ability to hide in a shade of purple
   


Simple Control of Decorators  
 
Click Image to view the full size
 
This image shows the Simple control of Decorators, with the ability to hide in a shade of purple and to show its number logo.
   

 

Simple Control of Decorators  
 
Click Image to view the full size
  This image shows the new amazing super ordinary control with all its powers.
 
In conclusion, using Decorators we can provide additional behaviors to an object. This can be done without resorting to subclassing. Behaviors can be attached or detached without the knowledge of the object being wrapped or decorated.
   
  References:
 

* Design Patterns Elements of Reusable Object Oriented Software by Gamma, Helm, Johnson, Vlissides

   
  For more information on this solution please email at contactus@blastasia.com
       
     
Back
 
   
 
   Solutions
 
    On-Line Inquiry
Tinette T. Arzadon
  (A.V.P. Business Development)
John Fajardo
  (Client Service Manager)
Meynard Aquino
  (Client Service Manager)
 
 
 
Home   :   Technology   :   Industries   :   Library   :   Partners   :   Clients   :   Dedicated Developer   :   Blastasia.us    :   Terms    :   Policies   :   Sitemap   :   Contact Us
    Copyright© 2008 Blastasia, Inc. All Rights Reserved