Reputation: 1056
I have a sheet class in my application:
class Sheet
{
Public int[] RatioIDs{get;set;}
Public int[] PartnerIDs{get;set;}
}
This sheet class have thousands of instances created and stored at the run time, However when I process them I would be working at max 5 sheet objects.Only before processing a sheet object I decorate it with additional properties and methods to avoid the overhead:
//Assume I have decorated the sheet object with following additional responsiblities.
class DecoratedSheet
{
//There would be like 100 partner object in this array.
//and these partners are fetched based on partnerIDs array in sheet object
Public Partner[] Partners;
public double GetRatioAmountForPartner(Partner partner)
{
//Get ratio amount, its calculation would be based on
//RatioID array in sheet object for a given partner.
}
}
Once I'm done with a decorated sheet object, I don't need its additional properties (esp. the array of 100 partners). So I what to withdraw the additional responsibilities attached to this decorated sheet object. How should I do this ??
Upvotes: 0
Views: 105
Reputation: 658
class Sheet : ISheetWithPartners {
public IEnumerable<int> RatioIDs { get; set; }
public IEnumerable<int> PartnerIDs { get; set; }
public IEnumerable<Partner> Partners { get; set; }
public double GetRatioAmountForPartner(Partner partner) {
// Your code here
}
}
interface ISheet {
IEnumerable<int> RatioIDs { get; }
IEnumerable<int> PartnerIDs { get; }
}
interface ISheetWithPartners : ISheet {
IEnumerable<Partner> Partners { get; }
double GetRatioAmountForPartner(Partner partner);
}
Then you can deal with interfaces. Easier than creating functions like .GetBase() or whatever
Upvotes: 1
Reputation: 304
Do you want to retain the decorated state of the object or do you just want to fetch the original object in it's original state? The idea of a decorator is that you have a base object with minimal properties and you add to it at run time. In this case I don't know what the decorator is - you haven't specified it in your code. Eg. is Partner a decorator for Sheet? Where does the actual 'Sheet' object come into play? DecoratedSheet takes a Partner object and stores it in an array of Partners, but where does Sheet come in to play?
Easiest way would be to implement a getComponent() or getBase() method that returns a Sheet object from whatever you're decorating it with. Each decorator you create and add to the original object can and should have access to the properties of the object, whilst containing their own properties, so that you can swap and change as you like.
I hope that helps.
The wiki page has a good example: http://en.wikipedia.org/wiki/Decorator_pattern
Upvotes: 0