Creating Extensible and Flexible Hosted Applications: Part 2


The Problem

For this article I will use the same requirements and goals defined in part 1. You are hosting your bill processing application for Company A and the new Customer B wants added functionality.

Possible Solution

In the previous version I used the factory pattern and inheritance to extend the base functionality for Company B. In this article I will show how to use the Decorator pattern for extending the functionality.  

using System;
using myBusinessObjects;
namespace ConsoleSample
{
class MySample
{
[STAThread]
static void Main(string[] args)
{
// test harness for company B
Bill myBill = new Bill();
myBill.CostofProduct = 10.25;
myBill.NumberOfProducts = 3;
CommissionBill CompanyBBill =
new CommissionBill(myBill);
CompanyBBill.Commission = 9.2;
Console.WriteLine(CompanyBBill.calculateBill().ToString());
/*
// test harness for company A
Bill myBill = new Bill();
myBill.CostofProduct = 10.25;
myBill.NumberOfProducts = 3;
Console.WriteLine(myBill.calculateBill().ToString());
*/

}
}
}
namespace myBusinessObjects
{ 
class Bill
{
public double CostofProduct;
public int NumberOfProducts;
public virtual double calculateBill()
{
return CostofProduct * NumberOfProducts;
}
public virtual double getDiscountPrice()
{
return CostofProduct * 0.8;
}
}
// This is an abstract class representing any
// future decorators of the base class Bill
// It must conform to the interface of the base class as
// well and maintain a reference to the instance class that
// it will decorate
abstract class BillDecorator : Bill
{
//maintain the instance
protected Bill BillItem;
public BillDecorator ( Bill BillItem )
{
this.BillItem = BillItem;
}
//direct the bill interface commands to the instance
public override double calculateBill()
{
return BillItem.calculateBill();
}
public override double getDiscountPrice()
{
return BillItem.getDiscountPrice();
}
}
class CommissionBill : BillDecorator
{
public CommissionBill( Bill BillItem ) : base( BillItem ) {}
public double Commission;
public override double calculateBill()
{
return base.calculateBill() + Commission;
}
}
}

Explanation

The Decorator pattern is used generally to dynamically extend the base class functionality. The pattern relies on a wrapper class (BillDecorator) to the base class (Bill). The wrapper class must maintain a reference to an instance of the base class that will be decorated at run time and also the wrapper must support the interface of the bill class. This combination allows the caller to pass an instance to be decorated and allows the decorator to override (decorate) functionality from the base class interface. It is typical to make the wrapper an abstract implementation (BillDecorator) so that many concrete classes (CommissionBill) may reuse the wrapper structure.

Applying to the Real World

In general you must be careful where this pattern is applied. Over utilizing the pattern could lead to overcomplicating a system and require extra coding and processing for the instantiation of the decorators.

Up Next
    Ebook Download
    View all
    Learn
    View all